Home > Mobile >  Numpy array mean each ten values
Numpy array mean each ten values

Time:02-15

I have a numpy array with millions of values. It has only a dimension. I'd like to return a new numpy array with the mean values of each tens.

What could be the solution?

Thanks for the support!

CodePudding user response:

reshape, sum, and divide by 10:

N = 10
a.reshape(-1,N).sum(1)/N

or, using numpy.mean:

np.mean(a.reshape(-1,10), 1)

NB. You need to ensure you array has a length that is a multiple of N. If not, slice to truncate or pad to add missing values

example:

a = np.arange(10*20)
a.reshape(-1,10).sum(1)/10

output:

array([  4.5,  14.5,  24.5,  34.5,  44.5,  54.5,  64.5,  74.5,  84.5,
        94.5, 104.5, 114.5, 124.5, 134.5, 144.5, 154.5, 164.5, 174.5,
       184.5, 194.5])

CodePudding user response:

Suppose 'a' is your array with million or so entries, then you can get desired output as follows:

x = np.array([np.mean(a[i:i 10]) for i in range(0, len(a), 10)]) 

CodePudding user response:

If your array is not well balanced, fill your array with nan and use np.nanmean to calculate the mean:

a = np.arange(123)
N = 10
M = int(np.ceil(len(a) / 10) * 10 - len(a))
b = np.nanmean(np.concatenate([a, np.full(M, np.nan)]).reshape(-1, N), axis=1)

Output:

>>> b
array([  4.5,  14.5,  24.5,  34.5,  44.5,  54.5,  64.5,  74.5,  84.5,
        94.5, 104.5, 114.5, 121. ])

121 = (120 121 122 nan nan nan nan nan nan nan) / 3

  • Related