Is there a numpy builtin to reject outliers from a list

后端 未结 10 795
孤城傲影
孤城傲影 2020-11-28 18:00

Is there a numpy builtin to do something like the following? That is, take a list d and return a list filtered_d with any outlying elements removed

10条回答
  •  没有蜡笔的小新
    2020-11-28 18:42

    I wanted to do something similar, except setting the number to NaN rather than removing it from the data, since if you remove it you change the length which can mess up plotting (i.e. if you're only removing outliers from one column in a table, but you need it to remain the same as the other columns so you can plot them against each other).

    To do so I used numpy's masking functions:

    def reject_outliers(data, m=2):
        stdev = np.std(data)
        mean = np.mean(data)
        maskMin = mean - stdev * m
        maskMax = mean + stdev * m
        mask = np.ma.masked_outside(data, maskMin, maskMax)
        print('Masking values outside of {} and {}'.format(maskMin, maskMax))
        return mask
    

提交回复
热议问题