numpy.ma.MaskedArray.shape#

屬性

property ma.MaskedArray.shape#

陣列維度的元組。

shape 屬性通常用於取得陣列的目前形狀,但也可用於透過指定陣列維度的元組來就地重塑陣列的形狀。如同 numpy.reshape,新的形狀維度之一可以是 -1,在這種情況下,其值會從陣列的大小和剩餘維度推斷出來。如果需要複製,就地重塑陣列形狀將會失敗。

警告

不建議設定 arr.shape,未來可能會棄用。建議使用 ndarray.reshape

另請參閱

numpy.shape

等效的 getter 函數。

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: 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 in-place modification. Use
`.reshape()` to make a copy with the desired shape.