首页 > 解决方案 > 如果没有指定评分,则传递的估计器应该有一个 '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.

我已经尝试过来自-的答案

Scikit-learn TypeError:如果没有指定评分,则传递的估计器应该有一个'score'方法

标签: pythonkerasscikit-learn

解决方案


我认为您需要指定要在 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")

检查 GridSearch 中的评分参数文档

您可以使用内置评分参数定义自己的函数


推荐阅读