NumPy中的加权标准差 [英] Weighted standard deviation in NumPy

查看:83
本文介绍了NumPy中的加权标准差的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

numpy.average()具有权重选项,但numpy.std()没有.有人对解决方法有建议吗?

numpy.average() has a weights option, but numpy.std() does not. Does anyone have suggestions for a workaround?

推荐答案

接下来的简短手动计算"如何?

How about the following short "manual calculation"?

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中的加权标准差的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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