当我尝试对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
吗? - Divakarnp.reshape(x, (x.shape[0], x.shape[1]*x[2]))
。 - Warren Weckesser