scipy.stats.
trim_mean#
- scipy.stats.trim_mean(a, proportiontocut, axis=0)[源代码]#
返回在修剪掉指定比例的极端值后的数组的平均值
从排序数组的每个末尾删除指定比例的元素,然后计算剩余元素的平均值。
- 参数:
- aarray_like
输入数组。
- proportiontocutfloat
要删除的最正和最负元素的比例。当指定的比例未产生整数数量的元素时,要修剪的元素数量将向下舍入。
- axisint 或 None,默认值:0
计算修剪均值的轴。如果为 None,则在扁平化数组上计算。
- 返回:
- trim_meanndarray
修剪数组的平均值。
注释
对于 1-D 数组 a,
trim_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. ])