繁体   English   中英

线性回归:ValueError:x 和 y 必须具有相同的第一维,但具有形状 (10, 1) 和 (1, 1)

[英]Linear Regression : ValueError: x and y must have same first dimension, but have shapes (10, 1) and (1, 1)

我还是 pyhton 的新手,我尝试显示线性回归图,但出现了一些错误

这是代码:

 linear_regression = lm.LinearRegression()
 linear_x = data.Luas.values.reshape(-1,1)
 linear_y = data.Harga.values.reshape(-1,1)
 linear_regression.fit(linear_x,linear_y)
 print("Intercept = ",linear_regression.intercept_)
 print("Coefisien = ",linear_regression.coef_)
 print("Persamaan menggunakan fungsi Linear Regression :")
 print("Y=",linear_regression.intercept_,"+",linear_regression.coef_,"X")
 print("Prediksi Luas Tanah (X) = 1800")
 print("Maka:")
 result = linear_regression.predict([[1800]])
 print("Harga Tanah(Y) = ",result,"jt")


 plt.scatter(linear_x,linear_y,color='black')
 plt.plot(linear_x,linear_regression.predict([[1800]]),color='blue')
 plt.title('Luas Tanah/Area VS Harga/Price')
 plt.ylabel('Harga Tanah/Price (jt)')
 plt.xlabel('Luas Tanah/Area')
 plt.show()

错误 :

Traceback (most recent call last):
 File "D:/Tugas/smt6/data mining/tugasKlasifikasi/klasifikasi.py", line 55, in <module>
  plt.plot(linear_x,linear_regression.predict([[1800]]),color='blue')
 File "C:\Users\Thor\Anaconda3\envs\coba\lib\site-packages\matplotlib\pyplot.py", line 2796, in 
  plot is not None else {}), **kwargs)
 File "C:\Users\Thor\Anaconda3\envs\coba\lib\site-packages\matplotlib\axes\_axes.py", line 1665, 
  in plot lines = [*self._get_lines(*args, data=data, **kwargs)]
 File "C:\Users\Thor\Anaconda3\envs\coba\lib\site-packages\matplotlib\axes\_base.py", line 225, 
  in __call__yield from self._plot_args(this, kwargs)
 File "C:\Users\Thor\Anaconda3\envs\coba\lib\site-packages\matplotlib\axes\_base.py", line 391, 
  in _plot_args x, y = self._xy_from_xy(x, y)
 File "C:\Users\Thor\Anaconda3\envs\coba\lib\site-packages\matplotlib\axes\_base.py", line 270, 
  in _xy_from_xy "have shapes {} and {}".format(x.shape, y.shape))
 ValueError: x and y must have same first dimension, but have shapes (10, 1) and (1, 1)

非常感谢您的帮助!

您的问题与线性回归无关。 当你想绘制时它会出现:

plt.plot(linear_x,linear_regression.predict([[1800]]),color='blue')

问题是linear_x形状为(10, 1)而您的prediction形状为(1, 1) 所以你不能那样做。 它们必须提供相同的第一个形状。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM