首页 > 解决方案 > 显示网格搜索属性,因为它们不是该方法的一部分

问题描述

我正在尝试使用 GridSearchCV 方法使用“log loss”对逻辑回归和 SGDClassifier 执行超参数调整。但是在打印 best_score_ 时,best_params_ 属性会出现错误,因为它们不是 GridSearchCV 方法的一部分。我已经安装了更新的 scikit 包(0.20.3)。查不出原因?还是我在网格搜索方法上启动多个模型时出错?有什么指导方针会有所帮助吗?

我已将 sklearn 软件包重新安装到最新的软件包,但仍然没有帮助!

我正在尝试检查 LogisticRegression() 方法和 SGDClassifier() 的逻辑模型性能与对数损失,因此了解随机梯度下降如何在度量结果中产生差异。以下是代码片段:

from sklearn.model_selection import GridSearchCV

estimator = [logr_w2v, sgd_lr_w2v]

para = {'C' : [0.0001, 0.001, 0.01, 0.1, 1, 5, 10, 100, 1000], 'alpha' : [0.0001, 0.001, 0.01, 0.1, 1, 5, 10, 100, 1000]}

Grid_w2v = GridSearchCV(estimator, param_grid = para, scoring = 'f1', n_jobs= 4, refit = True , cv = 5)

GridSearchCV(cv=5, error_score='raise-deprecating',
       estimator=[LogisticRegression(C=1.0, class_weight=None, dual=False, fit_intercept=True,
          intercept_scaling=1, max_iter=1000, multi_class='warn', n_jobs=4,
          penalty='l1', random_state=50, solver='saga', tol=0.0001,
          verbose=0, warm_start=False), SGDClassifier(alpha=0.0001, ...dom_state=50, shuffle=True, tol=0.001,
       validation_fraction=0.1, verbose=0, warm_start=False)],
       fit_params=None, iid='warn', n_jobs=4,
       param_grid={'C': [0.0001, 0.001, 0.01, 0.1, 1, 5, 10, 100, 1000], 'alpha': [0.0001, 0.001, 0.01, 0.1, 1, 5, 10, 100, 1000]},
       pre_dispatch='2*n_jobs', refit=True, return_train_score='warn',
       scoring='f1', verbose=0)

hyperparameter = []


for i in range(len(estimator)):

    Grid_w2v.estimator[i].fit(x1_w2v, y_true)

    hyperparameter.append((Grid_w2v.estimator[i]).best_params_)

    print("the best score value on each estimator is: ",Grid_w2v.best_score_)

我期待上述循环将在超参数中保存来自 Logisitic Regression 和 SGDClassifier 的超参数。但我收到以下错误:

AttributeError                            Traceback (most recent call last)
<ipython-input-44-b53708a5f29e> in <module>()
AttributeError: 'GridSearchCV' object has no attribute 'best_score_'

标签: python-3.xscikit-learngrid-search

解决方案


推荐阅读