Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy - Averaging multiple columns of a 2D array

Tags:

python

numpy

Right now I am doing this by iterating, but there has to be a way to accomplish this task using numpy functions. My goal is to take a 2D array and average J columns at a time, producing a new array with the same number of rows as the original, but with columns/J columns.

So I want to take this:

J = 2 // two columns averaged at a time

[[1 2 3 4]
 [4 3 7 1]
 [6 2 3 4]
 [3 4 4 1]]

and produce this:

[[1.5 3.5]
 [3.5 4.0]
 [4.0 3.5]
 [3.5 2.5]]

Is there a simple way to accomplish this task? I also need a way such that if I never end up with an unaveraged remainder column. So if, for example, I have an input array with 5 columns and J=2, I would average the first two columns, then the last three columns.

Any help you can provide would be great.

like image 914
user1764386 Avatar asked Jan 18 '13 14:01

user1764386


1 Answers

data.reshape(-1,j).mean(axis=1).reshape(data.shape[0],-1)

If your j divides data.shape[1], that is.

Example:

In [40]: data
Out[40]: 
array([[7, 9, 7, 2],
       [7, 6, 1, 5],
       [8, 1, 0, 7],
       [8, 3, 3, 2]])

In [41]: data.reshape(-1,j).mean(axis=1).reshape(data.shape[0],-1)
Out[41]: 
array([[ 8. ,  4.5],
       [ 6.5,  3. ],
       [ 4.5,  3.5],
       [ 5.5,  2.5]])
like image 109
tzelleke Avatar answered Sep 24 '22 01:09

tzelleke