numpy.ma.MaskedArray.shape#

属性

属性 ma.MaskedArray.shape#

数组维度的元组。

`shape` 属性通常用于获取数组的当前形状,但也可以通过向其赋值一个数组维度元组来就地重塑数组。与 numpy.reshape 一样,其中一个新的形状维度可以是 -1,在这种情况下,其值将从数组的大小和其余维度中推断出来。如果需要复制,就地重塑数组将会失败。

警告

不建议设置 arr.shape,将来可能会被弃用。推荐使用 ndarray.reshape

另请参见

numpy.shape

等效的获取函数。

numpy.reshape

类似于设置 shape 的函数。

ndarray.reshape

类似于设置 shape 的方法。

示例

>>> import numpy as np
>>> 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: cannot reshape array of size 24 into shape (3,6)
>>> np.zeros((4,2))[::2].shape = (-1,)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: Incompatible shape for in-place modification. Use
`.reshape()` to make a copy with the desired shape.