I have a numpy array with floats.
What I would like to have (if it is not already existing) is a function that gives me a new array of the average of every x points in the given array, like sub sampling (and opposite of interpolation(?)).
E.g. sub_sample(numpy.array([1, 2, 3, 4, 5, 6]), 2) gives [1.5, 3.5, 5.5]
E.g. Leftovers can be removed, e.g. sub_sample(numpy.array([1, 2, 3, 4, 5]), 2) gives [1.5, 3.5]
Thanks in advance.
Using NumPy routines you could try something like
and just replace the
2
in thereshape
call with the number of items you want to average over.Edit: This assumes that
n
divides into the length ofx
. You'll need to include some checks if you are going to turn this into a general function. Perhaps something like this:This function in action: