关于重塑numpy数组 [英] About reshaping numpy array
问题描述
trainX.size == 43120000
trainX = trainX.reshape([-1, 28, 28, 1])
(1)重塑是否接受列表作为argment而不是元组?
(1)Does reshape accept a list as an argment instead of a tuple?
(2)以下两个语句是否等效?
(2)Are the following two statements equivalent?
trainX = trainX.reshape([-1, 28, 28, 1])
trainX = trainX.reshape((55000, 28, 28, 1))
推荐答案
尝试各种版本:
In [1]: np.arange(12).reshape(3,4)
Out[1]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [2]: np.arange(12).reshape([3,4])
Out[2]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [3]: np.arange(12).reshape((3,4))
Out[3]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
使用reshape
方法,形状可以是参数,元组或列表. reshape
函数必须位于列表或元组中,以将它们与第一个数组参数分开
With the reshape
method, the shape can be arguments, a tuple or a list. In the reshape
function is has to be in a list or tuple, to separate them from the first array argument
In [4]: np.reshape(np.arange(12), (3,4))
Out[4]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
是的,可以使用一个-1
.整形的总大小是固定的,因此可以从其他值中推导出一个值.
and yes, one -1
can be used. The total size of the reshape is fixed, so one value can be deduced from the others.
In [5]: np.arange(12).reshape(-1,4)
Out[5]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
方法文档中有以下注释:
The method documentation has this note:
与自由功能
numpy.reshape
不同,ndarray
上的此方法允许 要作为单独参数传递的shape参数的元素. 例如,a.reshape(10, 11)
等效于a.reshape((10, 11))
.
Unlike the free function
numpy.reshape
, this method onndarray
allows the elements of the shape parameter to be passed in as separate arguments. For example,a.reshape(10, 11)
is equivalent toa.reshape((10, 11))
.
这是一个内置函数,但签名看起来像x.reshape(*shape)
,只要值有意义,它就会尝试保持灵活性.
It's a builtin function, but the signature looks like x.reshape(*shape)
, and it tries to be flexible as long as the values make sense.
这篇关于关于重塑numpy数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!