scikit ShuffleSplit raising pandas "IndexError: index N is out of bounds for axis 0 with size M"
<p>I'm trying to use a scikit's GridSearch to find the best alpha for a Lasso, and one of parameters I want it iterate is the cross validation split. So, I'm doing:</p>

<pre><code># X_train := Pandas Dataframe with no index (auto numbered index) and 62064 rows
# y_train := Pandas 1-column Dataframe with no index (auto numbered index) and 62064 rows

from sklearn import linear_model as lm
from sklearn import cross_validation as cv
from sklearn import grid_search

model = lm.LassoCV(eps=0.001, n_alphas=1000)

params = {"cv": [cv.ShuffleSplit(n=len(X_train), test_size=0.2),
                 cv.ShuffleSplit(n=len(X_train), test_size=0.1)]}

m_model = grid_search.GridSearchCV(model, params)

m_model.fit(X_train, y_train)
</code></pre>

<p>But it raises the exception</p>

<pre><code>---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
&lt;ipython-input-113-f791cb0644c1&gt; in &lt;module&gt;()
     10 m_model = grid_search.GridSearchCV(model, params)
     11 
---&gt; 12 m_model.fit(X_train.as_matrix(), y_train.as_matrix())

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/grid_search.py in fit(self, X, y)
    802 
    803         """
--&gt; 804         return self._fit(X, y, ParameterGrid(self.param_grid))
    805 
    806 

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/grid_search.py in _fit(self, X, y, parameter_iterable)
    551                                     self.fit_params, return_parameters=True,
    552                                     error_score=self.error_score)
--&gt; 553                 for parameters in parameter_iterable
    554                 for train, test in cv)
    555 

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py in __call__(self, iterable)
    798             # was dispatched. In particular this covers the edge
    799             # case of Parallel used with an exhausted iterator.
--&gt; 800             while self.dispatch_one_batch(iterator):
    801                 self._iterating = True
    802             else:

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py in dispatch_one_batch(self, iterator)
    656                 return False
    657             else:
--&gt; 658                 self._dispatch(tasks)
    659                 return True
    660 

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py in _dispatch(self, batch)
    564 
    565         if self._pool is None:
--&gt; 566             job = ImmediateComputeBatch(batch)
    567             self._jobs.append(job)
    568             self.n_dispatched_batches += 1

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py in __init__(self, batch)
    178         # Don't delay the application, to avoid keeping the input
    179         # arguments in memory
--&gt; 180         self.results = batch()
    181 
    182     def get(self):

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py in __call__(self)
     70 
     71     def __call__(self):
---&gt; 72         return [func(*args, **kwargs) for func, args, kwargs in self.items]
     73 
     74     def __len__(self):

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py in &lt;listcomp&gt;(.0)
     70 
     71     def __call__(self):
---&gt; 72         return [func(*args, **kwargs) for func, args, kwargs in self.items]
     73 
     74     def __len__(self):

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/cross_validation.py in _fit_and_score(estimator, X, y, scorer, train, test, verbose, parameters, fit_params, return_train_score, return_parameters, error_score)
   1529             estimator.fit(X_train, **fit_params)
   1530         else:
-&gt; 1531             estimator.fit(X_train, y_train, **fit_params)
   1532 
   1533     except Exception as e:

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/linear_model/coordinate_descent.py in fit(self, X, y)
   1146                 for train, test in folds)
   1147         mse_paths = Parallel(n_jobs=self.n_jobs, verbose=self.verbose,
-&gt; 1148                              backend="threading")(jobs)
   1149         mse_paths = np.reshape(mse_paths, (n_l1_ratio, len(folds), -1))
   1150         mean_mse = np.mean(mse_paths, axis=1)

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py in __call__(self, iterable)
    798             # was dispatched. In particular this covers the edge
    799             # case of Parallel used with an exhausted iterator.
--&gt; 800             while self.dispatch_one_batch(iterator):
    801                 self._iterating = True
    802             else:

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py in dispatch_one_batch(self, iterator)
    656                 return False
    657             else:
--&gt; 658                 self._dispatch(tasks)
    659                 return True
    660 

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py in _dispatch(self, batch)
    564 
    565         if self._pool is None:
--&gt; 566             job = ImmediateComputeBatch(batch)
    567             self._jobs.append(job)
    568             self.n_dispatched_batches += 1

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py in __init__(self, batch)
    178         # Don't delay the application, to avoid keeping the input
    179         # arguments in memory
--&gt; 180         self.results = batch()
    181 
    182     def get(self):

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py in __call__(self)
     70 
     71     def __call__(self):
---&gt; 72         return [func(*args, **kwargs) for func, args, kwargs in self.items]
     73 
     74     def __len__(self):

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/externals/joblib/parallel.py in &lt;listcomp&gt;(.0)
     70 
     71     def __call__(self):
---&gt; 72         return [func(*args, **kwargs) for func, args, kwargs in self.items]
     73 
     74     def __len__(self):

/home/user/Programs/repos/pyenv/versions/3.5.2/envs/work/lib/python3.5/site-packages/sklearn/linear_model/coordinate_descent.py in _path_residuals(X, y, train, test, path, path_params, alphas, l1_ratio, X_order, dtype)
    931         avoid memory copies
    932     """
--&gt; 933     X_train = X[train]
    934     y_train = y[train]
    935     X_test = X[test]

IndexError: index 60527 is out of bounds for axis 0 with size 41376
</code></pre>

<p>I tried to use X_train.as_matrix() but didn't work either, giving the same error.</p>

<p>Strange that I can use it manually:</p>

<pre><code>cv_split = cv.ShuffleSplit(n=len(X_train), test_size=0.2)

for tr, te in cv_split:
    print(X_train.as_matrix()[tr], y_train.as_matrix()[tr])

[[0 0 0 ..., 0 0 1]
 [0 0 0 ..., 0 0 1]
 [0 0 0 ..., 0 0 1]
 ..., 
 [0 0 0 ..., 0 0 1]
 [0 0 0 ..., 0 0 1]
 [0 0 0 ..., 0 0 1]] [2 1 1 ..., 1 4 1]
[[   0    0    0 ...,    0    0    1]
 [1720    0    0 ...,    0    0    1]
 [   0    0    0 ...,    0    0    1]
 ..., 
 [ 773    0    0 ...,    0    0    1]
 [   0    0    0 ...,    0    0    1]
 [ 501    1    0 ...,    0    0    1]] [1 1 1 ..., 1 2 1]
</code></pre>

<p>What am I not seeing here? Am I doing something wrong or is that a scikit bug?</p>

<hr>

<p><strong>Update 1</strong></p>

<p>Just found out that cv parameter is not a cv.ShuffleSplit object. This is counterintuitive for me, since <a href="http://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LassoCV.html#sklearn-linear-model-lassocv" rel="nofollow">the docs says</a></p>

<p><a href="http://i.stack.imgur.com/nHLXC.png" rel="nofollow"><img src="http://i.stack.imgur.com/nHLXC.png" alt="enter image description here"></a></p>

<p>Aren't cross_validation classes "object to be used as a cross-validation generator"?</p>

<p>Thanks!</p>

<p>You shouldn't be varying <code>cv</code> in the cross validation parameters grid, the idea is that you have a fixed cross-validation, and use this to grid search over other parameters, something like this:</p>

<pre><code>m_model = grid_search.GridSearchCV(model, 
                                   {'learning_rate': [0.1, 0.05, 0.02]},
                                   cv = cv.ShuffleSplit(n=len(X_train), test_size=0.2))
</code></pre>

python pandas dataframe scikit-learn cross-validation