Home > Mobile >  Adding arrays generated in for loop in Python
Adding arrays generated in for loop in Python

Time:12-13

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?

CodePudding user response:

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.

  • Related