我在 Python 3.8.5 和 scikit-learn 0.24.1 上运行了一个使用 GridSearchCV 的参数网格:
grid_search = GridSearchCV(estimator=xg_clf, scoring=make_scorer(matthews_corrcoef), param_grid=param_grid, n_jobs=args.n_jobs, verbose = 3)
根据文档,
| verbose : int | 控制详细程度:数值越高,输出的信息越多。 | | - >1 : 显示每个折叠和参数候选的计算时间; | - >2 : 也显示分数; | - >3 : 还显示折叠和候选参数的索引,以及计算的开始时间。
设置 verbose = 3
后,应该会打印每次运行的马修斯相关系数(Matthews Correlation Coefficient)。
然而,输出结果是
Fitting 5 folds for each of 480 candidates, totalling 2400 fits[CV 1/5] END colsample_bytree=0.8, gamma=0, learning_rate=0.7, max_depth=3, n_estimators=200, subsample=0.9; total time= 0.2s[CV 2/5] END colsample_bytree=0.8, gamma=0, learning_rate=0.7, max_depth=3, n_estimators=200, subsample=0.9; total time= 0.2s[CV 3/5] END colsample_bytree=0.8, gamma=0, learning_rate=0.7, max_depth=3, n_estimators=200, subsample=0.9; total time= 0.2s[CV 4/5] END colsample_bytree=0.8, gamma=0, learning_rate=0.7, max_depth=3, n_estimators=200, subsample=0.9; total time= 0.2s[CV 5/5] END colsample_bytree=0.8, gamma=0, learning_rate=0.7, max_depth=3, n_estimators=200, subsample=0.9; total time= 0.2s[CV 1/5] END colsample_bytree=0.8, gamma=0, learning_rate=0.7, max_depth=3, n_estimators=200, subsample=0.95; total time= 0.2s
为什么 GridSearchCV
没有打印每次运行的马修斯相关系数(MCC)?
可能是因为我使用了一个非标准的评分器?
回答:
我尝试了与你的代码类似的几种不同的 scikit-learn 版本。结果发现,版本 0.24.1 在 verbose=3
时不会输出分数。
以下是使用 scikit-learn 版本 0.22.2.post1 的代码和输出:
clf = XGBClassifier()search = GridSearchCV(estimator=clf, scoring=make_scorer(matthews_corrcoef), param_grid={'max_depth':[3, 4, 5]}, verbose=3)search.fit(X, y)> Fitting 5 folds for each of 3 candidates, totalling 15 fits [CV] max_depth=3 ..................................................... [CV] ......................... max_depth=3, score=0.959, total= 0.2s
以下是使用 scikit-learn 版本 0.24.1 的代码和输出:
clf = XGBClassifier()search = GridSearchCV(estimator=clf, scoring=make_scorer(matthews_corrcoef), param_grid={'max_depth':[3, 4, 5]}, verbose=3)search.fit(X, y)> Fitting 5 folds for each of 3 candidates, totalling 15 fits [CV 1/5] END ....................................max_depth=3; total time= 0.2s
总之,你发现了一个错误。通常,我会建议在 GitHub 上开一个问题,但你会高兴地知道,版本 0.24.2 确实会打印每个折叠的分数。
你可以尝试 pip install scikit-learn --upgrade
或 pip install scikit-learn==0.24.2
来修复这个错误。