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. 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!!
The problem is we shouldn't normalize y, only normalize X and it will work.