我试图使用交叉验证来使用Sklearn测试我的分类器。
我有三个班,总共50个样本。
一级有: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变大时,尽管计算起来可能会更困难,但结果应该更准确。我的推理有什么分歧吗?为什么我会得到警告和错误?
在这种情况下,我怎样才能正确地排除交叉验证?

最佳答案

默认情况下,用于分类的cv=5进行分层5倍交叉验证。
这意味着它试图保持一个类中样本的分数不变。当折叠次数与样本数目相同时,这可能会导致问题。
你在哪个版本?
这条错误信息肯定没有多大帮助。
顺便说一下,一般来说,我建议您对这样一个小的数据集使用StratifiedShuffleSplit
[编辑]:当前版本发出警告,这可能是一个错误:
sklearn/cross_validation.py:399:警告:y中填充最少的类只有13个成员,这太少了。任何类别的最小标签数不能小于n_folds=68。
%(最小标签,自我折叠),警告)

关于python - 使用Sklearn进行一次交叉验证,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29476807/

10-12 20:24