简体   繁体   English

线性回归:标准化数据时的预测问题

[英]Linear regression: Prediction issue when data is standardized

I'm working on auto-mpg dataset and I try to predict some values but I've faced this problem: before using linear_regression function of sickit learn, I standardized my data using preprocessing.scale But after that when I try to predict a value but it is always false, however, if I don't standardize data it gives an exact result. 我正在处理自动mpg数据集,但我尝试预测一些值,但是我遇到了这个问题:在使用感冒学习的linear_regression函数之前,我使用preprocessing.scale标准化了数据,但是在此之后,当我尝试预测值时但是,它始终是错误的,但是,如果我不对数据进行标准化,它将得出准确的结果。 Here is my code`enter code here: 这是我的代码在这里输入代码:

import pandas as pd
import numpy as np
import statsmodels.api as sm
from sklearn import linear_model

df = pd.read_csv("http://archive.ics.uci.edu/ml/machine-learning-    databases/auto-mpg/auto-mpg.data-original",
               delim_whitespace = True, header=None,
               names = ['mpg', 'cylinders', 'displacement', 'horsepower',    'weight', 'acceleration',
                        'model_year', 'origin', 'car_name'])
df.dropna(inplace=True)
params=['cylinders', 'displacement', 'horsepower', 'weight', 'acceleration','model_year']
pred=['mpg']
X=df[params]
y=df[pred]

X_scaled=preprocessing.scale(X)
y_scaled=preprocessing.scale(y)
regr = linear_model.LinearRegression(fit_intercept=True)
regr.fit(X_scaled,y_scaled)
y_hat=regr.predict(X_scaled) 
Nouveau_X=np.array([6,225,100,3233,15.4,76]).reshape(1,-1)
print Nouveau_X
Nouveau_X=(Nouveau_X-np.mean(Nouveau_X))/(np.var(Nouveau_X)**0.5)
print Nouveau_X
print "la prediction de la consommation pour ce nouveau vecteur X est ",       regr.predict(Nouveau_X)
#should be mainly equal to 22 but found -1.8 !!!

Plz help!! 请帮助!!

问题是我们不应该对y进行归一化,而仅对X进行归一化就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM