ValueError: Cannot have number of splits n_splits=3 greater than the number of samples: 1
<p>I am trying this training modeling using train_test_split and a decision tree regressor:</p>

<pre><code>import sklearn
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeRegressor
from sklearn.model_selection import cross_val_score

# TODO: Make a copy of the DataFrame, using the 'drop' function to drop the given feature
new_data = samples.drop('Fresh', 1)

# TODO: Split the data into training and testing sets using the given feature as the target
X_train, X_test, y_train, y_test = train_test_split(new_data, samples['Fresh'], test_size=0.25, random_state=0)

# TODO: Create a decision tree regressor and fit it to the training set
regressor = DecisionTreeRegressor(random_state=0)
regressor = regressor.fit(X_train, y_train)

# TODO: Report the score of the prediction using the testing set
score = cross_val_score(regressor, X_test, y_test, cv=3)

print score
</code></pre>

<p>When running this, I am getting the error:</p>

<pre><code>ValueError: Cannot have number of splits n_splits=3 greater than the number of samples: 1.
</code></pre>

<p>If I change the value of cv to 1, I get:</p>

<pre><code>ValueError: k-fold cross-validation requires at least one train/test split by setting n_splits=2 or more, got n_splits=1.
</code></pre>

<p>Some sample rows of the data look like: </p>

<pre><code>    Fresh   Milk    Grocery Frozen  Detergents_Paper    Delicatessen
0   14755   899 1382    1765    56  749
1   1838    6380    2824    1218    1216    295
2   22096   3575    7041    11422   343 2564
</code></pre>

<p>If the number of split is greater than number of sample, you will get the first error. Check the snippet from the <a href="https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/model_selection/_split.py#L315" rel="nofollow">source code</a> given below.</p>

<pre><code>if self.n_splits &gt; n_samples:
    raise ValueError(
        ("Cannot have number of splits n_splits={0} greater"
         " than the number of samples: {1}.").format(self.n_splits,
                                                     n_samples))
</code></pre>

<p>If the number of fold is less than or equal <code>1</code>, you will get the second error. In your case, the <code>cv = 1</code>. Check the <a href="https://github.com/scikit-learn/scikit-learn/blob/master/sklearn/cross_validation.py#L249" rel="nofollow">source code</a>.</p>

<pre><code>if n_folds &lt;= 1:
            raise ValueError(
                "k-fold cross validation requires at least one"
                " train / test split by setting n_folds=2 or more,"
                " got n_folds={0}.".format(n_folds))
</code></pre>

<p>An educated guess, the number of sample in <code>X_test</code> is less than <code>3</code>. Check that carefully.</p>

python scikit-learn cross-validation sklearn-pandas