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])))
你知道为什么会这样吗?
提前致谢!
Edit:
这是我正在运行并产生错误的代码
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
仅当第二个参数的一个元素为reshape不是整数,例如:
>>> x = np.ones((6, 10, 300))
>>> np.reshape(x, (np.array(x.shape[0], dtype=float), x.shape[1]*x.shape[2]))
TypeError: only integer scalar arrays can be converted to a scalar index
或者如果它是一个array(鉴于编辑历史记录:这就是您的案例中发生的情况):
>>> np.reshape(x, (x.shape[0], x.shape[1]*x[2]))
# forgot to access the shape------^^^^
TypeError: only integer scalar arrays can be converted to a scalar index