TypeError: only length-1 arrays can be converted to Python scalars, while using Kfold cross Validation
<p>I am trying to use <code>Kfold</code> cross valiadtion for my model, but get this error while doing so. I know that <code>KFold</code> only accepts 1D arrays but even after converting the length input to an array its giving me this problem.</p>

<pre><code>from sklearn.ensemble import ExtraTreesClassifier, RandomForestClassifier
from sklearn.cross_validation import train_test_split
from sklearn.cross_validation import KFold

if __name__ == "__main__":
  np.random.seed(1335)

  verbose = True
  shuffle = False
  n_folds = 5

  y = np.array(y)

  if shuffle: 
    idx = np.random.permutation(y.size)
    X_train = X_train[idx]
    y = y[idx]

  skf = KFold(y, n_folds) 

  models = [RandomForestClassifier(n_estimators=100, n_jobs=-1, criterion='gini'),ExtraTreesClassifier(n_estimators=100, n_jobs=-1, criterion='entropy')]

  print("Stacking in progress")
  A = []
  for j, clf in enumerate(models):
     print(j, clf)

     for i, (itrain, itest) in enumerate(skf):
        print("Fold :", i)
        x_train = X_train[itrain]
        x_test = X_train[itest]
        y_train = y[itrain]
        y_test = y[itest]
        print(x_train.shape, x_test.shape)

        print(len(x_train), len(x_test))

        clf.fit(x_train, y_train)
        pred = clf.predict_proba(x_test)
        A.append(pred)
</code></pre>

<p>I get the error for the line "<code>skf = KFold(y, n_folds)</code>". Any help with this will be appreciated.</p>

<p>From <a href="http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.KFold.html#sklearn.model_selection.KFold" rel="nofollow">its doc</a>, <code>KFold()</code> does not expect <code>y</code> as an input, but only the number of splits (n_folds). </p>

<p>Once you have an instance of <code>KFold</code>, you do <code>myKfold.split(x)</code> (<code>x</code> being all of your input data) to obtain an iterator yielding train and test indices. Example copy pasted from sklearn doc:</p>

<pre><code>&gt;&gt;&gt; from sklearn.model_selection import KFold
&gt;&gt;&gt; X = np.array([[1, 2], [3, 4], [1, 2], [3, 4]])
&gt;&gt;&gt; y = np.array([1, 2, 3, 4])
&gt;&gt;&gt; kf = KFold(n_splits=2)
&gt;&gt;&gt; kf.get_n_splits(X)
2
&gt;&gt;&gt; print(kf)  
KFold(n_splits=2, random_state=None, shuffle=False)
&gt;&gt;&gt; for train_index, test_index in kf.split(X):
...    print("TRAIN:", train_index, "TEST:", test_index)
...    X_train, X_test = X[train_index], X[test_index]
...    y_train, y_test = y[train_index], y[test_index]
TRAIN: [2 3] TEST: [0 1]
TRAIN: [0 1] TEST: [2 3]
</code></pre>

python arrays pandas scikit-learn cross-validation