Pramod
Pramod

Reputation: 89

Take Mean of Multiple Multidimensional array in a dictionary

I have a dictionary of 50(days) multidimensional arrays of gridded (lat/lon) data of precipitation. Each array is of size (88, 40). How can I compute the mean of all 50 arrays, and get the result in the same dimension of (88, 40)? i.e. need to take mean at same lat/lon point over all the 50 days.

It works for few days if I give Dictionary name with keys in the following code:

np.nanmean((arraysDict['ppt_subset0'], arraysDict['ppt_subset1']), axis =0)

These are the keys of my dictionary arraysDict ['ppt_subset0', 'ppt_subset1', ... 'ppt_subset49']

But I do not know how to do it for all 50 days, except typing "arraysDict['ppt_subset0']", 50 times in my code of all dictionary.

Upvotes: 0

Views: 475

Answers (2)

sam46
sam46

Reputation: 1271

you can convert it to a one giant 3-dimensional numpy array and then compute the mean:

arr = np.array(list(arraysDict.values()))
mean = arr.mean(axes=0)

Beware though that arr won't necessarily contain the subarrays (or days) in order.

Upvotes: 1

iz_
iz_

Reputation: 16593

You can get all of the values of the dict with .values():

np.nanmean(list(arraysDict.values()), axis=0)

Upvotes: 0

Related Questions