0
我試圖重新計算grid.best_score_
我在我自己的數據上獲得沒有成功... 所以我嘗試使用傳統數據集,但沒有更多的成功。下面是代碼:嘗試grid.best_score_(使用GridSearchCV獲得)的自定義計算
from sklearn import datasets
from sklearn import linear_model
from sklearn.cross_validation import ShuffleSplit
from sklearn import grid_search
from sklearn.metrics import r2_score
import numpy as np
lr = linear_model.LinearRegression()
boston = datasets.load_boston()
target = boston.target
param_grid = {'fit_intercept':[False]}
cv = ShuffleSplit(target.size, n_iter=5, test_size=0.30, random_state=0)
grid = grid_search.GridSearchCV(lr, param_grid, cv=cv)
grid.fit(boston.data, target)
# got cv score computed by gridSearchCV :
print grid.best_score_
0.677708680059
# now try a custom computation of cv score
cv_scores = []
for (train, test) in cv:
y_true = target[test]
y_pred = grid.best_estimator_.predict(boston.data[test,:])
cv_scores.append(r2_score(y_true, y_pred))
print np.mean(cv_scores)
0.703865991851
我不明白爲什麼它的不同,GridSearchCV
應該使用的得分手,從線性迴歸,這是R2的分數。也許我的代碼cv
得分不是用來計算best_score_
的......我在這裏通過GridSearchCV代碼來問這裏。