我对此感到非常困惑。我正在尝试对数据集执行线性回归,由于我的数据集需要是 2D 而不是 1D,因此我在程序的早期收到了一个值错误,所以我像这样重塑了数据集的每个部分:
x1_train = np.array(x1_train.reshape(-1, 1))
y_train = np.array(y_train.reshape(-1, 1))
x1_test = np.array(x1_test.reshape(-1, 1))
y_test = np.array(y_test.reshape(-1, 1))
x1_pred = np.array(x1_pred.reshape(-1, 1))
y_pred = np.array(y_pred.reshape(-1, 1))
现在,稍后当我尝试绘制训练数据集时,我得到另一个值错误:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-91-6b2e11318dc3> in <module>
1 # Visualizing our results
2 plt.scatter(x1_train, y_train, color = 'green')
----> 3 plt.plot(x1_train, y_pred, color = 'red')
4 plt.title('Happiness Index Score vs. GDP Per Capita(Training Dataset)')
5 plt.xlabel('GDP Per Capita')
~\anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(scalex, scaley, data, *args, **kwargs)
2759 @docstring.copy(Axes.plot)
2760 def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
-> 2761 return gca().plot(
2762 *args, scalex=scalex, scaley=scaley, **({"data": data} if data
2763 is not None else {}), **kwargs)
~\anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
1645 """
1646 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1647 lines = [*self._get_lines(*args, data=data, **kwargs)]
1648 for line in lines:
1649 self.add_line(line)
~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in __call__(self, *args, **kwargs)
214 this += args[0],
215 args = args[1:]
--> 216 yield from self._plot_args(this, kwargs)
217
218 def get_next_color(self):
~\anaconda3\lib\site-packages\matplotlib\axes\_base.py in _plot_args(self, tup, kwargs)
340
341 if x.shape[0] != y.shape[0]:
--> 342 raise ValueError(f"x and y must have same first dimension, but "
343 f"have shapes {x.shape} and {y.shape}")
344 if x.ndim > 2 or y.ndim > 2:
ValueError: x and y must have same first dimension, but have shapes (104, 1) and (52, 1)
这是产生该错误消息/回溯的代码:
plt.scatter(x1_train, y_train, color = 'green')
plt.plot(x1_train, y_pred, color = 'red')
plt.title('Happiness Index Score vs. GDP Per Capita(Training Dataset)')
plt.xlabel('GDP Per Capita')
plt.ylabel('Happiness Index Score')
plt.show()
任何人都明白这里发生了什么?