2010-03-09 41 views
52

numpy.average() có tùy chọn trọng số, nhưng numpy.std() thì không. Có ai có đề xuất cho một workaround?Độ lệch chuẩn có trọng số trong NumPy?

+0

Btw, tính dev std trọng thực sự là một vấn đề khá phức tạp - có nhiều hơn một cách để làm điều đó. Xem ở đây để có một cuộc thảo luận tuyệt vời: https://www.stata.com/support/faqs/statistics/weights-and-summary-statistics/ – JohnE

Trả lời

80

Làm thế nào về "tính toán thủ công" ngắn sau đây?

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)) 
+2

Tại sao không sử dụng lại 'numpy.average' cho phương sai? – user2357112

+4

Chỉ muốn chỉ ra rằng điều này sẽ cung cấp phương sai thiên vị. Đối với các kích thước mẫu nhỏ, bạn có thể muốn quy mô lại phương sai (trước sqrt) để có được phương sai không thiên vị. Xem https://en.wikipedia.org/wiki/Weighted_variance#Weighted_sample_variance – Corey

+1

Vâng, ước tính phương sai không thiên vị sẽ hơi khác. Câu trả lời này đưa ra độ lệch chuẩn, vì câu hỏi yêu cầu một phiên bản trọng số của 'numpy.std()'. – EOL

6

Dường như không có chức năng như vậy trong vũng/bẩn, nhưng có một ticket đề xuất chức năng bổ sung này. Bao gồm ở đó bạn sẽ tìm thấy Statistics.py thực hiện độ lệch tiêu chuẩn trọng số.

13

Có một lớp học trong statsmodels để tính toán thống kê trọng: 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 

Các tính năng thú vị của lớp này là nếu bạn muốn để tính toán tính chất thống kê khác nhau cuộc gọi tiếp theo sẽ rất nhanh vì đã được tính toán (ngay cả trung gian) kết quả được lưu trữ.

1

Có một ví dụ rất tốt bởi gaborous đề xuất:

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)

Các vấn đề liên quan