numpy.average()
有一個權重選項,但numpy.std()
沒有。有沒有人有解決方法的建議?NumPy中的加權標準偏差?
回答
以下簡短的「手動計算」如何?
def weighted_avg_and_std(values, weights):
"""
Return the weighted average and standard deviation.
values, weights -- Numpy ndarrays with the same shape.
"""
average = numpy.average(values, weights=weights)
# Fast and numerically precise:
variance = numpy.average((values-average)**2, weights=weights)
return (average, math.sqrt(variance))
爲什麼不再爲方差使用'numpy.average'? – user2357112 2013-08-07 01:26:29
只是想指出,這會給偏差的方差。對於小樣本量,您可能需要重新調整方差(在sqrt之前)以獲得無偏差的方差。請參閱https://en.wikipedia.org/wiki/Weighted_variance#Weighted_sample_variance – Corey 2014-03-07 05:17:17
是的,無偏差方差估計量會略有不同。這個答案給出了標準偏差,因爲問題要求'numpy.std()'的加權版本。 – EOL 2014-09-12 09:58:19
在numpy/scipy中似乎還沒有這樣的功能,但是有一個ticket提出了這個附加功能。包括那裏你會發現Statistics.py實施加權標準差。
有在statsmodels
一個類來計算加權統計:statsmodels.stats.weightstats.DescrStatsW
:
from statsmodels.stats.weightstats import DescrStatsW
array = np.array([1,2,1,2,1,2,1,3])
weights = np.ones_like(array)
weights[3] = 100
weighted_stats = DescrStatsW(array, weights=weights, ddof=0)
weighted_stats.mean # weighted mean of data (equivalent to np.average(array, weights=weights))
# 1.97196261682243
weighted_stats.std # standard deviation with default degrees of freedom correction
# 0.21434289609681711
weighted_stats.std_mean # standard deviation of weighted mean
# 0.020818822467555047
weighted_stats.var # variance with default degrees of freedom correction
# 0.045942877107170932
這個類的很好的功能是,如果你想計算不同的統計特性隨後調用將是非常快的,因爲已經計算(甚至中間)的結果被緩存。
有由gaborous提出了一個很好的例子:
import pandas as pd
import numpy as np
# X is the dataset, as a Pandas' DataFrame
mean = mean = np.ma.average(X, axis=0, weights=weights) # Computing the
weighted sample mean (fast, efficient and precise)
# Convert to a Pandas' Series (it's just aesthetic and more
# ergonomic; no difference in computed values)
mean = pd.Series(mean, index=list(X.keys()))
xm = X-mean # xm = X diff to mean
xm = xm.fillna(0) # fill NaN with 0 (because anyway a variance of 0 is
just void, but at least it keeps the other covariance's values computed
correctly))
sigma2 = 1./(w.sum()-1) * xm.mul(w, axis=0).T.dot(xm); # Compute the
unbiased weighted sample covariance
Correct equation for weighted unbiased sample covariance, URL (version: 2016-06-28)
- 1. 標準偏差javascript
- 2. 沿着Numpy陣列軸的質心的標準偏差
- 3. 偏心加權numpy histogram2d?
- 4. 標準偏差環路
- 5. 如何與標準偏差
- 6. 標準偏差問題
- 7. 標準偏差和模式
- 8. 標準偏差arraylist錯誤
- 9. 。計算標準偏差
- 10. 標準偏差Python繪圖
- 11. 差異執行計算標準偏差?
- 12. 在Haskell中計算標準偏差
- 13. Tableau,如何計算加權標準差
- 14. 添加誤差線顯示在R於繪圖標準偏差
- 15. 如何計算Jmeter的標準偏差?
- 16. 傳輸數據時的標準偏差
- 17. 編寫的標準偏差函數
- 18. 沒有意思的標準偏差C++
- 19. 小數位的標準偏差?
- 20. 與Apache Commons Math的標準偏差
- 21. 3個平均值的標準偏差
- 22. 用於快速計算Numpy中大數集標準偏差的方法
- 23. 標準偏差不起作用
- 24. 如何確定一組標準偏差
- 25. 使用Java計算標準偏差?
- 26. 蒙特卡洛標準偏差方程
- 27. 標準偏差代碼錯誤
- 28. scipy.stats.binned_statistcs來計算標準偏差
- 29. 無法計算標準偏差
- 30. 試圖使用標準偏差java
順便說一句,加權標準偏差計算實際上是一個相當複雜的課題 - 不止一種方法去做一件事。請參閱這裏進行一次精彩的討論:https://www.stata.com/support/faqs/statistics/weights-and-summary-statistics/ – JohnE 2017-11-18 17:09:47