Why GridSearchCV return score so different from the score returned by running model directly?
<p>I used GridSearchCV to find the best alpha for lasso model.</p>

<pre><code>alphas = np.logspace(-5, 2, 30)
grid = GridSearchCV(estimator=Lasso(),
param_grid=dict(alpha=alphas), cv=10, scoring='r2')
grid.fit(self.X, self.Y) # entire datasets were fed here

print grid.best_params_, grid.best_score_ # score -0.0470788758558
for params, mean_score, scores in grid.grid_scores_:
    print mean_score, params
</code></pre>

<p>I got the best parameter as 0.0014873521072935117, with negative r2 score -0.0470788758558.</p>

<hr>

<p>Then I tried this alpha on the model directly. I ran the following code in a loop.</p>

<pre><code>X_train, X_test, y_train, y_test = train_test_split(self.X, self.Y, train_size=0.7)
lasso = Lasso(alpha=0.001487)
lasso.fit(X_train, y_train)
print lasso.score(X_test, y_test)
</code></pre>

<p>Notice that I didn't set the random state, so it should work as a cross-validation. But the score I got here is around 0.11 (0.11-0.12) not matter how many times I ran the code.</p>

<hr>

<p><strong>Question</strong></p>

<p>Why are the scores -0.0470788758558 and 0.11 so different for the two approaches? </p>

<p>I found the reason. </p>

<p>cv should be set like this:</p>

<pre><code>cv = ShuffleSplit(n=len(X), n_iter=10, test_size=.3)
</code></pre>

<p>when cv equals to integer, it means how many folds there are in each iteration not the number of iterations. </p>

python scikit-learn linear-regression grid-search lasso