gpt4 book ai didi

python - 计算存储在列表中的二维 numpy 数组的平均值

转载 作者:太空宇宙 更新时间:2023-11-03 20:32:22 48 4
gpt4 key购买 nike

一个:

[array([[0.10865657, 0.10638294, 0.10471012, 0.09508586, 0.09283491],
[0.10892282, 0.10664408, 0.10496752, 0.09531553, 0.09305617],
[0.11664 , 0.1143077 , 0.11259081, 0.1026154 , 0.10025029],
[0.11626453, 0.11392252, 0.11219875, 0.10217754, 0.09980005]]),
array([[0.04213751, 0.04178241, 0.04158858, 0.04331489, 0.04447674],
[0.04213751, 0.04178241, 0.04158858, 0.04331489, 0.04447674],
[0.04267657, 0.04255925, 0.04253528, 0.04520177, 0.04655534],
...

我可以做a[0].mean,我会得到想要的结果。我想用 for 循环对 'a' 的整个长度进行处理。

我已经尝试过:

mean_all = []

for i in len(dist):
mean = dist[i].mean
mean_all.append(mean)

TypeError: 'int' object is not iterable

最佳答案

首先,dist[0].mean 返回一个函数,并且NOT返回平均值。一般来说,您需要 dist[0].mean()

您可以使用列表理解轻松避免 for 循环:

from numpy import array

dist = [array([[0.10865657, 0.10638294, 0.10471012, 0.09508586, 0.09283491],
[0.10892282, 0.10664408, 0.10496752, 0.09531553, 0.09305617],
[0.11664 , 0.1143077 , 0.11259081, 0.1026154 , 0.10025029],
[0.11626453, 0.11392252, 0.11219875, 0.10217754, 0.09980005]]),
array([[0.04213751, 0.04178241, 0.04158858, 0.04331489, 0.04447674],
[0.04213751, 0.04178241, 0.04158858, 0.04331489, 0.04447674],
[0.04267657, 0.04255925, 0.04253528, 0.04520177, 0.04655534]])]

mean_all = [dist[i].mean() for i in range(len(dist))]

print(mean_all)
[0.10536720549999998, 0.04307523133333334]
<小时/>

如果您确实想使用for循环,请使用:

mean_all = []
for i in range(len(dist)):
mean = dist[i].mean()
mean_all.append(mean)

print(mean_all)
[0.10536720549999998, 0.04307523133333334]

关于python - 计算存储在列表中的二维 numpy 数组的平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57408784/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com