Average value for two-dimensional numpy array

I want to find the average value of a 2D block in NumPy. For simplicity, suppose the array looks like this:

array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]]) 

I want to divide this array into 3 blocks of 2x4 size, and then find the average of all three blocks (so that the shape of the average is 2x4. The first block is formed of the first 4 columns, the next four columns, etc. Thus, my blocks :

 array([[0, 1, 2, 3], [12, 13, 14, 15]]) array([[ 4, 5, 6, 7], [16, 17, 18, 19]]) array([[ 8, 9, 10, 11], [20, 21, 22, 23]]) 

I can use a loop for this, but I feel that it would be better to first convert this array to a 3D array using reshape and then use the mean method in the 3D array along the third axis. This may be like this question .

I would be grateful if someone could provide me with:

one). The appropriate Pythonic command is to make the block medium without even converting to 3D if such a trick exists.

2). If not the appropriate Pythonic command to convert 2D to 3D.

3). Analyze if it would be more efficient (in terms of space) to do this with a loop or with the command above.

+7
source share
2 answers

Numpy methods will almost always beat python loops, so I will skip your 1.

As for 2, in this particular case, the following work is performed:

 a = np.array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]]) a = a.reshape(2, 3, 4) >>> a array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) >>> np.mean(a, axis=1) array([[ 4., 5., 6., 7.], [ 16., 17., 18., 19.]]) 

The trick is in reshape . For the general case, when you need n column blocks, the following option

 a = a.reshape((a.shape[0], -1, n)) 

Your concerns regarding 3 are largely unfounded. reshape returns a representation of the original array, not a copy, so converting to 3D requires changing the shape and strides attributes of the array, without having to copy any actual data.

EDIT To make sure reformatting does not copy the array, but returns a view, change the form as

 a.shape = a = a.reshape((a.shape[0], -1, n)) 

The example in docs matches the lines:

 >>> a = np.arange(12).reshape(3,4) >>> b = aT >>> b.shape = (12,) Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: incompatible shape for a non-contiguous array 

In general, there are only problems if you did transpose , rollaxis , swapaxes or the like in your array.

+4
source

I can answer ur number 1).

 vstack([mean(a[:,4*i:4*(i+1)],axis=1) for i in range(3)]).T 
-one
source

All Articles