两个Numpy数组的平均值 [英] Average values in two Numpy arrays
问题描述
给出两个ndarrays
Given two ndarrays
old_set = [[0, 1], [4, 5]]
new_set = [[2, 7], [0, 1]]
我正在寻找两个数组之间各个值的平均值,以便数据最终像这样:
I'm looking to get the mean of the respective values between the two arrays so that the data ends up something like:
end_data = [[1, 4], [2, 3]]
基本上它将应用类似
for i in len(old_set):
end_data[i] = (old_set[i]+new_set[i])/2
但是我不确定要使用哪种语法..谢谢您的提前帮助!
But I'm unsure what syntax to use.. Thanks for the help in advance!
推荐答案
您可以创建一个包含要求平均值的2D数组的3D数组,然后使用 np沿
或 axis = 0
求平均值.平均值 np.average
(后者允许加权平均值):
You can create a 3D array containing your 2D arrays to be averaged, then average along axis=0
using np.mean
or np.average
(the latter allows for weighted averages):
np.mean( np.array([ old_set, new_set ]), axis=0 )
此平均方案可以应用于任何(n)
维数组,因为创建的(n + 1)
维数组将始终包含原始数组沿其 axis = 0
进行平均.
This averaging scheme can be applied to any (n)
-dimensional array, because the created (n+1)
-dimensional array will always contain the original arrays to be averaged along its axis=0
.
这篇关于两个Numpy数组的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!