Python报错:(矩阵不匹配)ValueError: c of shape (1L, 300L) not acceptable as a color sequence for x with...

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/zongza/article/details/83040519

具体错误如下

ValueError: c of shape (1L, 300L) not acceptable as a color sequence for x with size 300, y with size 300

#代码
plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)

#追踪
Traceback (most recent call last):
  File "D:/Ng-DL-HW/course2/week1/initialization/zero_init.py", line 118, in <module>
    plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)
  File "D:\Ng-DL-HW\course2\week1\initialization\init_utils.py", line 217, in plot_decision_boundary
    plt.scatter(X[0, :], X[1, :], c=y, cmap=plt.cm.Spectral)
  File "D:\tools\Anaconda2\lib\site-packages\matplotlib\pyplot.py", line 3357, in scatter
    edgecolors=edgecolors, data=data, **kwargs)
  File "D:\tools\Anaconda2\lib\site-packages\matplotlib\__init__.py", line 1710, in inner
    return func(ax, *args, **kwargs)
  File "D:\tools\Anaconda2\lib\site-packages\matplotlib\axes\_axes.py", line 4055, in scatter
    raise ValueError(msg.format(c.shape, x.size, y.size))
ValueError: c of shape (1L, 300L) not acceptable as a color sequence for x with size 300, y with size 300

报错原因

此处train_Y应该reshape成一个一维向量,而不是作为一个多维矩阵送进去

解决

方法1:

plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, train_Y)
#改成
plot_decision_boundary(lambda x: predict_dec(parameters, x.T), train_X, np.squeeze(train_Y))

np.squeeze():

从数组的形状中删除单维条目,即把shape中为1的维度去掉

x = np.array([[[0], [1], [2]]])
 
x.shape
Out[99]: (1, 3, 1)
 
np.squeeze(x).shape
Out[100]: (3,)

猜你喜欢

转载自blog.csdn.net/zongza/article/details/83040519