NumPy参考 >数组对象 >N维数组(ndarray) >numpy.ndarray.tolist > numpy.ndarray.shape
属性
ndarray.
shape
¶数组维度的元组。
shape属性通常用于获取数组的当前形状,但也可以通过向其分配数组尺寸的元组来就地对数组进行整形。与一样numpy.reshape
,新形状尺寸之一可以为-1,在这种情况下,其值是从数组的大小和其余尺寸推断出来的。如果需要复制,就地重塑阵列将失败。
也可以看看
numpy.reshape
类似功能
ndarray.reshape
相似的方法
例子
>>> x = np.array([1, 2, 3, 4])
>>> x.shape
(4,)
>>> y = np.zeros((2, 3, 4))
>>> y.shape
(2, 3, 4)
>>> y.shape = (3, 8)
>>> y
array([[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.],
[ 0., 0., 0., 0., 0., 0., 0., 0.]])
>>> y.shape = (3, 6)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: total size of new array must be unchanged
>>> np.zeros((4,2))[::2].shape = (-1,)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: incompatible shape for a non-contiguous array