【发布时间】:2016-05-23 00:44:41
【问题描述】:
我有一个包含 56 个图像的数组,每个图像都有 2 个像素通道。因此它的形状是 (1200, 800, 52, 2)。我需要做一个 KNeighborsClassifier,它需要被展平,以便所有 52 个图像中的所有像素都在一列中。所以形状(1200*800*52,2)。然后在执行分类之后 - 我需要知道我可以以正确的顺序对它们进行变形。
作为第一步,我试图对同一个数组进行变形和重塑,并尝试使其与原始数组相同。
这是我尝试过但似乎不起作用的方法:
In [55]: Y.shape
Out[55]: (1200, 800, 2, 52)
In [56]: k = np.reshape(Y,(1200*800*52,2))
In [57]: k.shape
Out[57]: (49920000, 2)
In [58]: l = np.reshape(k,(1200,800,52,2))
In [59]: l.shape
Out[59]: (1200, 800, 52, 2)
In [60]: assert l == Y
/Users/alex/anaconda2/bin/ipython:1: DeprecationWarning: elementwise == comparison failed; this will raise an error in the future.
#!/bin/bash /Users/alex/anaconda2/bin/python.app
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-60-9faf5e8e20ba> in <module>()
编辑:我在 k 和 Y 的形状中犯了一个错误。这是更正的版本,但仍然有错误
In [78]: Y.shape
Out[78]: (1200, 800, 2, 52)
In [79]: k = np.reshape(Y,(1200*800*52,2))
In [80]: k.shape
Out[80]: (49920000, 2)
In [81]: l = np.reshape(k,(1200,800,2,52))
In [82]: assert Y == l
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-82-6f6815930213> in <module>()
----> 1 assert Y == l
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
【问题讨论】:
-
Y的形状为(1200, 800, 2, 52),但l的形状为(1200, 800, 52, 2),两者不相等,因为形状不匹配。 -
哎呀。我已经更新了问题和代码。发生不同的错误,但重塑似乎仍然不起作用