I am trying to build multiple linear regression model with 3 different method and I am getting different results for each one. I think that I have to get the same results but Where is this difference come from?
Using GridSearchCV
X_train, X_test, y_train, y_test = cross_validation.train_test_split(data, ground_truth_data,
test_size=0.3,random_state =1 )
model = linear_model.LinearRegression()
parameters = {'fit_intercept':[True,False], 'normalize':[True,False], 'copy_X':[True, False]}
grid = GridSearchCV(model,parameters, cv=None)
grid.fit(X_train, y_train)
print "r2 / variance : ", grid.best_score_
print("Residual sum of squares: %.2f"
% np.mean((grid.predict(X_test) - y_test) ** 2))
The output is:
r2 / variance : 0.823041227357
Residual sum of squares: 0.18
Using Linear Regression without GridSearchCV
X_train, X_test, y_train, y_test = cross_validation.train_test_split(data, ground_truth_data,
test_size=0.3,random_state =1 )
model = linear_model.LinearRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print "r2/variance : ", model.score(X_test,y_test)
print("Residual sum of squares: %.2f"
% np.mean((model.predict(X_test) - y_test) ** 2))
The output is:
r2 / variance : 0.883799174674
Residual sum of squares: 0.18
Using Statsmodel OLS method
X_train, X_test, y_train, y_test = cross_validation.train_test_split(data, ground_truth_data, test_size=0.3,random_state =1 )
x_train = sm.add_constant(X_train)
model = sm.OLS(y_train, x_train)
results = model.fit()
print "r2/variance : ", results.rsquared
The output is :
r2/variance : 0.893686634315
I have confused on three different point.
- Why using GridSearchCV does not increase the r_score and why sum of error is same ?
My guess is GridSearchCV make some cross validation (maybe k-fold) so the r_square score is decrease when we use it. But I am not clear on this issue.
- What is the difference between Scikit and Statsmodel OLS ?
> My guess is Statsmodel OLS looks the training error and Scikit looks the test error. So I think that using Scikit OLS is more rational.
- When and how we can use GridSearchCv on Regression model ?
> I have not to much guess.
Thanks for every idea.