StandardScaler with Pipelines and GridSearchCV

自闭症网瘾萝莉.ら 提交于 2019-12-22 09:03:41

问题


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)

The Results:

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])

Thanks in advance.


回答1:


@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]

This prints

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],


来源:https://stackoverflow.com/questions/53252156/standardscaler-with-pipelines-and-gridsearchcv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!