交叉验证和模型选择

Jea*_*nne 4 python numpy machine-learning scikit-learn cross-validation

我正在使用skilearn进行SVM培训.我正在使用交叉验证来评估估算器并避免过度拟合模型.

我将数据分成两部分.训练数据和测试数据.这是代码:

import numpy as np
from sklearn import cross_validation
from sklearn import datasets
from sklearn import svm
X_train, X_test, y_train, y_test = cross_validation.train_test_split(
    iris.data, iris.target, test_size=0.4, random_state=0
)
clf = svm.SVC(kernel='linear', C=1)
scores = cross_validation.cross_val_score(clf, X_train, y_train, cv=5)
print scores

# Now I need to evaluate the estimator *clf* on X_test.
clf.score(X_test,y_test)
# here,  I get an error say that the model is not fitted using fit(), but normally,
# in cross_val_score function the model is fitted? What is the problem?
Run Code Online (Sandbox Code Playgroud)

ali*_*i_m 7

cross_val_score基本上是sklearn 交叉验证迭代器的便利包装.您给它一个分类器和您的整个(训练+验证)数据集,它通过将您的数据分成随机训练/验证集,拟合训练集,并在验证集上计算得分,自动执行一轮或多轮交叉验证.有关示例和更多说明,请参阅此处的文档.

clf.score(X_test, y_test)引发异常的原因是因为在估计器cross_val_score副本而不是原始副本上执行拟合(请参阅此处clone(estimator)的源代码中的使用).因此,在函数调用之外保持不变,因此在调用时未正确初始化.clfclf.fit

  • 交叉验证的目的是估计分类器在看不见的示例上的表现.如果您直接在训练集上评估其表现,那么您将倾向于获得不切实际的好成绩.*单独*,交叉验证不会*做任何*,以使您的分类器表现更好.但是,在优化学习策略时,通常会使用交叉验证分数作为性能指标 - 例如,通过调整分类器的元参数(例如在SVM的情况下为`C`和'gamma`) . (2认同)