Numpy: How masking in index works?

Question:

I’m a new learner of Python.
May I get some help on NumPy ?
How does the Masking in index selection works?

My first masking is like

array[mask][:,0]
# array([1905., 1920., 1929., 1938., 1948., 1965., 2002., 2008., 2016.,2022.]

Which I understand how it works using index
But my second

numpy.array[mask1][:,mask2]
# [[1905.    18.9]
#  [1920.    20.6]
#  [1929.    18.9]
#  [1938.     nan]
#  [1948.    19.9]
#  [1965.     nan]
#  [2002.     nan]
#  [2008.    19.5]
#  [2016.    19.4]
#  [2022.     nan]]
# [[1905.    18.9]
#  [1920.    20.6]
#  [1929.    18.9]
#  [1938.     nan]
#  [1948.    19.9]
#  [1965.     nan]
#  [2002.     nan]
#  [2008.    19.5]
#  [2016.    19.4]
#  [2022.     nan]]

I don’t understand why it returns a (10,2) shape array

I hope I asking questions in the right way, sorry for any inconvenience.

Asked By: Lewise Ham

||

Answers:

Masks basically say select that column, row, or axis. There are two main ways that are used in numpy.

  • Boolean [True, False, True, …] with the same shape as the array or an axis
  • Indices [2,2,0] of arbitrary shape that say, select axis/value 2, 2 and 0 in that order.

np.array([1,2,3])[[2,2,0,0]] -> [3,3,1,1] note the double brackets, it says select indices [2,2,0,0] from axis 0

In case of boolean masks you get a shape the sum of the True values in each mask.

In case of index masks your results are in the shape of the masks.
numpy.array[mask1][:,mask2] will have roughly the shape (mask1.shape, mask2.shape), but of course these could be more than 2 dimensions.

Answered By: Daraan
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.