[Python] I'm working on a classification task using scikit-learn (LogisticRegression())

Discussão em 'Python' iniciado por Stack, Junho 14, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on a classification task using scikit-learn. After filtering and scaling my dataset, I'm trying to train a LogisticRegression model and compute the confusion matrix and F1 score. Here's the code I'm using:

    import pandas as pd
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import MinMaxScaler
    from sklearn.linear_model import LogisticRegression
    from sklearn.metrics import confusion_matrix, f1_score

    df_filtered = df[(df['MIP'] >= 10) & (df['MIP'] <= 100)]
    df_sorted = df_filtered.sort_values(by='SIP')

    X = df_sorted.drop(columns='target') # target is the response column
    y = df_sorted['target']

    X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, stratify=y, random_state=33
    )

    scaler = MinMaxScaler()
    X_train_scaled = pd.DataFrame(scaler.fit_transform(X_train), columns=X_train.columns)
    X_test_scaled = pd.DataFrame(scaler.transform(X_test), columns=X_test.columns)

    lr_model = LogisticRegression()
    lr_model.fit(X_train_scaled, y_train)
    y_pred_lr = lr_model.predict(X_test_scaled)

    cm_lr = confusion_matrix(y_test, y_pred_lr)
    print(cm_lr)

    tp_lr = cm_lr[1][1]
    print(tp_lr)

    f1_lr = f1_score(y_test, y_pred_lr)
    print(round(f1_lr, 3))


    The problem is that the model either returns a warning or unexpected metrics. Sometimes the F1 score is 0, or the confusion matrix only shows one class. What could be the issue? Could it be related to the scaling, stratification, or the formatting of the target variable?

    Let me know if more data samples or specific error messages are needed.

    Continue reading...

Compartilhe esta Página