使用GridSearchCV与一组多个得分手错误输出

时间:2017-08-21 11:37:49

标签: python machine-learning scikit-learn grid-search

我正在尝试使用GridSearchCV来优化我正在进行的分析,并且我已经读过它supports multiple scoring methods,我在别处找到了这个方法的一个例子(example),但是当我尝试以多种格式运行具有多个评分指标的GridSearchCV,这些格式应该被支持,它会引发错误:

File "/home/graduate/adsherma/miniconda2/envs/testenv/lib/python2.7/site-packages/sklearn/model_selection/_validation.py", line 288, in _score
  score = scorer(estimator, X_test, y_test)
TypeError: 'dict' object is not callable

我的源代码是:

DF = pd.read_pickle("OutPut/from_ntuples/nominal.pkl")
X = DF[RC.FittableParameters]
y = DF['isSignal']

pipe = Pipeline([
    ('reduce_dim', SelectKBest()),
    ('classify', AdaBoostClassifier())
])

BASE_ESTIMATORS = [DecisionTreeClassifier(max_depth=i) for i in range(1, 4)]
N_ESTIMATORS = range(100, 600, 100)

param_grid = [
    {
        'reduce_dim': [PCA()],
        'reduce_dim__n_components': [1,10,20,30,40,50],
        'classify__base_estimator': BASE_ESTIMATORS, 
        'classify__n_estimators': N_ESTIMATORS, 
    } ,
]
scoring = {'Precision': make_scorer(precision_score), 
    'Accuracy': make_scorer(accuracy_score)} #does not work
# scoring = ['accuracy', 'precision'] #Does not work
# scoring = 'accuracy' #works
# scoring = make_scorer(accuracy_score) #works
grid = GridSearchCV(pipe, cv=5, n_jobs=1, 
                    param_grid=param_grid, verbose=1,
                    scoring=scoring)
grid.fit(X, y)

如果我尝试列表或元组,错误将是相同的,但会抱怨列表和元组不可调用。这基本上是从上面的示例链接中复制粘贴的(不能重新链接,因为我显然没有足够的声誉)所以我有点不知所措。

0 个答案:

没有答案