【发布时间】: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