带有管道和GridSearchCV的StandardScaler [英] StandardScaler with Pipelines and GridSearchCV

查看:183
本文介绍了带有管道和GridSearchCV的StandardScaler的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经将standardScaler放在了管道上,并且 CV_mlpregressor.predict(x_test)的结果很奇怪.我认为我必须必须将值从standardScaler带回来,但仍然不知道怎么做.

I've put standardScaler on the pipeline, and the results of CV_mlpregressor.predict(x_test), are weird. I think i must have to bring the values back from the standardScaler, but still can't figure how.

pipe_MLPRegressor = Pipeline([('scaler',  StandardScaler()),
            ('MLPRegressor', MLPRegressor(random_state = 42))])


grid_params_MLPRegressor = [{
    'MLPRegressor__solver': ['lbfgs'],
    'MLPRegressor__max_iter': [100,200,300,500],
    'MLPRegressor__activation' : ['relu','logistic','tanh'],
    'MLPRegressor__hidden_layer_sizes':[(2,), (4,),(2,2),(4,4),(4,2),(10,10),(2,2,2)],
}]


CV_mlpregressor = GridSearchCV (estimator = pipe_MLPRegressor,
                               param_grid = grid_params_MLPRegressor,
                               cv = 5,return_train_score=True, verbose=0)

CV_mlpregressor.fit(x_train, y_train)

CV_mlpregressor.predict(x_test)

结果:

array([ 2.67564153e+04,  1.90010572e+04,  9.62702942e+04,  3.98791931e+04,
        1.48889808e+03,  7.08980726e+03,  3.86311279e+02,  7.05602301e+04,
        4.06858486e+03,  4.29186303e+04,  3.86701735e+03,  6.30228075e+04,
        6.78276925e+04, -5.91956287e+02, -7.37680434e+02,  3.07485001e+04,
        4.81417953e+03,  5.18697686e+03,  1.61221952e+04,  1.33794944e+04,
       -1.48375101e+03,  1.80891807e+04,  1.39740243e+04,  6.57156849e+04,
        3.32962481e+04,  5.71332087e+05,  1.79130092e+03,  5.25642370e+04,
        2.08111172e+04,  4.31060127e+04])

谢谢.

推荐答案

@Lian,我认为您以正确的方式进行了所有操作.请检查您的数据.我对sklearn数据集进行了实验,该实验符合预期.

@Lian, I think you are doing everything in the correct way. Please check your data. I did an experiment with sklearn dataset and this works as expected.

from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPRegressor
from sklearn.pipeline import Pipeline
from sklearn.model_selection import GridSearchCV
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
import numpy as np

x,y = load_boston(return_X_y=True)


xtrain, xtest, ytrain, ytest = train_test_split(x,y, random_state=6784)

pipe_MLPRegressor = Pipeline([('scaler',  StandardScaler()),
            ('MLPRegressor', MLPRegressor(random_state = 42))])
grid_params_MLPRegressor = [{
    'MLPRegressor__solver': ['lbfgs'],
    'MLPRegressor__max_iter': [100,200,300,500],
    'MLPRegressor__activation' : ['relu','logistic','tanh'],
    'MLPRegressor__hidden_layer_sizes':[(2,), (4,),(2,2),(4,4),(4,2),(10,10),(2,
2,2)],}]


CV_mlpregressor = GridSearchCV (estimator = pipe_MLPRegressor,
                               param_grid = grid_params_MLPRegressor,
                               cv = 5,return_train_score=True, verbose=0)

CV_mlpregressor.fit(xtrain, ytrain)

ypred=CV_mlpregressor.predict(xtest)

print np.c_[ytest, ypred]

此打印

array([[ 29.9       ,  30.79749986],
       [ 22.5       ,  24.52180656],
       [ 22.6       ,  18.9567779 ],
       [ 28.7       ,  22.17189123],
       [ 13.8       ,  19.16797811],
       [ 21.2       ,  24.63527335],
       [ 11.3       ,  13.58962076],
       [ 23.        ,  18.33693455],
       [ 12.7       ,  15.52294714],
       [ 23.3       ,  26.65083451],
       [ 25.3       ,  24.04219813],
       [ 22.6       ,  19.81454969],
       [ 36.2       ,  22.16994764],
       [ 17.9       ,  11.1221789 ],
       [ 18.5       ,  17.84162452],
       [ 16.8       ,  22.99832673],
       [ 20.3       ,  20.22598426],
       [ 23.9       ,  26.80997945],
       [ 17.6       ,  16.08188321],
       [ 23.2       ,  18.5995955 ],
       [ 48.3       ,  43.37911488],
       [ 19.1       ,  22.36379857],

这篇关于带有管道和GridSearchCV的StandardScaler的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆