加权平均列表

Weighted averaging a list

感谢您的回复。是的,我正在寻找加权平均值。

rate = [14.424, 14.421, 14.417, 14.413, 14.41]

amount = [3058.0, 8826.0, 56705.0, 30657.0, 12984.0]

我想要基于底部列表的每个项目的顶部列表的加权平均值。

因此,如果第一个底部列表项目很小(例如 3,058 与总数 112,230 相比),那么第一个顶部列表项目对顶部列表平均值的影响应该较小。

这是我尝试过的一些方法。它给了我一个看起来正确的答案,但我不确定它是否符合我正在寻找的内容。

for g in range(len(rate)):
    rate[g] = rate[g] * (amount[g] / sum(amount))
rate = sum(rate)

编辑: 在将其他回复与我的代码进行比较后,我决定使用邮政编码以使其尽可能短。

这看起来像是加权平均值。

values = [1, 2, 3, 4, 5]
weights = [2, 8, 50, 30, 10]

s = 0
for x, y in zip(values, weights):
    s += x * y

average = s / sum(weights)
print(average) # 3.38

这输出 3.38,它确实更倾向于具有最高权重的值。

for g in range(len(rate)):
   rate[g] = rate[g] * amount[g] / sum(amount)
rate = sum(rate)

等同于:

sum(rate[g] * amount[g] / sum(amount) for g in range(len(rate)))

等同于:

sum(rate[g] * amount[g] for g in range(len(rate))) / sum(amount)

等同于:

sum(x * y for x, y in zip(rate, amount)) / sum(amount)

结果:

14.415602815646439

让我们使用 python zip 函数

zip([iterable, ...])

此函数 returns 一个元组列表,其中第 i 个元组包含来自每个参数序列或可迭代对象的第 i 个元素。返回列表的长度被截断为最短参数序列的长度。当有多个长度相同的参数时,zip() 类似于 map(),初始参数为 None。使用单个序列参数,它 returns 一个 1 元组列表。没有参数,它 returns 一个空列表。

weights = [14.424, 14.421, 14.417, 14.413, 14.41]
values = [3058.0, 8826.0, 56705.0, 30657.0, 12984.0]
weighted_average = sum(weight * value for weight, value in zip(weights, values)) / sum(weights)

您可以使用 numpy.average 来计算加权平均值。

In [13]: import numpy as np

In [14]: rate = [14.424, 14.421, 14.417, 14.413, 14.41]

In [15]: amount = [3058.0, 8826.0, 56705.0, 30657.0, 12984.0]

In [17]: weighted_avg = np.average(rate, weights=amount)

In [19]: weighted_avg
Out[19]: 14.415602815646439

作为记录和测试的函数:

def weighted_average(values, weights=None):
    """
    Returns the weighted average of `values` with weights `weights`
    Returns the simple aritmhmetic average if `weights` is None.
    >>> weighted_average([3, 9], [1, 2])
    7.0
    >>> 7 == (3*1 + 9*2) / (1 + 2)
    True
    """
    if weights == None:
        weights = [1 for _ in range(len(values))]
    normalization = 0
    val = 0
    for value, weight in zip(values, weights):
        val += value * weight
        normalization += weight
    return val / normalization

为了完整起见,另一个版本的值和权重存储在元组中:

def weighted_average(values_and_weights):
    """
    The input is expected in the form:
        [(value_1, weight_1), (value_2, weight_2), ...(value_n, weight_n)]
    >>> weighted_average([(3,1), (9,2)])
    7.0
    >>> 7 == (3*1 + 9*2) / (1 + 2)
    True

    """
    normalization = 0
    val = 0
    for value, weight in values_and_weights:
        val += value * weight
        normalization += weight
    return val / normalization