尽管 X 和 Y 的形状相同,但绘图值错误
plot value error despite same shape of X and Y
我正在从事 ML 项目,我正在尝试使用以下代码绘制 roc_curve
plot_roc_curve(rfc,ord_train_t,y_train)
这里 rfc
是我的模型,ord_train_t
是我的输入训练数据,y_train
是训练数据的标签。输入和输出的形状如下
但我不确定以下错误是从哪里引起的(我的输入和输出在任何地方都没有 200 行)
ValueError: x and y must have same first dimension, but have shapes (200,) and (732, 15)
完整错误如下
----> 1 plot_roc_curve(rfc,ord_train_t,y_train)
C:\Users\abcd~1\AppData\Local\Temp/ipykernel_4180/1232546388.py in plot_roc_curve(fpr, tpr, label)
6 plt.figure(figsize=(8,8))
7 plt.title('ROC Curve')
----> 8 plt.plot(fpr, tpr, linewidth=2, label=label)
9 plt.plot([0, 1], [0, 1], 'k--')
10 plt.axis([-0.005, 1, 0, 1.005])
~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(scalex, scaley, data, *args, **kwargs) 3017 @_copy_docstring_and_deprecators(Axes.plot) 3018 def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
-> 3019 return gca().plot( 3020 *args, scalex=scalex, scaley=scaley, 3021 **({"data": data} if data is not None else {}), **kwargs)
~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs) 1603 """ 1604 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1605 lines = [*self._get_lines(*args, data=data, **kwargs)] 1606 for line in lines: 1607 self.add_line(line)
~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in
__call__(self, data, *args, **kwargs)
313 this += args[0],
314 args = args[1:]
--> 315 yield from self._plot_args(this, kwargs)
316
317 def get_next_color(self):
~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in
_plot_args(self, tup, kwargs, return_kwargs)
499
500 if x.shape[0] != y.shape[0]:
--> 501 raise ValueError(f"x and y must have same first dimension, but "
502 f"have shapes {x.shape} and {y.shape}")
503 if x.ndim > 2 or y.ndim > 2:
ValueError: x and y must have same first dimension, but have shapes (200,) and (732, 15)
根据错误消息,您在 plot_roc_curve
函数中将模型传递给了 plt.plot
。
def plot_roc_curve(fpr, tpr, label):
...
plt.plot(fpr, tpr, linewidth=2, label=label)
...
我正在从事 ML 项目,我正在尝试使用以下代码绘制 roc_curve
plot_roc_curve(rfc,ord_train_t,y_train)
这里 rfc
是我的模型,ord_train_t
是我的输入训练数据,y_train
是训练数据的标签。输入和输出的形状如下
但我不确定以下错误是从哪里引起的(我的输入和输出在任何地方都没有 200 行)
ValueError: x and y must have same first dimension, but have shapes (200,) and (732, 15)
完整错误如下
----> 1 plot_roc_curve(rfc,ord_train_t,y_train)
C:\Users\abcd~1\AppData\Local\Temp/ipykernel_4180/1232546388.py in plot_roc_curve(fpr, tpr, label)
6 plt.figure(figsize=(8,8))
7 plt.title('ROC Curve')
----> 8 plt.plot(fpr, tpr, linewidth=2, label=label)
9 plt.plot([0, 1], [0, 1], 'k--')
10 plt.axis([-0.005, 1, 0, 1.005])
~\Anaconda3\lib\site-packages\matplotlib\pyplot.py in plot(scalex, scaley, data, *args, **kwargs) 3017 @_copy_docstring_and_deprecators(Axes.plot) 3018 def plot(*args, scalex=True, scaley=True, data=None, **kwargs):
-> 3019 return gca().plot( 3020 *args, scalex=scalex, scaley=scaley, 3021 **({"data": data} if data is not None else {}), **kwargs)
~\Anaconda3\lib\site-packages\matplotlib\axes\_axes.py in plot(self, scalex, scaley, data, *args, **kwargs) 1603 """ 1604 kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D)
-> 1605 lines = [*self._get_lines(*args, data=data, **kwargs)] 1606 for line in lines: 1607 self.add_line(line)
~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in
__call__(self, data, *args, **kwargs)
313 this += args[0],
314 args = args[1:]
--> 315 yield from self._plot_args(this, kwargs)
316
317 def get_next_color(self):
~\Anaconda3\lib\site-packages\matplotlib\axes\_base.py in
_plot_args(self, tup, kwargs, return_kwargs)
499
500 if x.shape[0] != y.shape[0]:
--> 501 raise ValueError(f"x and y must have same first dimension, but "
502 f"have shapes {x.shape} and {y.shape}")
503 if x.ndim > 2 or y.ndim > 2:
ValueError: x and y must have same first dimension, but have shapes (200,) and (732, 15)
根据错误消息,您在 plot_roc_curve
函数中将模型传递给了 plt.plot
。
def plot_roc_curve(fpr, tpr, label):
...
plt.plot(fpr, tpr, linewidth=2, label=label)
...