[Python] Python LogisticRegression() and RandomSearch: RuntimeWarning

Discussão em 'Python' iniciado por Stack, Maio 27, 2025.

  1. Stack

    Stack Membro Participativo

    Working on an assignment to knock out a logistic regression with random search for tuning, but I keep getting runtime warnings. Any idea what i'm doing wrong?

    /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/sklearn/utils/extmath.py:205: RuntimeWarning: divide by zero encountered in matmul
    ret = a @ b
    /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/sklearn/utils/extmath.py:205: RuntimeWarning: overflow encountered in matmul
    ret = a @ b
    /Library/Frameworks/Python.framework/Versions/3.12/lib/python3.12/site-packages/sklearn/utils/extmath.py:205: RuntimeWarning: invalid value encountered in matmul


    X = hospital_df_a.drop(columns=['stay_7days'])
    y = hospital_df_a['stay_7days']

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



    numeric_columns_if = X_train.select_dtypes(include=[np.number]).columns
    cat_columns_if = X_train.select_dtypes(include=['object']).columns

    numeric_transformer= Pipeline(
    steps=[("imputer", SimpleImputer(strategy="median")),
    ("scaler",StandardScaler())])

    categorical_transformer = OneHotEncoder(handle_unknown="ignore")

    preprocessor_if = ColumnTransformer(
    transformers=[
    ("num", numeric_transformer, numeric_columns_if),
    ("cat", categorical_transformer, cat_columns_if)])

    r_param_grid_if = [
    # Grid for L1 and L2 penalties
    {
    'logreg__penalty': ['elasticnet'],
    'logreg__solver': ['saga'],
    'logreg__l1_ratio': loguniform(0.05, 1.0),
    'logreg__C': [0.01, 0.1, 1], # Regularization strengths
    'logreg__max_iter': [5000, 10000, 20000],
    'logreg__class_weight': ['balanced'] # Handling class imbalance
    }, {
    'logreg__penalty': ['l1', 'l2'],
    'logreg__solver': ['liblinear'],

    'logreg__C': [0.01, 0.1, 1], # Regularization strengths
    'logreg__max_iter': [5000, 10000, 20000],
    'logreg__class_weight': [None, 'balanced']
    }]

    model = Pipeline([('preprocessor', preprocessor_if),
    ('logreg',LogisticRegression(tol=1e-4))])

    random_search = RandomizedSearchCV(model, param_distributions=r_param_grid_if,
    cv=3, scoring='precision', n_jobs=-1,
    n_iter=50, verbose=1)

    random_search.fit(X_train, y_train)

    Continue reading...

Compartilhe esta Página