Subsampling/averaging over a numpy array
Using NumPy routines you could try something like
import numpy
x = numpy.array([1, 2, 3, 4, 5, 6])
numpy.mean(x.reshape(-1, 2), 1) # Prints array([ 1.5, 3.5, 5.5])
and just replace the 2
in the reshape
call with the number of items you want to average over.
Edit: This assumes that n
divides into the length of x
. You'll need to include some checks if you are going to turn this into a general function. Perhaps something like this:
def average(arr, n):
end = n * int(len(arr)/n)
return numpy.mean(arr[:end].reshape(-1, n), 1)
This function in action:
>>> x = numpy.array([1, 2, 3, 4, 5, 6])
>>> average(x, 2)
array([ 1.5, 3.5, 5.5])
>>> x = numpy.array([1, 2, 3, 4, 5, 6, 7])
>>> average(x, 2)
array([ 1.5, 3.5, 5.5])
def subsample(data, sample_size):
samples = list(zip(*[iter(data)]*sample_size)) # use 3 for triplets, etc.
return map(lambda x:sum(x)/float(len(x)), samples)
l = [1, 2, 3, 4, 5, 6]
print subsample(l, 2)
print subsample(l, 3)
print subsample(l, 5)
Gives:
[1.5, 3.5, 5.5]
[2.0, 5.0]
[3.0]