首页 > 解决方案 > 线性回归 - 不正确的输出

问题描述

我有一个包含两列 ["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

能解释一下是什么原因吗,谢谢

标签: pythonmachine-learningscikit-learnlinear-regression

解决方案


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


推荐阅读