它似乎SciPy的一次提供的功能mad
來計算一組數字的平均絕對偏差:我在哪裏可以找到瘋狂(平均絕對偏差)在scipy?
http://projects.scipy.org/scipy/browser/trunk/scipy/stats/models/utils.py?rev=3473
不過,我不能在任何地方的SciPy的當前版本中找到它。當然,可以從存儲庫複製舊代碼,但我更喜歡使用scipy的版本。我在哪裏可以找到它,或者它已被替換或刪除?
它似乎SciPy的一次提供的功能mad
來計算一組數字的平均絕對偏差:我在哪裏可以找到瘋狂(平均絕對偏差)在scipy?
http://projects.scipy.org/scipy/browser/trunk/scipy/stats/models/utils.py?rev=3473
不過,我不能在任何地方的SciPy的當前版本中找到它。當然,可以從存儲庫複製舊代碼,但我更喜歡使用scipy的版本。我在哪裏可以找到它,或者它已被替換或刪除?
statsmodels的當前版本具有mad
在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])
請注意,默認情況下,此計算標準偏差的穩健估計值,假定正態分佈通過縮放結果得到一個縮放因子;從help
:
Signature: robust.mad(a,
c=0.67448975019608171,
axis=0,
center=<function median at 0x10ba6e5f0>)
在R
版本也有類似的正常化。如果你不想要這個,顯然只需設置c=1
。
(較早的評論在statsmodels.robust.scale
提到這一點的存在,其實現是statsmodels/robust/scale.py
(見github),但robust
包不出口scale
,而是在scale.py
出口的公共職能明確。)
由於烘烤不足,它看起來像2008年8月的scipy.stats.models爲removed。開發已遷移到statsmodels
。
是的,大部分舊的stats.models是scikits.statsmodels的基礎,經過大量的清理。 MAD位於底部頁面http://statsmodels.sourceforge.net/rlm.html作爲線性模型的穩健估計的一部分,但我從未單獨使用它,因爲它只是幾行。 – user333700
上述鏈接已損壞,因此我在statsmodels上找到[this one](http://statsmodels.sourceforge.net/devel/generated/statsmodels.robust.scale.mad.html?highlight=median%20absolute%20deviation)文檔。 – gabra
這不是SciPy的版本,但這裏使用屏蔽陣列忽略壞值MAD的實現: http://code.google.com/p/agpy/source/browse/trunk/agpy/mad.py
編輯:最近的一個版本可用here。
編輯2:還有一個版本在astropy here。
[編輯]由於這種不斷得到downvoted:我知道平均絕對偏差是一個比較常用的統計數據,但要求提問意味着絕對偏差,這裏是如何做到這一點:
from numpy import mean, absolute
def mad(data, axis=None):
return mean(absolute(data - mean(data, axis)), axis)
我使用:
from math import fabs
a = [1, 1, 2, 2, 4, 6, 9]
median = sorted(a)[len(a)//2]
for b in a:
mad = fabs(b - median)
print b,mad
當樣本數爲偶數時,這不起作用。 – heroxbd
對於它的價值,我用什麼本作MAD:
def mad(arr):
""" Median Absolute Deviation: a "Robust" version of standard deviation.
Indices variabililty of the sample.
https://en.wikipedia.org/wiki/Median_absolute_deviation
"""
arr = np.ma.array(arr).compressed() # should be faster to not use masked arrays.
med = np.median(arr)
return np.median(np.abs(arr - med))
不錯的解決方案;然而,提問者詢問**平均**絕對偏差。你提供了**中位**絕對偏差。 –
我只是學習Python和NumPy的,但這裏是我寫檢查哪曾想2臺數的M(EAN)AD我七年級學生的數學作業代碼:
在numpy的矩陣數據行:
import numpy as np
>>> 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)
>>> matMad = np.mean(np.abs(np.tile(np.mean(a, axis=1), (1, a.shape[1])) - a), axis=1)
>>> matMad
matrix([[ 1.81632653],
[ 3.73469388]])
數據在numpy的一維數組:
>>> a1 = np.array([ 80, 76, 77, 78, 79, 81, 76, 77, 79, 84, 75, 79, 76, 78 ], dtype=float)
>>> a2 = np.array([ 66, 69, 76, 72, 79, 77, 74, 77, 71, 79, 74, 66, 67, 73 ], dtype=float)
>>> madA1 = np.mean(np.abs(np.tile(np.mean(a1), (1, len(a1))) - a1))
>>> madA2 = np.mean(np.abs(np.tile(np.mean(a2), (1, len(a2))) - a2))
>>> madA1, madA2
(1.816326530612244, 3.7346938775510199)
如果你喜歡在Pandas工作(像我一樣),它有一個非常有用function for the mean absolute deviation:
import pandas as pd
df = pd.DataFrame()
df['a'] = [1, 1, 2, 2, 4, 6, 9]
df['a'].mad()
輸出:2.3673469387755106
使用numpy
只:
def meanDeviation(numpyArray):
mean = np.mean(numpyArray)
f = lambda x: abs(x - mean)
vf = np.vectorize(f)
return (np.add.reduce(vf(numpyArray)))/len(numpyArray)
不好意思,在[github存儲庫](https://github.com/scipy/scipy)中搜索沒有給我任何東西。 –
從頭開始編寫它很難嗎? –
@RomanSusi,不,但正如我在問題中所說的,那不是重點。 –