python - 如果没有指定评分,则传递的估计器应该有一个 'score' 方法
问题描述
我正在做超参数调整,我写了这段代码(来自本教程 - https://machinelearningmastery.com/grid-search-hyperparameters-deep-learning-models-python-keras/?unapproved=524264&moderation-hash=83f45bd57dd6c1c5e37699b257905830#comment- 524264- )
from sklearn.model_selection import GridSearchCV
# fix random seed for reproducibility
seed = 7
np.random.seed(seed)
# define the grid search parameters
batch_size = [10, 20, 40, 60, 80, 100]
epochs = [10, 50, 100]
param_grid = dict(batch_size=batch_size, epochs=epochs)
grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3)
grid_result = grid.fit(scaled_X, y)
# summarize results
print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
means = grid_result.cv_results_['mean_test_score']
stds = grid_result.cv_results_['std_test_score']
params = grid_result.cv_results_['params']
for mean, stdev, param in zip(means, stds, params):
print("%f (%f) with: %r" % (mean, stdev, param))
这是我得到的错误-
TypeError Traceback (most recent call last)
<ipython-input-39-3821841029c0> in <module>
11 param_grid = dict(batch_size=batch_size, epochs=epochs)
12 grid = GridSearchCV(estimator=model, param_grid=param_grid, n_jobs=-1, cv=3)
---> 13 grid_result = grid.fit(scaled_X, y)
14 # summarize results
15 print("Best: %f using %s" % (grid_result.best_score_, grid_result.best_params_))
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\model_selection\_search.py in fit(self, X, y, groups, **fit_params)
607
608 scorers, self.multimetric_ = _check_multimetric_scoring(
--> 609 self.estimator, scoring=self.scoring)
610
611 if self.multimetric_:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\metrics\scorer.py in _check_multimetric_scoring(estimator, scoring)
340 if callable(scoring) or scoring is None or isinstance(scoring,
341 str):
--> 342 scorers = {"score": check_scoring(estimator, scoring=scoring)}
343 return scorers, False
344 else:
~\AppData\Local\Continuum\anaconda3\lib\site-packages\sklearn\metrics\scorer.py in check_scoring(estimator, scoring, allow_none)
293 "If no scoring is specified, the estimator passed should "
294 "have a 'score' method. The estimator %r does not."
--> 295 % estimator)
296 elif isinstance(scoring, Iterable):
297 raise ValueError("For evaluating multiple scores, use "
TypeError: If no scoring is specified, the estimator passed should have a 'score' method. The estimator <keras.engine.sequential.Sequential object at 0x0000025F8892C248> does not.
我已经尝试过来自-的答案
解决方案
我认为您需要指定要在 GridSearch 中使用的分数类型。因为 GridSearch 在参数网格上最大化分数。例如分类问题,您可以使用 f1 分数、精度、召回分数。如果评分为 None GridSearch 将使用估算器的评分方法。
from sklearn.metrics import make_scorer
from sklearn.metrics import accuracy_score, precision_score, recall_score
from sklearn.model_selection import GridSearchCV
scorers = {
'precision_score': make_scorer(precision_score),
'recall_score': make_scorer(recall_score),
'accuracy_score': make_scorer(accuracy_score)
}
grid_search_cv=GridSearchCV(model,param_grid,verbose=1,cv=3,scoring=scorers,refit="precision_score")
推荐阅读
- javascript - 输入后需要获取coffeescript的数据
- python - Python读取文件然后转换成矩阵
- swift - HKObjectQuery 不会停止
- ios - 不平衡在模态存在时调用警告,然后导航弹出到根完成
- r - 使用dplyr求和并除以重复总数?
- business-catalyst - Adobe Business 催化剂联系表通知电子邮件已更改
- ios - 首先滑动/向下滚动将消失视图
- android - 使用 facebook AdView 查找呈现的横幅的 WebView - 通过反射
- angular - Internet Explorer 正在删除我的 Angular 应用程序的上下文根?有什么办法可以避免?
- asp.net-core - 如何在 ASP Net Core 2.0 MVC Web 应用程序和 Azure AD 之间强制重新身份验证