0

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.

2
  • How do your masks look like? mask.shape? Commented Oct 9, 2022 at 11:07
  • mask1 (107,) mask2 (10,2) Commented Oct 9, 2022 at 11:25

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.