【问题标题】:GridSearchCV scikit-learn: TypeError LogisticRegression...does not implement a 'get_params' methodsGridSearchCV scikit-learn:TypeError LogisticRegression...不实现“get_params”方法
【发布时间】:2020-08-21 10:15:22
【问题描述】:

我想使用 sklearn 的 GridSearchCV(参见 https://towardsdatascience.com/hyperparameter-tuning-c5619e7e6624)优化逻辑回归估计器的超参数,基于以下代码:

X_train, X_test, y_train, y_test,indices_train,indices_test = train_test_split(features_all2, df_all['labels'], df_all.index, test_size=0.25, random_state=1)

penalty = ['l1', 'l2']
C = [0.0001, 0.001, 0.01, 0.1, 1, 10, 100, 1000]
class_weight = ['balanced']
solver = ['liblinear', 'saga']

param_grid = dict(penalty=penalty,
                  C=C,
                  class_weight=class_weight,
                  solver=solver)

grid = GridSearchCV(estimator=LogisticRegression,
                    param_grid=param_grid,
                    scoring='roc_auc',
                    verbose=1,
                    n_jobs=-1)
grid_result = grid.fit(X_train, y_train)

print('Best Score: ', grid_result.best_score_)
print('Best Params: ', grid_result.best_params_)

直到grid_result = grid.fit(X_train, y_train) 我得到错误 TypeError: Cannot clone object '' (type ): 它似乎不是 scikit-learn 估计器,因为它没有实现 'get_params' 方法。 .

虽然在执行 hasattr(LogisticRegression, 'get_params') 时我得到 True

我被困在这里。那里的任何人都可能知道如何处理这个问题?非常感谢!

【问题讨论】:

  • 应该是estimator= LogisticRegression()

标签: python typeerror clone logistic-regression gridsearchcv


【解决方案1】:

您需要传递estimator= LogisticRegression() 而不是estimator= LogisticRegression

示例:

from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LogisticRegression
grid={"C":np.logspace(-3,3,7), "penalty":["l1","l2"]}# l1 lasso l2 ridge
logreg=LogisticRegression()
logreg_cv=GridSearchCV(logreg,grid,cv=10)

【讨论】:

    猜你喜欢
    • 2013-10-01
    • 2020-07-27
    • 2020-09-02
    • 2019-07-23
    • 2019-09-30
    • 1970-01-01
    • 2019-12-09
    • 2016-02-24
    • 1970-01-01
    相关资源
    最近更新 更多