there numpy way make sum each 3 elements in interval? example:
import numpy np mydata = np.array([4, 2, 3, 8, -6, 10])
i result:
np.array([9, 13, 5, 12])
we can use np.convolve
-
np.convolve(mydata,np.ones(3,dtype=int),'valid')
the basic idea convolution
have kernel slide through input array , convolution operation sums elements multiplied kernel elements kernel slides through. so, solve our case window size of 3
, using kernel of 3 1s
generated np.ones(3)
.
sample run -
in [334]: mydata out[334]: array([ 4, 2, 3, 8, -6, 10]) in [335]: np.convolve(mydata,np.ones(3,dtype=int),'valid') out[335]: array([ 9, 13, 5, 12])
Comments
Post a Comment