numpy - how to calculate np.nanmean of 2d array -


i have dictionary containing 2d arrays. tried calculate mean way not work because, arrays contains nan values also. there simpler ways calculate mean?

all = np.zeros(385000).reshape(550,700)   in dic.keys():     = dic[i]['data']     avg = (all+a)/len(dic.keys()) 

it seems trying finding mean considering elementwise across both inputs a , b, ignoring nans. so, 1 way stack 2 arrays np.dstack, stack a , b along third axis , use np.nanmean along same axis. thus, have simple implementation -

np.nanmean(np.dstack((a,b)),axis=2) 

sample run -

in [28]: out[28]:  array([[  2.,  nan],        [  5.,   4.]])  in [29]: b out[29]:  array([[ nan,   3.],        [  7.,   2.]])  in [30]: np.nanmean(np.dstack((a,b)),axis=2) out[30]:  array([[ 2.,  3.],        [ 6.,  3.]]) 

for case when getting 2d arrays dictionary shown in posted code of question, can use loop-comprehension gather arrays 3d array np.dstack , use np.nanmean along last axis, -

np.nanmean(np.dstack([d['data'] d in dic]),axis=2) 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -