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. 加權標準偏差
- 2. 掩碼下的Numpy標準偏差
- 3. numpy標準偏差堆疊陣列
- 4. numpy標準偏差估計偏差與ddof = 1
- 5. 標準偏差javascript
- 6. Cassandra標準偏差
- 7. 如何計算加權測量值之間的標準偏差?
- 8. ChunkyPNG的標準偏差
- 9. 加權標準偏差和四分位excel
- 10. 沿着Numpy陣列軸的質心的標準偏差
- 11. 偏心加權numpy histogram2d?
- 12. Numpy:評估高於/低於平均值的標準偏差
- 13. 如何從一組標準偏差中獲得增量標準偏差?
- 14. 移動標準偏差Gnuplot
- 15. 明日標準偏差列
- 16. 滾動標準偏差
- 17. ZeroDivisionError:計算標準偏差
- 18. 標準偏差計劃
- 19. Python標準偏差檢查
- 20. 。計算標準偏差
- 21. 標準偏差問題
- 22. 標準偏差arraylist錯誤
- 23. 如何與標準偏差
- 24. 標準偏差Python繪圖
- 25. 標準偏差和模式
- 26. 標準偏差環路
- 27. 查找標準偏差
- 28. 計算作業中的標準偏差
- 29. 差異執行計算標準偏差?
- 30. 找到偏差/元素列表的標準偏差
順便說一句,加權標準偏差計算實際上是一個相當複雜的課題 - 不止一種方法去做一件事。請參閱這裏進行一次精彩的討論:https://www.stata.com/support/faqs/statistics/weights-and-summary-statistics/ – JohnE 2017-11-18 17:09:47