Trimmed Mean with Percentage Limit in Python?

前端 未结 3 779
感动是毒
感动是毒 2020-12-17 10:39

I am trying to calculate the trimmed mean, which excludes the outliers, of an array.

I found there is a module called scipy.stats.tmean, but it req

3条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-17 10:54

    Here's a manual implementation using floor from the math library...

    def trimMean(tlist,tperc):
        removeN = int(math.floor(len(tlist) * tperc / 2))
        tlist.sort()
        if removeN > 0: tlist = tlist[removeN:-removeN]
        return reduce(lambda a,b : a+b, tlist) / float(len(tlist))
    

提交回复
热议问题