python - 使用 Sklearn 预测的线性回归不起作用。数据不正确

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

我正在尝试对以下数据执行线性回归。

X = [[ 1 26]
 [ 2 26]
 [ 3 26]
 [ 4 26]
 [ 5 26]
 [ 6 26]
 [ 7 26]
 [ 8 26]
 [ 9 26]
 [10 26]
 [11 26]
 [12 26]
 [13 26]
 [14 26]
 [15 26]
 [16 26]
 [17 26]
 [18 26]
 [19 26]
 [20 26]
 [21 26]
 [22 26]
 [23 26]
 [24 26]
 [25 26]
 [26 26]
 [27 26]
 [28 26]
 [29 26]
 [30 26]
 [31 26]
 [32 26]
 [33 26]
 [34 26]
 [35 26]
 [36 26]
 [37 26]
 [38 26]
 [39 26]
 [40 26]
 [41 26]
 [42 26]
 [43 26]
 [44 26]
 [45 26]
 [46 26]
 [47 26]
 [48 26]
 [49 26]
 [50 26]
 [51 26]
 [52 26]
 [53 26]
 [54 26]
 [55 26]
 [56 26]
 [57 26]
 [58 26]
 [59 26]
 [60 26]
 [61 26]
 [62 26]
 [63 26]
 [64 26]
 [65 26]
 [66 26]
 [67 26]
 [68 26]
 [69 26]]

Y = [  192770 14817993  1393537   437541   514014   412468   509393   172715
   329806   425876   404031   524371   362817   692020   585431   446286
   744061   458805   330027   495654   459060   734793   701697   663319
   750496   525311  1045502   250641   500360   507594   456444   478666
   431382   495689   458200   349161   538770   355879   535924   549858
   611428   517146   239513   354071   342354   698360   467248   500903
   625170   404462  1057368   564703   700988  1352634   727453   782708
   1023673  1046348  1175588   698072   605187   684739   884551  1067267
   728643   790098   580151   340890   299185]

我正在尝试绘制结果以查看回归线

regr = linear_model.LinearRegression()

regr.fit(X, Y)

plt.scatter(X[:,0], Y,  color='black')
plt.plot(X[:,0], regr.predict(X), color='blue',
     linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show()

我得到的图表是 enter image description here

('系数:\n', array([-34296.90306122, 0. ])) 残差平方和:1414631501323.43 方差得分:-17.94

我正在尝试预测

pred = regr.predict([[49, 26]])

print pred

训练数据中已经存在的东西,结果是 [-19155.16326531]

实际值为625170

我做错了什么?

请注意,26 的值来自较大的数组,我已将该数据切成一小部分,以便在 26 上进行训练和预测,类似地,X[:,0] 可能不是连续值,它会再次出现来自更大的数组。 我所说的数组是指 numpy 数组

最佳答案

正如 SAMO 在评论中所说,目前尚不清楚您的数据结构是什么。假设 X 和目标 Y 有两个特征,如果将 X 和 Y 转换为 numpy 数组,则代码将按预期工作。

import numpy as np
from sklearn import linear_model
import matplotlib.pyplot as plt

x1 = range(1, 70)
x2 = [26]*69

X = np.column_stack([x1, x2])

y = '''  192770 14817993  1393537   437541   514014   412468   509393   172715
   329806   425876   404031   524371   362817   692020   585431   446286
   744061   458805   330027   495654   459060   734793   701697   663319
   750496   525311  1045502   250641   500360   507594   456444   478666
   431382   495689   458200   349161   538770   355879   535924   549858
   611428   517146   239513   354071   342354   698360   467248   500903
   625170   404462  1057368   564703   700988  1352634   727453   782708
   1023673  1046348  1175588   698072   605187   684739   884551  1067267
   728643   790098   580151   340890   299185'''

Y = np.array(map(int, y.split()))
regr = linear_model.LinearRegression()

regr.fit(X, Y)

plt.scatter(X[:,0], Y,  color='black')
plt.plot(X[:,0], regr.predict(X), color='blue',
     linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show()

print regr.predict([[49,26]])
# 611830.33589088

关于python - 使用 Sklearn 预测的线性回归不起作用。数据不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38641342/

相关文章:

python - 将数据拆分为训练/测试文件,以便为这两个文件至少选取一个样本

tree - 如何解读sklearn决策树tree_中的children_left属性

matlab - Matlab 中的决策树

python - 微软图形API : Limiting MSAL Python Daemon app to individual user access

python - 无法使用 Django 和 Python 在 URL 查询字符串中传递值

python - nltk pos 标记器看起来包含 '.'

python - ValueError : Expected n_neighbors <= 1. Got 5 -Scikit K 最近分类器

machine-learning - 如何使用 Keras 将 3D 矩阵简化为 2D 矩阵?

python - 如何存储 TfidfVectorizer 以备将来在 scikit-learn 中使用?

python - pip install django-toolbelt 出现错误 :"codecs.ascii_decode(input, self.errors)[0] UnicodeDecodeError: ' ascii' 编解码器无法解码字节 0xc2