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

查看:19
本文介绍了是否有一个 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天全站免登陆