私はn_componentsがPCAで使用するための最高の価値を把握するためにグリッドサーチを使用しようとしています:GridSearchCVが機能しませんか?
from sklearn.decomposition import PCA
from sklearn.grid_search import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
pca = PCA()
pipe_lr = Pipeline([('pca', pca),
('regr', LinearRegression())])
param_grid = [{'pca__n_components': range(2, X.shape[1])}]
gs = GridSearchCV(estimator=pipe_lr,
param_grid=param_grid,
cv=3)
gs = gs.fit(X_train, y_train)
print(gs.best_score_)
print(gs.best_params_)
for i in range(2, X.shape[1]):
pca.n_components = i
pipe_lr = pipe_lr.fit(X_train, y_train)
print i, pipe_lr.score(X_test, y_test)
はしかし、私が見ていた結果は非常に奇妙です(私はforループから取得数字が完全にありますグリッド検索からのもの)とは異なる:forループによると
-0.232877626581
{'pca__n_components': 2}
2 0.0989156092429
3 0.258170750388
4 0.26328990417
5 0.263620889601
6 0.315725901097
7 0.315477694958
8 0.330445632512
9 0.328779889242
10 0.323594949214
11 0.322914495543
12 0.324050681182
13 0.334970652728
14 0.334333880177
15 0.335040376094
16 0.330876375034
17 0.335395590901
18 0.335132468578
19 0.331201691511
20 0.337244411372
21 0.337130708041
22 0.333092723232
23 0.340707011134
24 0.344046515328
25 0.337869318771
26 0.332590709621
27 0.345343677247
28 0.344728264973
29 0.343084912122
30 0.340332251028
31 0.34
32 0.340290453979
33 0.340349696151
34 0.337021304382
35 0.327271480372
36 0.334423097757
37 -5.09330041094e+21
38 -5.06403949113e+21
、n_componentsのための最高値は約28でなければなりませんが、これは、私は、グリッド検索から何を得るのにも近接していない
注:私はしなかった列車とテストセットを設定するステップが含まれていますが、私はsklearnのtrain_test_split
を使用しました。