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]
source share