我在哪里可以找到 scipy 中的 mad(平均绝对偏差)? [英] Where can I find mad (mean absolute deviation) in scipy?

查看:32
本文介绍了我在哪里可以找到 scipy 中的 mad(平均绝对偏差)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎 scipy 曾经提供了一个函数 mad 来计算一组数字的平均绝对偏差:

http://projects.scipy.org/scipy/browser/trunk/scipy/stats/models/utils.py?rev=3473

但是,我在当前版本的 scipy 中找不到它.当然,可以只从存储库中复制旧代码,但我更喜欢使用 scipy 的版本.我在哪里可以找到它,或者它是否已被更换或移除?

解决方案

statsmodels<的当前版本/a> 在 statsmodels.robust 中有 mad:

<预><代码>>>>将 numpy 导入为 np>>>从 statsmodels 导入强大>>>a = np.matrix( [... [ 80, 76, 77, 78, 79, 81, 76, 77, 79, 84, 75, 79, 76, 78 ],... [ 66, 69, 76, 72, 79, 77, 74, 77, 71, 79, 74, 66, 67, 73 ]... ], dtype=float )>>>健壮的.mad(a,轴=1)数组([2.22390333,5.18910776])

请注意,默认情况下,通过将结果缩放比例因子来计算假设正态分布的标准偏差的稳健估计;来自 help:

签名:robust.mad(a,c=0.67448975019608171,轴=0,center=<0x10ba6e5f0处的函数中位数>)

R 中的版本进行了类似的规范化.如果你不想这样,显然只需设置 c=1.

(之前的评论提到这是在 statsmodels.robust.scale 中.实现在 statsmodels/robust/scale.py(见 github) 但 robust 包不导出 scale,而是显式导出 scale.py 中的公共函数.)

It seems scipy once provided a function mad to calculate the mean absolute deviation for a set of numbers:

http://projects.scipy.org/scipy/browser/trunk/scipy/stats/models/utils.py?rev=3473

However, I can not find it anywhere in current versions of scipy. Of course it is possible to just copy the old code from repository but I prefer to use scipy's version. Where can I find it, or has it been replaced or removed?

解决方案

The current version of statsmodels has mad in statsmodels.robust:

>>> import numpy as np
>>> from statsmodels import robust
>>> a = np.matrix( [
...     [ 80, 76, 77, 78, 79, 81, 76, 77, 79, 84, 75, 79, 76, 78 ],
...     [ 66, 69, 76, 72, 79, 77, 74, 77, 71, 79, 74, 66, 67, 73 ]
...  ], dtype=float )
>>> robust.mad(a, axis=1)
array([ 2.22390333,  5.18910776])

Note that by default this computes the robust estimate of the standard deviation assuming a normal distribution by scaling the result a scaling factor; from help:

Signature: robust.mad(a, 
                      c=0.67448975019608171, 
                      axis=0, 
                      center=<function median at 0x10ba6e5f0>)

The version in R makes a similar normalization. If you don't want this, obviously just set c=1.

(An earlier comment mentioned this being in statsmodels.robust.scale. The implementation is in statsmodels/robust/scale.py (see github) but the robust package does not export scale, rather it exports the public functions in scale.py explicitly.)

这篇关于我在哪里可以找到 scipy 中的 mad(平均绝对偏差)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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