scipy.ndimage.

median#

scipy.ndimage.median(input, labels=None, index=None)[原始碼]#

計算標記區域上陣列數值的中位數。

參數:
inputarray_like

數值的類陣列 (array-like)。對於 labels 指定的每個區域,計算 input 在該區域上的中位數值。

labelsarray_like,選填

一個整數的類陣列 (array-like),標記要計算 input 中位數值的不同區域。labels 必須與 input 具有相同的形狀。如果未指定 labels,則返回整個陣列的中位數。

indexarray_like,選填

一個區域標籤列表,用於計算中位數。如果 index 為 None,則返回 labels 非零的所有元素的中位數。

返回值:
medianfloat 或 float 列表

input 在由 labels 確定且索引在 index 中的區域上的中位數列表。如果未指定 indexlabels,則返回一個 float:如果 labels 為 None,則返回 input 的中位數值;如果 index 為 None,則返回 labels 大於零的元素的中位數值。

註解

此函數返回一個 Python 列表,而不是 NumPy 陣列,請使用 np.array 將列表轉換為陣列。

範例

>>> from scipy import ndimage
>>> import numpy as np
>>> a = np.array([[1, 2, 0, 1],
...               [5, 3, 0, 4],
...               [0, 0, 0, 7],
...               [9, 3, 0, 0]])
>>> labels, labels_nb = ndimage.label(a)
>>> labels
array([[1, 1, 0, 2],
       [1, 1, 0, 2],
       [0, 0, 0, 2],
       [3, 3, 0, 0]], dtype=int32)
>>> ndimage.median(a, labels=labels, index=np.arange(1, labels_nb + 1))
[2.5, 4.0, 6.0]
>>> ndimage.median(a)
1.0
>>> ndimage.median(a, labels=labels)
3.0