訓練前に訓練セットとテストセットを標準化し、訓練が終わった後、標準化されていないデータを抽出して予測した結果、問題が発生した.


元のデータセットのtargetはすべて正の値ですが、次のコードで作成された予測は負の値です.
from sklearn.datasets import load_boston
boston=load_boston()
x=boston.data
y=boston.target
from sklearn.model_selection import train_test_split
xtrain,xtest,ytrain,ytest=train_test_split(x,y,test_size=0.2,random_state=35)
from sklearn.preprocessing import StandardScaler
xs=StandardScaler()
ys=StandardScaler()
xtrain=xs.fit_transform(xtrain)
ytrain=ys.fit_transform(ytrain)
ytest=ys.fit_transform(ytest)
xtest=xs.fit_transform(xtest)
from sklearn.linear_model import LinearRegression
lr=LinearRegression()
lr.fit(xtrain,ytrain)
ypredict=lr.predict(xtest)
xwp=boston.data[0]
lr.predict(xwp)

 
モデル訓練の前にデータを正規化したので,予測する際に訓練したパラメータを逆調整して一化する前に回帰する必要がある.ここでは訓練セットのデータ相手差および平均値を用いて推定することができる.次のコードを試してみてください.
from sklearn.datasets import load_boston
boston=load_boston()
x=boston.data
y=boston.target
from sklearn.model_selection import train_test_split
xtrain,xtest,ytrain,ytest=train_test_split(x,y,test_size=0.2,random_state=35)
from sklearn.preprocessing import StandardScaler
xs=StandardScaler()
ys=StandardScaler()
xtrain_norm=xs.fit_transform(xtrain)
ytrain_norm=ys.fit_transform(ytrain)
ytest_norm=ys.fit_transform(ytest)
xtest_norm=xs.fit_transform(xtest)
from sklearn.linear_model import LinearRegression
lr=LinearRegression()
lr.fit(xtrain_norm,ytrain_norm)


xtrain_std  = np.std(xtrain, axis=0)
xtrain_mean = np.mean(xtrain, axis=0)
ytrain_std  = np.std(ytrain, axis=0)
ytrain_mean = np.mean(ytrain, axis=0)

calculate prediction


xwp=boston.data[0] prediction = ytrain_mean + ytrain_std*(np.dot((xwp - xtrain_mean)/xtrain_std, lr.coef_)) print prediction
 
 
またsklearnの線形モデル自体は標準化のオプションを提供しているので、直接次の方法で標準化のステップを避けることができます.
from sklearn.datasets import load_boston
boston=load_boston()
x=boston.data
y=boston.target
from sklearn.model_selection import train_test_split
xtrain,xtest,ytrain,ytest=train_test_split(x,y,test_size=0.2,random_state=35)
from sklearn.linear_model import LinearRegression
lr=LinearRegression(normalize=True)
lr.fit(xtrain, ytrain)

calculate prediction


xwp=boston.data[0] prediction = lr.predict(xwp) print prediction