Spurious ValueError using learning_curve on sklearn.svm.SVC(kernel='rbf') classifier
<p>I am getting weird ValueError when using learning_curve on svm.SVC(kernel='rbf') classifier.</p>

<p>I am using:</p>

<pre><code>from sklearn import svm
from sklearn import cross_validation, datasets, preprocessing
clf=svm.SVC(kernel='rbf')
cv=cross_validation.StratifiedKFold(y, n_folds=10)
for enum, (train, test) in enumerate(cv):
      print("Fold {0}, classes in train {1}, \t classes in test {2}".format(enum, set(y[train]), set(y[test])))
train_sizes, train_scores, test_scores = learning_curve(
    clf, X, y, cv=cv, n_jobs=n_jobs, train_sizes=train_sizes)
</code></pre>

<p>I can see, there are both classes in train and test sets.</p>

<pre><code>Fold 0, classes in train set([0, 1]),    classes in test set([0, 1])
Fold 1, classes in train set([0, 1]),    classes in test set([0, 1])
Fold 2, classes in train set([0, 1]),    classes in test set([0, 1])
Fold 3, classes in train set([0, 1]),    classes in test set([0, 1])
Fold 4, classes in train set([0, 1]),    classes in test set([0, 1])
Fold 5, classes in train set([0, 1]),    classes in test set([0, 1])
Fold 6, classes in train set([0, 1]),    classes in test set([0, 1])
Fold 7, classes in train set([0, 1]),    classes in test set([0, 1])
Fold 8, classes in train set([0, 1]),    classes in test set([0, 1])
Fold 9, classes in train set([0, 1]),    classes in test set([0, 1])
</code></pre>

<p>But then I get following error:</p>

<pre><code>ValueError: The number of classes has to be greater than one; got 1
</code></pre>

<p>Could somebody please help in finding a workaround?
Thanks!</p>

<p>This looks likely to be caused by the <code>learning_curve</code> which retrains the model on different sized subsamples of your data; by default the sample sizes are <code>train_sizes=array([ 0.1, 0.33, 0.55, 0.78, 1. ])</code>, depending on your data you may be able address the issue by leaving out the smaller fractions, for example by setting <code>train_sizes=array([0.55, 0.78, 1. ])</code>, you also should consider reducing the number of folds in your cross-validation.</p>

python machine-learning scikit-learn