I have a for loop (100 passes) which generates a numpy array during each pass. Is there a way to add these 100 arrays (element wise) and then calculate the array which represents the average of these 100 arrays?
1 Answer
Perhaps you're looking for:
np.mean(arr, axis=0)
Alternatively, you can do:
np.sum(arr, axis=0) / len(arr)
Here, arr is the array you created with the loop.
You can define arr as:
arr = []
for i in range(100):
# create numpy array here and assign it to x
arr.append(x)
Then you can do np.mean etc on arr.
np.meanalong the right axis. But if all you need is the mean, you could sum at each step, e.g.total += new_array, and get the mean from that sum.