Given two ndarrays
给定两个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]]
basically it would apply something like
它基本上可以应用
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!
但是我不确定使用什么语法。提前谢谢你的帮助!
3 个解决方案
#1
26
>>> import numpy as np
>>> old_set = [[0, 1], [4, 5]]
>>> new_set = [[2, 7], [0, 1]]
>>> (np.array(old_set) + np.array(new_set)) / 2
array([[1, 4],
[2, 3]])
#2
73
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):
您可以创建一个包含要平均的2D数组的3D数组,然后使用np对轴进行平均。意味着或np。平均数(后者考虑加权平均数):
np.mean( np.array([ old_set, new_set ]), 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
.
这种平均方案可以应用于任何(n)维数组,因为创建的(n+1)维数组始终包含沿其轴=0进行平均的原始数组。
#3
2
Using numpy.average
Also numpy.average
can be used with the same syntax:
也numpy。average(平均)可以使用相同的语法:
import numpy as np
a = np.array([np.arange(0,9).reshape(3,3),np.arange(9,18).reshape(3,3)])
averaged_array = np.average(a,axis=0)
The advantage of numpy.average compared to numpy.mean
is the possibility to use also the weights parameter as an array of the same shape:
numpy的优势。平均numpy相比。均值是使用权重参数的可能性,它是一个形状相同的数组:
weighta = np.empty((3,3))
weightb = np.empty((3,3))
weights = np.array([weighta.fill(0.5),weightb.fill(0.8) ])
np.average(a,axis=0,weights=weights)
If you use masked arrays consider also using numpy.ma.average
because numpy.average
don't deal with them.
如果您使用蒙面数组,请考虑使用numpi .ma。因为numpy平均。普通人是不会处理的。
#1
26
>>> import numpy as np
>>> old_set = [[0, 1], [4, 5]]
>>> new_set = [[2, 7], [0, 1]]
>>> (np.array(old_set) + np.array(new_set)) / 2
array([[1, 4],
[2, 3]])
#2
73
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):
您可以创建一个包含要平均的2D数组的3D数组,然后使用np对轴进行平均。意味着或np。平均数(后者考虑加权平均数):
np.mean( np.array([ old_set, new_set ]), 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
.
这种平均方案可以应用于任何(n)维数组,因为创建的(n+1)维数组始终包含沿其轴=0进行平均的原始数组。
#3
2
Using numpy.average
Also numpy.average
can be used with the same syntax:
也numpy。average(平均)可以使用相同的语法:
import numpy as np
a = np.array([np.arange(0,9).reshape(3,3),np.arange(9,18).reshape(3,3)])
averaged_array = np.average(a,axis=0)
The advantage of numpy.average compared to numpy.mean
is the possibility to use also the weights parameter as an array of the same shape:
numpy的优势。平均numpy相比。均值是使用权重参数的可能性,它是一个形状相同的数组:
weighta = np.empty((3,3))
weightb = np.empty((3,3))
weights = np.array([weighta.fill(0.5),weightb.fill(0.8) ])
np.average(a,axis=0,weights=weights)
If you use masked arrays consider also using numpy.ma.average
because numpy.average
don't deal with them.
如果您使用蒙面数组,请考虑使用numpi .ma。因为numpy平均。普通人是不会处理的。