scipy.stats.

trimboth#

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

从数组两端切掉一定比例的元素。

从传入数组的两端切掉指定比例的元素(例如,当 proportiontocut = 0.1 时,切掉最左边的 10% **和** 最右边的 10% 的分数)。被修剪的值是最低和最高的值。如果 proportion 导致非整数切片索引,则切掉更少(即保守地切掉 proportiontocut)。

参数:
aarray_like

要修剪的数据。

proportiontocutfloat

每个端点要修剪的总数据集的比例(范围为 0-1)。

axisint 或 None,可选

沿其修剪数据的轴。默认为 0。如果为 None,则计算整个数组 a

返回值:
outndarray

数组 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,)