我正在尝试使用网格搜索找出PCA中n_组件的最佳值:
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循环中得到的数字与从网格搜索中得到的完全不同):
-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.34012312844
32 0.340290453979
33 0.340349696151
34 0.337021304382
35 0.327271480372
36 0.334423097757
37 -5.09330041094e+21
38 -5.06403949113e+21
根据for循环,n_组件的最佳值应该在28左右,但这甚至不接近我从网格搜索中得到的值
注:我没有包括设置列车和测试集的步骤,但我使用了sklearn的
train_test_split
。 最佳答案
GridSearchCV
,显示交叉验证分数。在for循环中添加cross_validation
可能会得到更接近的结果。
而且你用的是不同的数据。你已经提到你使用了train_test_split
。在你的for循环中,你得到了X_测试,y_测试的分数。在GridSearchCV
中,你在X_train,y_train上得到了平均分。测试集中可能有异常值。
我稍微修改了你的代码并将其应用到波士顿数据集。
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
from sklearn.datasets import load_boston
import numpy as np
from sklearn.cross_validation import cross_val_score
boston = load_boston()
X = boston.data
y = boston.target
pca = PCA()
pipe_lr = Pipeline([('pca', pca),
('regr', LinearRegression())])
param_grid = {'pca__n_components': np.arange(2, X.shape[1])}
gs = GridSearchCV(estimator=pipe_lr,
param_grid=param_grid,
cv=3)
gs = gs.fit(X, y)
print(gs.best_score_)
print(gs.best_params_)
all_scores = []
for i in range(2, X.shape[1]):
pca.n_components = i
scores = cross_val_score(pipe_lr,X,y,cv=3)
all_scores.append(np.mean(scores))
print(i,np.mean(scores))
print('Best result:',all_scores.index(max(all_scores)),max(all_scores))
给予:
0.35544286032
{'pca__n_components': 9}
2 -0.419093097857
3 -0.192078129541
4 -0.24988282122
5 -0.0909566048894
6 0.197185975618
7 0.173454370084
8 0.276509863992
9 0.355148081819
10 -17.2280089182
11 -0.291804450954
12 -0.281263153468
Best result: 7 0.355148081819
关于python - GridSearchCV无法正常工作?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38866705/