使用 Sklearn 预测的线性回归不起作用。数据不合适

Linear regression using Sklearn prediction not working. data not fit properly

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

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()

我得到的图表是

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

我正在尝试预测

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

print pred

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

实际值为625170

我做错了什么?

请注意,26 的值不是来自更大的数组,我已将该数据切成一小部分以便在 26 上进行训练和预测,同样 X[:,0] 可能不是连续值再次来自更大的阵列。 通过数组我的意思是 numpy array

正如 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

您可能在绘图之前弄乱了输入数组。根据您问题中的信息,回归确实 returns 一个接近您预期答案 625170 的结果。

from sklearn import linear_model

# your input arrays
x = [[a, 26] for a in range(1, 70, 1)]
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]

# your code for regression
regr = linear_model.LinearRegression()
regr.fit(x, y)

# the correct coef is different from your findings
print regr.coef_

这returns个结果:array([-13139.72031421, 0. ])

尝试预测时:regr.predict([[49, 26]]) returns array([ 611830.33589088]),接近您预期的答案。

print(regression.predict(np.array([[60]])))

如果我们想预测单个值(浮点数)来预测代码,那可能行不通。 我一开始尝试了以下代码,但没有成功:

lin_reg.predict(6.5)

找到的解决方案是:

lin_reg.predict([[6.5]])

试试看是否也适合您。