计算多个数组中每个索引的平均值
Compute mean of values for each index across multiple arrays
目前我正在寻找一种紧凑且更有效的解决方案(而不是多个嵌套 for 循环)来计算给定跨多个 numpy 数组的索引的值的平均值。
具体给出
[array([2.4, 3.5, 2.9]),
array([4.5, 1.8, 1.4])]
我需要计算以下数组:
[array([3.45, 2.65, 2.15])]
有什么想法吗?谢谢大家
只需要一行命令 numpy
import numpy as np
arr=[np.array([2.4, 3.5, 2.9]),
np.array([4.5, 1.8, 1.4])]
np.mean(arr, axis = 0)
如果没有 Numpy,你可以使用 map 和 zip 来获取它。
lists = [[2.4, 3.5, 2.9],[4.5, 1.8, 1.4]]
li = list(zip(*lists))
sumation = list(map(sum,li))
average = list(map( lambda x: x/len(lists) ,sumation))
print(s)
目前我正在寻找一种紧凑且更有效的解决方案(而不是多个嵌套 for 循环)来计算给定跨多个 numpy 数组的索引的值的平均值。
具体给出
[array([2.4, 3.5, 2.9]),
array([4.5, 1.8, 1.4])]
我需要计算以下数组:
[array([3.45, 2.65, 2.15])]
有什么想法吗?谢谢大家
只需要一行命令 numpy
import numpy as np
arr=[np.array([2.4, 3.5, 2.9]),
np.array([4.5, 1.8, 1.4])]
np.mean(arr, axis = 0)
如果没有 Numpy,你可以使用 map 和 zip 来获取它。
lists = [[2.4, 3.5, 2.9],[4.5, 1.8, 1.4]]
li = list(zip(*lists))
sumation = list(map(sum,li))
average = list(map( lambda x: x/len(lists) ,sumation))
print(s)