numpy.ma.apply_over_axes#

ma.apply_over_axes(func, a, axes)[原始碼]#

在多個軸上重複應用函式。

func 被呼叫為 res = func(a, axis),其中 axisaxes 的第一個元素。函式呼叫的結果 res 必須具有與 a 相同的維度或少一個維度。如果 resa 少一個維度,則在 axis 之前插入一個維度。然後針對 axes 中的每個軸重複呼叫 func,並以 res 作為第一個引數。

參數:
func函式

此函式必須接受兩個引數,func(a, axis)

aarray_like

輸入陣列。

axesarray_like

應用 func 的軸;元素必須為整數。

回傳值:
apply_over_axisndarray

輸出陣列。維度數量與 a 相同,但形狀可能不同。這取決於 func 是否更改其輸出相對於輸入的形狀。

另請參閱

apply_along_axis

沿給定軸將函式應用於陣列的 1-D 切片。

範例

>>> import numpy as np
>>> a = np.ma.arange(24).reshape(2,3,4)
>>> a[:,0,1] = np.ma.masked
>>> a[:,1,:] = np.ma.masked
>>> a
masked_array(
  data=[[[0, --, 2, 3],
         [--, --, --, --],
         [8, 9, 10, 11]],
        [[12, --, 14, 15],
         [--, --, --, --],
         [20, 21, 22, 23]]],
  mask=[[[False,  True, False, False],
         [ True,  True,  True,  True],
         [False, False, False, False]],
        [[False,  True, False, False],
         [ True,  True,  True,  True],
         [False, False, False, False]]],
  fill_value=999999)
>>> np.ma.apply_over_axes(np.ma.sum, a, [0,2])
masked_array(
  data=[[[46],
         [--],
         [124]]],
  mask=[[[False],
         [ True],
         [False]]],
  fill_value=999999)

ufunc 的元組軸引數是等效的

>>> np.ma.sum(a, axis=(0,2)).reshape((1,-1,1))
masked_array(
  data=[[[46],
         [--],
         [124]]],
  mask=[[[False],
         [ True],
         [False]]],
  fill_value=999999)