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 )
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.
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 )
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.
>>> 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.0
array([[1., 4.],
[2., 3.]])
python - Weighted average element-wise between two arrays - Stack Overflow
python - How to calculate Average of n numpy arrays - Stack Overflow
How can I average an array of arrays in python? - Stack Overflow
Average multiple arrays in loop
Just use NumPy's vectorised operations. To do so, first convert your lists to arrays and then just multiply each array with the respective weight and take the sum
import numpy as np
array_1 = np.array([0,1,2,3,4])
array_2 = np.array([2,3,4,5,6])
weight_1 = 0.5
weight_2 = 0.5
array_3 = weight_1*array_1 + weight_2*array_2
# array([1., 2., 3., 4., 5.])
A direct NumPy solution using np.average would be the following, where axis=0 means take the average row wise (using both columns). np.vstack() simply stacks the two arrays vertically.
np.average(np.vstack((array_1, array_2)), axis=0, weights=[weight_1, weight_2])
As pointed out by @yatu, you can also pass a list of your arrays and specify the axis
np.average([array_1, array_2], axis=0, weights=[weight_1, weight_2])
Timing comparison inspired by the comments on @yatu's answer: As you can see, list comprehension and zip is slightly faster here but then this performance is for small arrays. I am sure, for large arrays, the vectorised solution will take over
Devesh's method
%timeit result = [ item1 * weight_1 + item2 * weight_2 for item1, item2 in zip(array_1, array_2)]
# 25.5 µs ± 3.75 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit np.average([array_1, array_2], axis=0, weights=[weight_1, weight_2])
# 42.9 µs ± 2.94 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit np.average(np.vstack((array_1, array_2)), axis=0, weights=[weight_1, weight_2])
# 44.8 µs ± 4.98 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
You can zip both iterators, and multiply each element with the corresponding weight
array_1 = [0,1,2,3,4]
array_2 = [2,3,4,5,6]
weight_1 = 0.5
weight_2 = 0.5
#Zip both iterators and multiply weight with corresponding item
result = [ item1 * weight_1 + item2 * weight_2 for item1, item2 in zip(array_1, array_2)]
print(result)
The output will be
[1.0, 2.0, 3.0, 4.0, 5.0]
Your record array from the example above is three dimensional, with shape:
>>> record.shape
(2, 10, 2)
The first dimension corresponds to the 2 iterations of your experiment. To average them, you need to tell np.average to do its thing along axis=0
>>> np.average(record, axis=0)
array([[ 0. , 0.45688836],
[ 0.91377672, 1.37066507],
[ 1.82755343, 2.28444179],
[ 2.74133015, 3.19821851],
[ 3.65510686, 4.11199522],
[ 4.56888358, 5.02577194],
[ 5.4826603 , 5.93954865],
[ 6.39643701, 6.85332537],
[ 7.31021373, 7.76710209],
[ 8.22399044, 8.6808788 ]])
If you know beforehand how many simulations you are going to run, you are better off skipping the list thing altogether and doing something like this:
simulations, sim_rows, sim_cols = 1000000, 10, 2
record = np.empty((simulations, sim_rows, sim_cols))
for j in xrange(simulations) :
record[j] = np.random.rand(sim_rows, sim_cols)
>>> np.average(record, axis=0)
[[ 0.50021935 0.5000554 ]
[ 0.50019659 0.50009123]
[ 0.50008591 0.49973058]
[ 0.49995812 0.49973941]
[ 0.49998854 0.49989957]
[ 0.5002542 0.50027464]
[ 0.49993122 0.49989623]
[ 0.50024623 0.49981818]
[ 0.50005848 0.50016798]
[ 0.49984452 0.49999112]]
Basically you can use
record.mean(axis=0)
I am not sure over which axis you want to average, as in your example two axes have dimension 2 (your array has shape (2,10,2)). If you meant to average the last one, just use
record.mean(axis=2)