是否有一个内置的numpy来拒绝列表中的离群值 [英] Is there a numpy builtin to reject outliers from a list

查看:204
本文介绍了是否有一个内置的numpy来拒绝列表中的离群值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有内置的numpy来执行以下操作?也就是说,取一个列表d并返回一个列表filtered_d,其中删除了所有基于d中点的假定分布而删除的外围元素.

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 based on some assumed distribution of the points in d.

import numpy as np

def reject_outliers(data):
    m = 2
    u = np.mean(data)
    s = np.std(data)
    filtered = [e for e in data if (u - 2 * s < e < u + 2 * s)]
    return filtered

>>> d = [2,4,5,1,6,5,40]
>>> filtered_d = reject_outliers(d)
>>> print filtered_d
[2,4,5,1,6,5]

之所以说类似",是因为该函数可能允许变化的分布(泊松,高斯等)和分布中的异常阈值(例如我在这里使用的m).

I say 'something like' because the function might allow for varying distributions (poisson, gaussian, etc.) and varying outlier thresholds within those distributions (like the m I've used here).

推荐答案

此方法与您的方法几乎相同,只是更多的numpyst(仅适用于numpy数组):

This method is almost identical to yours, just more numpyst (also working on numpy arrays only):

def reject_outliers(data, m=2):
    return data[abs(data - np.mean(data)) < m * np.std(data)]

这篇关于是否有一个内置的numpy来拒绝列表中的离群值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆