python - GridSearchCV 不工作?

标签 python machine-learning scikit-learn

我正在尝试使用网格搜索找出要在 PCA 中使用的 n_components 的最佳值:

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_components 的最佳值应该在 28 左右,但这与我从网格搜索中得到的值相去甚远

注意:我没有包括设置训练集和测试集的步骤,但我使用了 sklearn 中的 train_test_split

最佳答案

GridSearchCV,吐出一个cross_validation 分数。在您的 for 循环中添加一个 cross_validation 可能会给您一个更接近的结果。

此外,您使用的是不同的数据。您提到您使用了 train_test_split。在您的 for 循环中,您获得了 X_test、y_test 的分数。在 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/

相关文章:

python - Django 中的设置在运行服务器时重复运行

python - 'max_pooling2d_3/MaxPool' 的 1 减 2 导致的负维度大小(op : 'MaxPool' ) with input shapes: [? ,1,148,32]

python - Tensorflow 查找具有匹配值的像素

python - 为什么 r2_score 在 train_test_split 和 pipeline cross_val_score 之间有很大不同?

python - 如何在 HTML 文档中查找字符串,忽略空格?

machine-learning - 如何利用字符串特征进行分类?

machine-learning - 分类器是如何分类的?

python - scikits.learn 曲线拟合参数的聚类方法

python - 在 scikit-learn 中使用带有 rbf 内核的 SVM 的递归特征消除的 ValueError

python - 如何使用 Python Click 的多个命令组设置控制台脚本的入口点?