Indexing multiple items in a multidimensional array

I have a 3D Numpy array and would like to take the average value along one axis, given some elements from two other dimensions.

This is an example code depicting my problem:

import numpy as np myarray = np.random.random((5,10,30)) yy = [1,2,3,4] xx = [20,21,22,23,24,25,26,27,28,29] mymean = [ np.mean(myarray[t,yy,xx]) for t in np.arange(5) ] 

However, this leads to:

 ValueError: shape mismatch: objects cannot be broadcast to a single shape 

Why does indexing, for example, myarray [:, [1,2,3,4], [1,2,3,4]] work, but not my code above?

+4
source share
3 answers

Here's how you imagine more than one dimension:

 >>> np.mean(myarray[np.arange(5)[:, None, None], np.array(yy)[:, None], xx], axis=(-1, -2)) array([ 0.49482768, 0.53013301, 0.4485054 , 0.49516017, 0.47034123]) 

When you use fancy indexing, that is, a list or array as an index, more than one dimension, numpy passes these arrays into a general form and uses them to index the array. You need to add these additional sizes of length 1 at the end of the first indexing arrays for the broadcast system to work correctly. Here are the rules of the game .

+4
source

Since you are using sequential elements, you can use a slice:

 import numpy as np myarray = np.random.random((5,10,30)) yy = slice(1,5) xx = slice(20, 30) mymean = [np.mean(myarray[t, yy, xx]) for t in np.arange(5)] 
+3
source

To answer the question of why it doesn't work: when using lists / arrays as indexes, Numpy uses a different set of indexing semantics than when using slices. You can see the full version of the documentation and, as the page says, it can be somewhat overwhelming.

If you want to do this for inconsistent elements, you should check out this complex indexing mechanism.

+3
source

Source: https://habr.com/ru/post/1499176/


All Articles