title: How do I use a TimeSeriesSplit with a GridSearchCV object to tune a model in scikit-learn?
body: <p>I've searched the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.TimeSeriesSplit.html" rel="noreferrer">sklearn docs for  <code>TimeSeriesSplit</code></a> and the <a href="http://scikit-learn.org/stable/modules/cross_validation.html#time-series-split" rel="noreferrer">docs for cross-validation</a> but I haven't been able to find a working example.</p>
  
  <p>I'm using sklearn version 0.19.</p>
  
  <p>This is my setup</p>
  
  <pre><code>import xgboost as xgb
  from sklearn.model_selection import TimeSeriesSplit
  from sklearn.grid_search import GridSearchCV
  import numpy as np
  X = np.array([[4, 5, 6, 1, 0, 2], [3.1, 3.5, 1.0, 2.1, 8.3, 1.1]]).T
  y = np.array([1, 6, 7, 1, 2, 3])
  tscv = TimeSeriesSplit(n_splits=2)
  for train, test in tscv.split(X):
      print(train, test)
  </code></pre>
  
  <p>gives:</p>
  
  <pre><code>[0 1] [2 3]
  [0 1 2 3] [4 5]
  </code></pre>
  
  <p>If I try:</p>
  
  <pre><code>model = xgb.XGBRegressor()
  param_search = {'max_depth' : [3, 5]}
  
  my_cv = TimeSeriesSplit(n_splits=2).split(X)
  gsearch = GridSearchCV(estimator=model, cv=my_cv,
                          param_grid=param_search)
  gsearch.fit(X, y)
  </code></pre>
  
  <p>it gives: <code>TypeError: object of type 'generator' has no len()</code></p>
  
  <p>I get the problem: <code>GridSearchCV</code> is trying to call <code>len(cv)</code> but <code>my_cv</code> is an iterator without length. However, the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html" rel="noreferrer">docs for <code>GridSearchCV</code></a> state I can use a </p>
  
  <blockquote>
    <p>int, cross-validation generator or an iterable, optional</p>
  </blockquote>
  
  <p>I tried using <code>TimeSeriesSplit</code> without the <code>.split(X)</code> but it still didn't work.</p>
  
  <p>I'm sure I'm overlooking something simple, thanks!!</p>