GridSearchCV 的文档指出我可以通过评分功能。
评分:字符串,可调用或无,默认=无
我想使用原生的accuracy_score作为评分函数。
所以这是我的尝试。进口和一些数据:
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 折交叉验证时,一切都按预期工作:
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: Cannot have number of folds n_folds=10 greater than the number of samples: 6.
我认为这并不代表真正的问题。
在我看来,问题在于accuracy_score
不遵循scorer(estimator, X, y)
文档中所写的签名
那么我该如何解决这个问题呢?