【问题标题】:Print parameters and scores GridSearchCV打印参数和分数 GridSearchCV
【发布时间】:2023-03-03 08:54:27
【问题描述】:

我不明白为什么我不能在 GridSearchCV 中使用不同的参数打印所有分数。

代码:

from sklearn.svm import SVC

pipe_svm = Pipeline([
    ('sc', StandardScaler()),
    ('SVM', SVC())
    ])

params_svm = {'SVM__C': np.logspace(-2, 10, 13),
              'SVM__kernel': ['rbf', 'poly', 'sigmoid']}

search_svm = GridSearchCV(estimator=pipe_svm,
                      param_grid=params_svm,
                      cv = 5,
                      return_train_score=True)

search_svm.fit(X_train, y_train)
print(search_svm.best_score_)
print(search_svm.best_params_)

输出:

0.9004240532229588
{'SVM__C': 1.0, 'SVM__kernel': 'rbf'}

这很好,但我想用给定的参数打印所有不同的分数(与最好的比较)。以下是我尝试过的,缺少很多不同的参数组合与各自的分数。

代码:

scores_svm = search_svm.cv_results_['mean_test_score']
for score, C, kernel in zip(scores_svm, np.logspace(-2, 10, 13), ['rbf', 'poly', 'sigmoid']):
    print(f"{C, kernel}: {score:.10f}")

输出:

0.01, rbf: 0.8500203678
0.1, poly: 0.6785667684
1.0, sigmoid: 0.8364788196

所需的输出将包括 np.logspace(-2, 10, 13) 中具有不同内核的所有 C 值并分配相应的分数。像这样的:

0.001, rbf: corresponding score
0.01, rbf: corresponding score
1.0, rbf: corresponding score
10.0, rbf: corresponding score
.
.
.

等等

【问题讨论】:

  • 只需将cv_results_ 加载到熊猫数据框中?

标签: python scikit-learn printf grid-search


【解决方案1】:

这应该是:

kernels = ['rbf', 'poly', 'sigmoid']
C = np.logspace(-2, 10, 13)
for idx, kernel in enumerate(kernels):
    for score, c in (zip(scores_svm[idx*len(C): (idx+1)*len(C)], C)):
        print(f"{c, kernel}: {score:.10f}")
        

其实len(scores_svm)13*nlen(np.logspace(-2, 10, 13))13

当它们的长度不同时,如何将它们拉在一起。这就是为什么在将它们拉在一起时,您只会得到其中的三个。因为压缩包内的最小 len 为 3。

示例代码:

from sklearn.svm import SVC
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
import numpy as np
from sklearn.model_selection import GridSearchCV

X_train = np.array([[-1, -1], [-2, -1], [1, 1], [2, 1], [3, 1], [3, 2], [2, 3]])
y_train = np.array([1, 1, 2, 2, 1, 2, 1])
pipe_svm = Pipeline([
    ('sc', StandardScaler()),
    ('SVM', SVC())
    ])

params_svm = {'SVM__C': np.logspace(-2, 10, 13),
              'SVM__kernel': ['rbf', 'poly', 'sigmoid']}

search_svm = GridSearchCV(estimator=pipe_svm,
                      param_grid=params_svm,
                      cv = 2,
                      return_train_score=True)

search_svm.fit(X_train, y_train)
print(search_svm.best_score_)
print(search_svm.best_params_)

# 0.41666666666666663
# {'SVM__C': 0.01, 'SVM__kernel': 'rbf'}

scores_svm = search_svm.cv_results_['mean_test_score']
for score, C, kernel in zip(scores_svm, np.logspace(-2, 10, 13), ['rbf', 'poly', 'sigmoid']):
    print(f"{C, kernel}: {score:.10f}")

(0.01, 'rbf'): 0.4166666667
(0.1, 'rbf'): 0.4166666667
(1.0, 'rbf'): 0.4166666667
(10.0, 'rbf'): 0.4166666667
(100.0, 'rbf'): 0.4166666667
(1000.0, 'rbf'): 0.4166666667
(10000.0, 'rbf'): 0.4166666667
(100000.0, 'rbf'): 0.4166666667
(1000000.0, 'rbf'): 0.4166666667
(10000000.0, 'rbf'): 0.4166666667
(100000000.0, 'rbf'): 0.4166666667
(1000000000.0, 'rbf'): 0.4166666667
(10000000000.0, 'rbf'): 0.4166666667
(0.01, 'poly'): 0.4166666667
(0.1, 'poly'): 0.4166666667
(1.0, 'poly'): 0.4166666667
.
.
.

【讨论】:

  • 这完全符合我的预期,谢谢。但是现在我无法忽视我的最佳参数与列表不匹配的事实。 search_svm.best_params_ 选择 (C=1.0, 'rbf') 并使用给定的代码显然是最好的 (C=10000.0, 'rbf')
  • 可能是使用'mean_test_score'以外的其他指标获取的最佳参数。
  • 它正在使用'mean_test_score'。主要问题显示了一个 SCV 模型,我也在使用其他模型(logistic reg 和 DTC),你的答案非常有效,只是不知道为什么不在这个 SVC 上。
  • 他们的分数一样吗?你能发布这两个值的分数吗?
  • DTC: print(search_decsT.best_score_) 显示 0.8591172612157564 print(search_decsT.best_params_) 显示 {'decsT__criterion': 'gini', 'decsT__max_depth': 8} 针对这种情况调整您的答案:(7, 'gini'): 0.8576808259 (8, 'gini'): 0.8591172612 (9, 'gini'): 0.8570654413 . . . 绝对匹配,与逻辑规则相同。这是 SVC 0.9004240532229588 的最佳分数,它实际上与您的答案中的 (10000.0, 'rbf'): 0.9004240532 匹配,而不是 C=1。
猜你喜欢
  • 2019-04-17
  • 1970-01-01
  • 2017-04-20
  • 2020-12-11
  • 2018-11-26
  • 2021-06-26
  • 2021-01-16
  • 2019-01-31
  • 1970-01-01
相关资源
最近更新 更多