Python - 加权平均列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29330792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 04:25:03  来源:igfitidea点击:

Python - Weighted averaging a list

pythonlistaverage

提问by GShocked

Thanks for your responses. Yes, I was looking for the weighted average.

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

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

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

I want the weighted average of the top list based on each item of the bottom list.

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

So, if the first bottom-list item is small (such as 3,058 compared to the total 112,230), then the first top-list item should have less of an effect on the top-list average.

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

Here is some of what I have tried. It gives me an answer that looks right, but I am not sure if it follows what I am looking for.

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

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

EDIT: After comparing other responses with my code, I decided to use the zip code to keep it as short as possible.

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

采纳答案by JuniorCompressor

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

is the same as:

是相同的:

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

which is the same as:

这与:

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

which is the same as:

这与:

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

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

Result:

结果:

14.415602815646439

回答by maahl

This looks like a weighted average.

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

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

This outputs 3.38, which indeed tends more toward the values with the highest weights.

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

回答by ?ukasz Rogalski

Let's use python zipfunction

让我们使用pythonzip函数

zip([iterable, ...])

This function returns a list of tuples, where the i-th tuple contains the i-th element from each of the argument sequences or iterables. The returned list is truncated in length to the length of the shortest argument sequence. When there are multiple arguments which are all of the same length, zip() is similar to map() with an initial argument of None. With a single sequence argument, it returns a list of 1-tuples. With no arguments, it returns an empty list.

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

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)

回答by Akavall

You could use numpy.averageto calculate weighted average.

您可以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