使用numpy.average的加权平均值

编程入门 行业动态 更新时间:2024-10-24 21:27:17
本文介绍了使用numpy.average的加权平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个数组:

In [37]: bias_2e13 # our array Out[37]: [1.7277990734072355, 1.9718263893212737, 2.469657573252167, 2.869022991373125, 3.314720313010104, 4.232269039271717]

数组中每个值的错误是:

The error on each value in the array is:

In [38]: bias_error_2e13 # the error on each value Out[38]: array([ 0.13271387, 0.06842465, 0.06937965, 0.23886647, 0.30458249, 0.57906816])

现在,我将每个值的错误除以2:

Now I divide the error on each value by 2:

In [39]: error_half # error divided by 2 Out[39]: array([ 0.06635694, 0.03421232, 0.03468982, 0.11943323, 0.15229124, 0.28953408])

现在,我使用numpy.average计算数组的平均值,但使用errors作为weights.

Now I calculate the average of the array using numpy.average, but using the errors as weights.

首先,我在值上使用完整错误,然后在一半上使用 错误,即错误除以2.

First I am using the full error on the values, then I am using half the error, i.e. the error divided by 2.

In [40]: test = np.average(bias_2e13,weights=bias_error_2e13) In [41]: test_2 = np.average(bias_2e13,weights=error_half)

当一个数组的错误是另一个数组的一半时,两个平均值如何给我相同的结果?

In [42]: test Out[42]: 3.3604746813456936 In [43]: test_2 Out[43]: 3.3604746813456936

推荐答案

因为所有错误均具有相同的相对权重.提供weight参数不会更改您平均的实际值,它仅表示每个值对平均值贡献的权重.换句话说,在将每个传递的值乘以其相应的权重后,np.average除以所提供的权重之和.

Because all of the errors have the same relative weight. Supplying a weight parameter does not change the actual values you are averaging, it just indicates the weight with which each value value contributes to the average. In other words, after multiplying each value passed by its corresponding weight, np.average divides by the sum of the weights provided.

>>> import numpy as np >>> np.average([1, 2, 3], weights=[0.2, 0.2, 0.2]) 2.0 >>> np.average([1, 2, 3]) 2.0

有效地,n维数组状容器的平均公式为

Effectively, the average formula for an n-dimensional array-like container is

其中,当未提供给 numpy.average .

where each weight is assumed to be equal to 1 when not provided to numpy.average.

更多推荐

使用numpy.average的加权平均值

本文发布于:2023-11-22 13:13:28,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1617553.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:平均值   numpy   average

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!