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

最佳答案

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

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

关于python - NumPy中的加权标准差,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2413522/

10-12 19:55