scipy.stats.

trim_mean#

scipy.stats.trim_mean(a, proportiontocut, axis=0)[原始碼]#

傳回修剪指定比例極端值後的陣列平均值

從已排序陣列的每一端移除指定比例的元素,然後計算剩餘元素的平均值。

參數:
aarray_like

輸入陣列。

proportiontocutfloat

要移除的最正和最負元素的比例。當指定的比例未產生整數數量的元素時,要修剪的元素數量會向下捨入。

axisint 或 None,預設值:0

計算修剪平均值的軸。如果為 None,則在展平的陣列上計算。

傳回值:
trim_meanndarray

修剪後陣列的平均值。

另請參閱

trimboth

從陣列的每一端移除一定比例的元素。

tmean

在修剪掉指定限制以外的值後計算平均值。

註解

對於 1-D 陣列 atrim_mean 近似於以下計算

import numpy as np
a = np.sort(a)
m = int(proportiontocut * len(a))
np.mean(a[m: len(a) - m])

範例

>>> import numpy as np
>>> from scipy import stats
>>> x = [1, 2, 3, 5]
>>> stats.trim_mean(x, 0.25)
2.5

當指定的比例未產生整數數量的元素時,要修剪的元素數量會向下捨入。

>>> stats.trim_mean(x, 0.24999) == np.mean(x)
True

使用 axis 來指定執行計算的軸。

>>> x2 = [[1, 2, 3, 5],
...       [10, 20, 30, 50]]
>>> stats.trim_mean(x2, 0.25)
array([ 5.5, 11. , 16.5, 27.5])
>>> stats.trim_mean(x2, 0.25, axis=1)
array([ 2.5, 25. ])