i have numpy matrix, , each row has combination of positive , negative numbers.
i want create new vector, gives me average of positive numbers in row in matrix.
for instance, if matrix:
[[1 2 3 -1] [2 5 -6 5]]
i want create vector values:
[[2] [4]]
what's fastest way so?
there positive numbers.
if it's guaranteed have @ least 1 positive number (>=0)
per row, convert negative numbers (excluding 0)
nans
np.where
, use np.nanmean
along rows, -
np.nanmean(np.where(a>=0,a,np.nan),axis=1)
sample run -
in [69]: out[69]: array([[ 2, 3, -6, -6, -4], [-5, -6, -1, -1, 3], [-8, 5, -7, -9, -9], [-3, 0, 7, -5, -6]]) in [70]: np.nanmean(np.where(a>=0,a,np.nan),axis=1) out[70]: array([ 2.5, 3. , 5. , 3.5])
Comments
Post a Comment