我正在尝试使用以下代码将大小为112943的“数据”截断为形状(1,15000):
data = np.reshape(data, (1, 15000))
然而,这给了我以下错误:
ValueError: cannot reshape array of size 112943 into shape (1,15000)
有什么建议可以解决这个错误吗?
我正在尝试使用以下代码将大小为112943的“数据”截断为形状(1,15000):
data = np.reshape(data, (1, 15000))
ValueError: cannot reshape array of size 112943 into shape (1,15000)
换句话说,如果你只需要前15K个元素,你可以使用基本切片:
In [114]: arr = np.random.randn(112943)
In [115]: truncated_arr = arr[:15000]
In [116]: truncated_arr.shape
Out[116]: (15000,)
In [117]: truncated_arr = truncated_arr[None, :]
In [118]: truncated_arr.shape
Out[118]: (1, 15000)
resize
:>>> import numpy as np
>>>
>>> a = np.arange(17)
>>>
# copy
>>> np.resize(a, (3,3))
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>>
# in-place - only use if you know what you are doing
>>> a.resize((3, 3), refcheck=False)
>>> a
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
refcheck=False
,但这是很危险的。在脚本或模块中,您不需要这样做,也不应该这样做。