线性回归-输出不正确

时间:2020-02-06 07:34:50

标签: python machine-learning scikit-learn linear-regression

我有两列的数据库[“ A”,“ B”],其中“ A”是输入变量,而“ B”是目标变量。所有值都是整数。

我的代码:

X.shape
>>(2540, 1)

y.shape
>>(2540, 1)

from sklearn.preprocessing import StandardScaler

scaler = StandardScaler()
scaler.fit(X)
X = scaler.transform(X)

import numpy as np
from sklearn.model_selection import train_test_split
np.random.rand(4)
X_train, X_test, y_train, y_test  = train_test_split(X,y,test_size = 0.2)

Sklearn的线性回归

regr = LinearRegression(fit_intercept=True)
regr.fit(X_train, y_train)  

print ('Coefficients: ', regr.coef_)
print ('Intercept: ',regr.intercept_)          
>>Coefficients:  [[43.95569425]]
>>Intercept:  [100.68681298]

我的R2值为0.93

X_train中的最后一条记录为3687,相应的y_train值为212.220001

我将最后一条记录用于预测,例如

regr.predict([[3687]] )
>>array([161825.22279211])

我不知道发生了什么,我只是预测值将在212附近。

但是,预测值为161825

请问原因是什么,谢谢

1 个答案:

答案 0 :(得分:2)

也许您需要在通过回归之前通过定标器传递测试数据。 尝试reg.predict(scaler.transform([3687])