我正在使用Scikit-learn。我尝试使用普通交叉验证程序和快速cross_validation.cross_val_score
使用交叉验证。但我发现我得到了不同的数字。为什么?
import numpy as np
from sklearn import cross_validation, datasets
digits = datasets.load_digits()
X = digits.data
y = digits.target
svc = svm.SVC(kernel='linear')
kfold = cross_validation.KFold(len(X))
scores = [svc.fit(X[train], y[train]).score(X[test], y[test]) for train, test in kfold]
#scores output: [0.93489148580968284, 0.95659432387312182, 0.93989983305509184]
cross_validation.cross_val_score(svc, X, y)
#output: array([ 0.98 , 0.982, 0.983])
正如 cross_val_score
的文档字符串会告诉您的那样,在给出类标签(整数)的目标向量时,它会进行分层交叉验证。
>>> kfold = cross_validation.StratifiedKFold(y)
>>> [svc.fit(X[train], y[train]).score(X[test], y[test])
... for train, test in kfold]
[0.93521594684385378, 0.95826377295492482, 0.93791946308724827]
>>> cross_validation.cross_val_score(svc, X, y)
array([ 0.93521595, 0.95826377, 0.93791946])