scipy.stats.

trimboth#

scipy.stats.trimboth(a, proportiontocut, axis=0)[source]#

從陣列的兩端切除一定比例的項目。

從傳入陣列的兩端切除傳入比例的項目(例如,當 proportiontocut = 0.1 時,切除最左邊 10% 最右邊 10% 的分數)。修剪的值是最低和最高的。如果比例導致非整數切片索引,則少切一些(即保守地切除 proportiontocut)。

參數:
aarray_like (類陣列)

要修剪的資料。

proportiontocutfloat (浮點數)

要從每一端修剪的總資料集比例(範圍 0-1)。

axisint 或 None,選填

沿著哪個軸修剪資料。預設值為 0。如果為 None,則在整個陣列 a 上計算。

回傳值:
outndarray (N 維陣列)

陣列 a 的修剪版本。修剪後內容的順序未定義。

另請參閱

trim_mean

範例

建立一個包含 10 個值的陣列,並從每一端修剪掉 10% 的值

>>> import numpy as np
>>> from scipy import stats
>>> a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> stats.trimboth(a, 0.1)
array([1, 3, 2, 4, 5, 6, 7, 8])

請注意,輸入陣列的元素是按值修剪的,但輸出陣列不一定已排序。

要修剪的比例會四捨五入到最接近的整數。例如,從包含 10 個值的陣列的每一端修剪 25% 的值將會回傳一個包含 6 個值的陣列

>>> b = np.arange(10)
>>> stats.trimboth(b, 1/4).shape
(6,)

多維陣列可以沿著任何軸或跨整個陣列進行修剪

>>> c = [2, 4, 6, 8, 0, 1, 3, 5, 7, 9]
>>> d = np.array([a, b, c])
>>> stats.trimboth(d, 0.4, axis=0).shape
(1, 10)
>>> stats.trimboth(d, 0.4, axis=1).shape
(3, 2)
>>> stats.trimboth(d, 0.4, axis=None).shape
(6,)