python - 线性回归 - 不正确的输出
问题描述
我有一个包含两列 ["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
能解释一下是什么原因吗,谢谢
解决方案
也许您需要在输入回归之前通过缩放器传递您的测试数据。尝试reg.predict(scaler.transform([3687])
推荐阅读
- r - 将一个数据框的列匹配到另一个数据框
- java - 使用Java中的Hoare Partitioning对数组进行排序的快速排序算法
- python - 如何处理python中的重音字符?
- python - 即使在收到完整请求之前,我如何才能知道 Flask 请求何时开始?
- arduino-ide - arduino 中是否有可以从键盘读取以执行功能的命令?
- reactjs - 无法删除 React 中的列表项
- jersey - 无法在我的资源类中注入 @Service 和 @Contract 依赖项
- php - 并行多线程php
- assembly - ARM 错误消息未对齐的分支目标
- spring - 在 Spring Boot 中运行 r2dbc 获取 H2 时出错