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

2024-09-30 02:27:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在研究自动mpg数据集,我试图预测一些值,但我遇到了一个问题:在使用sickit learn的线性回归函数之前,我使用预处理.scale 但在那之后,当我试图预测一个值,但它总是错误的,然而,如果我不标准化数据,它会给出一个准确的结果。 这是我的代码`在此处输入代码:

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 !!!

救命啊!!在


Tags: 数据代码importtruedfmodelasnp

热门问题