【问题标题】:'GridSearchCV' object has no attribute 'best_params_' when using LogisticRegression使用 LogisticRegression 时,“GridSearchCV”对象没有属性“best_params_”
【发布时间】:2020-07-02 00:27:35
【问题描述】:

下面是我要执行的代码

# Train a logistic regression model, report the coefficients and model performance 
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_val_score
from sklearn import metrics

clf = LogisticRegression().fit(X_train, y_train)
params = {'penalty':['l1','l2'],'dual':[True,False],'C':[0.001, 0.01, 0.1, 1, 10, 100, 1000], 'fit_intercept':[True,False],
        'solver':['saga']}
gridlog = GridSearchCV(clf, params, cv=5, n_jobs=2, scoring='roc_auc')

cv_scores = cross_val_score(gridlog, X_train, y_train)

#find best parameters
print('Logistic Regression parameters: ',gridlog.best_params_) # throws error

上面的最后一行代码是引发错误的地方。我已经使用这个完全相同的代码来运行其他模型。知道为什么我可能会遇到这个问题吗?

【问题讨论】:

  • 与错误本身无关(请参阅下面的答案),我们不将cross_val_score 用于 GridSearchCV 对象;相反,我们在拟合对象后使用best_score_ 属性

标签: python machine-learning scikit-learn logistic-regression gridsearchcv


【解决方案1】:

你需要先适应gridlogcross_val_score 不会这样做,它只返回分数,没有别的。 因此,由于 gridlog 没有经过训练,它会引发错误。

以下代码运行良好:

from sklearn import datasets
from sklearn.model_selection import cross_val_score
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import GridSearchCV
diabetes = datasets.load_breast_cancer()
x = diabetes.data[:150]
y = diabetes.target[:150]
clf = LogisticRegression().fit(x, y)
params = {'C':[0.001, 0.01, 0.1, 1, 10, 100, 1000]}
gridlog = GridSearchCV(clf, params, cv=2, n_jobs=2, 
scoring='roc_auc')
gridlog.fit(x,y) # <- missing in your code
cv_scores = cross_val_score(gridlog, x, y)
print(cv_scores)
#find best parameters
print('Logistic Regression parameters: ',gridlog.best_params_)
# result:
Logistic regression parameters: {'C': 1}

【讨论】:

  • 您可能需要更新答案以指出我们通常不将cross_val_score 用于 GridSearchCV 对象;相反,我们在拟合对象后使用best_score_ 属性
【解决方案2】:

应更新您的代码,以便将 LogisticRegression 分类器传递给 GridSearch(不适合):

from sklearn.datasets import load_breast_cancer # For example only
X_train, y_train = load_breast_cancer(return_X_y=True)

params = {'penalty':['l1', 'l2'],'dual':[True, False],'C':[0.001, 0.01, 0.1, 1, 10, 100, 1000], 'fit_intercept':[True, False],
        'solver':['saga']}

gridlog = GridSearchCV(LogisticRegression(), params, cv=5, n_jobs=2, scoring='roc_auc')
gridlog.fit(X_train, y_train)

#find best parameters
print('Logistic Regression parameters: ', gridlog.best_params_) # Now it displays all the parameters selected by the grid search

结果

Logistic Regression parameters:  {'C': 0.1, 'dual': False, 'fit_intercept': True, 'penalty': 'l2', 'solver': 'saga'}

注意,正如@desertnaut 所指出的,您不要将cross_val_score 用于GridSearchCV。

查看如何使用 GridSearch here 的完整示例。 该示例使用 SVC 分类器而不是 LogisticRegression,但方法相同。

【讨论】:

    猜你喜欢
    • 2020-07-02
    • 2020-04-20
    • 2019-03-29
    • 2023-03-26
    • 2021-10-03
    • 2020-03-13
    • 2020-01-31
    • 2016-03-15
    • 2017-05-22
    相关资源
    最近更新 更多