将评分函数从 sklearn.metrics 传递给 GridSearchCV [英] Pass a scoring function from sklearn.metrics to GridSearchCV

查看:61
本文介绍了将评分函数从 sklearn.metrics 传递给 GridSearchCV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GridSearchCV 的文档 指出我可以通过评分功能.

GridSearchCV's documentations states that I can pass a scoring function.

评分:字符串,可调用或无,默认=无

scoring : string, callable or None, default=None

我想使用原生 accuracy_score作为评分函数.

I would like to use a native accuracy_score as a scoring function.

这是我的尝试.导入和一些数据:

So here is my attempt. Imports and some data:

import numpy as np
from sklearn.cross_validation import KFold, cross_val_score
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import accuracy_score
from sklearn import neighbors

X = np.array([[-1, -1], [-2, -1], [-3, -2], [1, 1], [2, 1], [3, 2]])
Y = np.array([0, 1, 0, 0, 0, 1])

现在,当我在没有评分函数的情况下只使用 k 折交叉验证时,一切都按预期进行:

Now when I use just k-fold cross-validation without my scoring function, everything works as intended:

parameters = {
    'n_neighbors': [2, 3, 4],
    'weights':['uniform', 'distance'],
    'p': [1, 2, 3]
}
model = neighbors.KNeighborsClassifier()
k_fold = KFold(len(Y), n_folds=6, shuffle=True, random_state=0)
clf = GridSearchCV(model, parameters, cv=k_fold)  # TODO will change
clf.fit(X, Y)

print clf.best_score_

但是当我将行更改为

clf = GridSearchCV(model, parameters, cv=k_fold, scoring=accuracy_score) # or accuracy_score()

我收到错误:ValueError: 折叠数不能大于样本数 n_folds=10:6. 在我看来这并不代表真正的问题.

I get the error: ValueError: Cannot have number of folds n_folds=10 greater than the number of samples: 6. which in my opinion does not represent the real problem.

在我看来,问题在于accuracy_score 没有遵循文档中写的签名scorer(estimator, X, y)

In my opinion the problem is that accuracy_score does not follow the signature scorer(estimator, X, y), which is written in the documentation

那么我该如何解决这个问题?

So how can I fix this problem?

推荐答案

如果您将 scoring=accuracy_score 更改为 scoring='accuracy' (请参阅文档 以获取您可以通过这种方式按名称使用的完整评分者列表.)

It will work if you change scoring=accuracy_score to scoring='accuracy' (see the documentation for the full list of scorers you can use by name in this way.)

理论上,您应该能够像尝试一样传递自定义评分函数,但我的猜测是您是对的,accuracy_score 没有正确的 API.

In theory, you should be able to pass custom scoring functions like you're trying, but my guess is that you're right and accuracy_score doesn't have the right API.

这篇关于将评分函数从 sklearn.metrics 传递给 GridSearchCV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆