【问题标题】:Linear Regression : ValueError: x and y must have same first dimension, but have shapes (10, 1) and (1, 1)线性回归:ValueError:x 和 y 必须具有相同的第一维,但具有形状 (10, 1) 和 (1, 1)
【发布时间】:2020-03-19 08:15:23
【问题描述】:

我还是 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)

非常感谢您的帮助!

【问题讨论】:

    标签: python matplotlib linear-regression


    【解决方案1】:

    您的问题与线性回归无关。它会在您要绘图时出现:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-04-01
      • 2020-05-02
      • 2022-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多