0

For example, when i want to use numpy masked array as index, i got some trouble:

import numpy as np
a=np.array([0,1,2,3,4,5])
b=np.array([[0,1],[3,5]])
>>> a[b]
   array([[0, 1],
          [3, 5]]

well, THE PROBLEM is number 3 in b is not what i want. The result i want to geti is like

a[b]=array([[0, 1],
              [5]])

so i used the numpy.ma:

b=np.ma.masked_equal(b,3)
>>> b
masked_array(data =
 [[0 1]
  [-- 5]],
         mask =
 [[False False]
 [ True False]],
   fill_value = 3)
>>> a[b]
array([[0, 1],
   [3, 5]])

As we can see, the masked value still works as index, is there a way to fix it ?

1
  • What does 'ignore it' mean, in this context? In general masked arrays only work with masked array functions and methods. They aren't a drop in substitute for regular arrays. But, with more information we can suggest ways to use it, or something equivalent. Commented May 12, 2017 at 18:16

1 Answer 1

1
>>> np.ma.masked_array(a[b.data], b.mask)
masked_array(data =
 [[0 1]
 [-- 5]],
             mask =
 [[False False]
 [ True False]],
       fill_value = 999999)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this code snippet, which might provide some limited, immediate help. A proper explanation would greatly improve its long-term value by showing why this is a good solution to the problem, and would make it more useful to future readers with other, similar questions. Please edit your answer to add some explanation, including the assumptions you've made.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.