使用Sklearn留出一个交叉验证

时间:2015-04-06 18:03:20

标签: python numpy scikit-learn cross-validation

我正在尝试使用交叉验证来使用Sklearn测试我的分类器。

我有3个班级,总共50个样本。

  • 第1类有:5个样本
  • 第2类有:15个样本
  • 第3类有:30个样本

以下按预期运行,大概是进行5倍交叉验证。

result = cross_validation.cross_val_score(classifier, X, y, cv=5)

我正在尝试使用cv = 50折叠进行一次性离开,所以我执行以下操作,

result = cross_validation.cross_val_score(classifier, X, y, cv=50)

然而,令人惊讶的是,它会出现以下错误:

/Library/Python/2.7/site-packages/sklearn/cross_validation.py:413: Warning: The least populated class in y has only 5 members, which is too few. The minimum number of labels for any class cannot be less than n_folds=50.
  % (min_labels, self.n_folds)), Warning)
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/_methods.py:55: RuntimeWarning: Mean of empty slice.
  warnings.warn("Mean of empty slice.", RuntimeWarning)
/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy/core/_methods.py:67: RuntimeWarning: invalid value encountered in double_scalars
  ret = ret.dtype.type(ret / rcount)
Traceback (most recent call last):
  File "b.py", line 96, in <module>
    scores1 = cross_validation.cross_val_score(classifier, X, y, cv=50)
  File "/Library/Python/2.7/site-packages/sklearn/cross_validation.py", line 1151, in cross_val_score
    for train, test in cv)
  File "/Library/Python/2.7/site-packages/sklearn/externals/joblib/parallel.py", line 653, in __call__
    self.dispatch(function, args, kwargs)
  File "/Library/Python/2.7/site-packages/sklearn/externals/joblib/parallel.py", line 400, in dispatch
    job = ImmediateApply(func, args, kwargs)
  File "/Library/Python/2.7/site-packages/sklearn/externals/joblib/parallel.py", line 138, in __init__
    self.results = func(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/sklearn/cross_validation.py", line 1240, in _fit_and_score
    test_score = _score(estimator, X_test, y_test, scorer)
  File "/Library/Python/2.7/site-packages/sklearn/cross_validation.py", line 1296, in _score
    score = scorer(estimator, X_test, y_test)
  File "/Library/Python/2.7/site-packages/sklearn/metrics/scorer.py", line 176, in _passthrough_scorer
    return estimator.score(*args, **kwargs)
  File "/Library/Python/2.7/site-packages/sklearn/base.py", line 291, in score
    return accuracy_score(y, self.predict(X), sample_weight=sample_weight)
  File "/Library/Python/2.7/site-packages/sklearn/neighbors/classification.py", line 147, in predict
    neigh_dist, neigh_ind = self.kneighbors(X)
  File "/Library/Python/2.7/site-packages/sklearn/neighbors/base.py", line 332, in kneighbors
    return_distance=return_distance)
  File "binary_tree.pxi", line 1307, in sklearn.neighbors.kd_tree.BinaryTree.query (sklearn/neighbors/kd_tree.c:10506)
  File "binary_tree.pxi", line 226, in sklearn.neighbors.kd_tree.get_memview_DTYPE_2D (sklearn/neighbors/kd_tree.c:2715)
  File "stringsource", line 247, in View.MemoryView.array_cwrapper (sklearn/neighbors/kd_tree.c:24789)
  File "stringsource", line 147, in View.MemoryView.array.__cinit__ (sklearn/neighbors/kd_tree.c:23664)
ValueError: Invalid shape in axis 0: 0.

另外,另一个奇怪的事情是,当我做cv = 5时,我没有得到任何警告。当我做cv = 50时,我得到了上述警告,这很奇怪。因为我认为当cv变大时,即使它可能在计算上更难,但结果应该更准确。我的推理有什么差距吗?为什么我会收到警告和错误?

如何正确地在这种情况下进行一次性交叉验证?

1 个答案:

答案 0 :(得分:4)

默认情况下,cv = 5进行分类会对5倍交叉验证进行分层。 这意味着它试图保持一个类的样本分数不变。当折叠数与样本数相同时,这可能会导致麻烦。 你在哪个版本? 此错误消息肯定不是很有帮助。

顺便说一下,我建议您使用StratifiedShuffleSplit来表示这么小的数据集。

[edit]:当前版本发出警告,该错误应该是错误:

  

sklearn / cross_validation.py:399:警告:y中填充最少的类只有13个成员,这个成员太少了。任何类的最小标签数不能少于n_folds = 68。     %(min_labels,self.n_folds)),警告)