关于重塑 numpy 数组
About reshaping numpy array
trainX.size == 43120000
trainX = trainX.reshape([-1, 28, 28, 1])
(1)reshape 是否接受列表而不是元组作为参数?
(2)下面两个语句是否等价?
trainX = trainX.reshape([-1, 28, 28, 1])
trainX = trainX.reshape((55000, 28, 28, 1))
来自 numpy 文档:
newshape : int or tuple of ints
The new shape should be compatible with the original shape. If an
integer, then the result will be a 1-D array of that length. One shape
dimension can be -1. In this case, the value is inferred from the
length of the array and remaining dimensions.
所以是的,-1
对于一维来说很好,你的两个陈述是等价的。关于元组要求,
>>> import numpy as np
>>> a = np.arange(9)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> a.reshape([3,3])
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>>
显然列表也不错。
尝试变化:
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
函数中必须在列表或元组中,以将它们与第一个数组参数分开
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
。 reshape 的总大小是固定的,因此可以从其他值中推导出一个值。
In [5]: np.arange(12).reshape(-1,4)
Out[5]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
方法文档有这样的注释:
Unlike the free function numpy.reshape
, this method on ndarray
allows
the elements of the shape parameter to be passed in as separate arguments.
For example, a.reshape(10, 11)
is equivalent to
a.reshape((10, 11))
.
它是一个内置函数,但签名看起来像 x.reshape(*shape)
,只要值有意义,它就会尽量灵活。
trainX.size == 43120000
trainX = trainX.reshape([-1, 28, 28, 1])
(1)reshape 是否接受列表而不是元组作为参数?
(2)下面两个语句是否等价?
trainX = trainX.reshape([-1, 28, 28, 1])
trainX = trainX.reshape((55000, 28, 28, 1))
来自 numpy 文档:
newshape : int or tuple of ints
The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions.
所以是的,-1
对于一维来说很好,你的两个陈述是等价的。关于元组要求,
>>> import numpy as np
>>> a = np.arange(9)
>>> a
array([0, 1, 2, 3, 4, 5, 6, 7, 8])
>>> a.reshape([3,3])
array([[0, 1, 2],
[3, 4, 5],
[6, 7, 8]])
>>>
显然列表也不错。
尝试变化:
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
函数中必须在列表或元组中,以将它们与第一个数组参数分开
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
。 reshape 的总大小是固定的,因此可以从其他值中推导出一个值。
In [5]: np.arange(12).reshape(-1,4)
Out[5]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 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)
,只要值有意义,它就会尽量灵活。