【发布时间】:2018-02-06 05:04:09
【问题描述】:
我在尝试重塑 3D numpy 数组时遇到了一个奇怪的错误。
数组 (x) 的形状为 (6, 10, 300),我想将其重塑为 (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
【问题讨论】:
-
你能举报吗:
type(x)和x.dtype? -
type =
dtype = float64 -
我对上面的代码没有任何问题。你能发一个minimal example来显示这个错误吗?
-
整个情况听起来好像在您告诉我们的内容与您实际运行的内容之间存在一些您没有注意到的关键差异。
-
我可以通过使用有错字的行来重现错误:
np.reshape(x, (x.shape[0], x.shape[1]*x[2]))