Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Subsampling/averaging over a numpy array

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.

like image 716
Michel Keijzers Avatar asked Jun 01 '12 09:06

Michel Keijzers


People also ask

How do you find the average of a NumPy array?

Numpy Average Using Numpy, you can calculate average of elements of total Numpy Array, or along some axis, or you can also calculate weighted average of elements. To find the average of an numpy array, you can use numpy. average() statistical function.

How do you average two NumPy arrays?

Finding average of NumPy arrays is quite similar to finding average of given numbers. We just have to get the sum of corresponding array elements and then divide that sum with the total number of arrays.

Is appending to NumPy array efficient?

Appending to numpy arrays is very inefficient. This is because the interpreter needs to find and assign memory for the entire array at every single step. Depending on the application, there are much better strategies. If you know the length in advance, it is best to pre-allocate the array using a function like np.

What does .all do in NumPy?

all() in Python. The numpy. all() function tests whether all array elements along the mentioned axis evaluate to True.


2 Answers

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])
like image 117
Chris Avatar answered Oct 18 '22 00:10

Chris


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]
like image 38
Maria Zverina Avatar answered Oct 18 '22 00:10

Maria Zverina