Building an array by averaging n consecutive numbers in an array

I have an array of different sizes, from which I would like to average each consecutive n numbers and build another array as a result.

I came up with two different ways, but each has its own problems, and I'm not sure if this is the best way to solve this problem:

  • Using the numpy.array_split () function:

    import numpy as np no_splits = 3 #Or any number user defines no_items = int(np.random.random(1)*100) # To get a variable number of items pre_array = np.random.random(no_items) mean_array = np.mean(np.array_split(pre_array,no_splits)) #This is efficient but gives an error if len(pre_array)%no_splits != 0 
  • list (pre_array) alternative:

     mean_array = [np.mean(pre_array[i-no_splits+1:i]) for i, x in enumerate(pre_array) if i%no_splits == 0 and i != 0] 

This is fine, but it pinches the last values โ€‹โ€‹if i% no_splits! = 0. Ideally, I would create the last value, which is average for the rest, keeping a compact code.

Each of them works for my purposes, but I'm not sure if they are most effective for large arrays.

Thank you in advance!

+4
source share
2 answers

Use uniform_filter :

 >>> import scipy.ndimage.filters as filter >>> a=np.arange(5,dtype=np.double) >>> filter.uniform_filter(a,size=3) array([ 0.33333333, 1. , 2. , 3. , 3.66666667]) #What this is actually doing >>> np.mean([0,0,1]) #ind0 0.33333333333333331 >>> np.mean([0,1,2]) #ind1 1.0 >>> np.mean([1,2,3]) #ind2 2.0 

It can be used with any size window.

 >>> filter.uniform_filter(a,size=5) array([ 0.8,  1.2,  2. ,  2.8,  3.2]) 

The danger here is that the battery will be any type of array.


Group in three, then take the average:

 def stride_mean(arr,stride): extra = arr.shape[0]%stride if extra==0: return np.mean(arr.reshape(-1,stride),axis=1) else: toslice = arr.shape[0]-extra first = np.mean(arr[:toslice].reshape(-1,stride),axis=1) rest = np.mean(arr[toslice:]) return np.hstack((first,rest)) print pre_array [ 0.50712539 0.75062019 0.78681352 0.35659332] print stride_mean(pre_array,3) [ 0.6815197 0.35659332] 
+4
source
 no_splits = 3 no_items = 100 a = np.random.rand(no_items) no_bins = no_splits + no_items % no_splits b = np.empty((no_bins,), dtype=a.dtype) endpoint = no_items//no_splits b[:no_splits] = np.mean(a[:endpoint*no_splits].reshape(-1, endpoint), axis=-1) b[no_splits:] = np.mean(a[endpoint*no_splits:]) >>> b array([ 0.49898723, 0.49457975, 0.45601632, 0.5316093 ]) 
+1
source

Source: https://habr.com/ru/post/1499845/


All Articles