<分区>
我在尝试 reshape 3D numpy 数组时遇到了一个奇怪的错误。
数组 (x) 的形状为 (6, 10, 300),我想将其 reshape 为 (6, 3000)。
我正在使用以下代码:
reshapedArray = np.reshape(x, (x.shape[0], x.shape[1]*x.shape[2]))
我收到的错误是:
TypeError: only integer scalar arrays can be converted to a scalar index
但是,如果我将 x 变成一个列表,它就可以工作了:
x = x.tolist()
reshapedArray = np.reshape(x, (len(x), len(x[0])*len(x[0][0])))
你知道为什么会这样吗?
提前致谢!
编辑:
这是我正在运行并产生错误的代码
x = np.stack(dataframe.as_matrix(columns=['x']).ravel())
print("OUTPUT:")
print(type(x), x.dtype, x.shape)
print("----------")
x = np.reshape(x, (x.shape[0], x.shape[1]*x[2]))
OUTPUT:
<class 'numpy.ndarray'> float64 (6, 10, 300)
----------
TypeError: only integer scalar arrays can be converted to a scalar index