Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Find numpy array with don't cares in a numpy array

I would like to find a numpy array with don’t cares like:

b = np.array(
    [
        [0,0,-1,-1]
    ]
    , dtype=np.int8
)

where -1 is the don’t care, and find it in arrays like:

a = np.array(
    [
        [1,2,0,0],
        [0,1,2,0],
        [0,0,1,2],
        [2,0,0,1],
        [3,4,0,0],
        [0,3,4,0],
        [0,0,3,4],
        [4,0,0,3]
    ]
    , dtype=np.int8
)

and return the row index’s 2 and 6 for the above sample

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

the a array are normally around 1000 rows shape(~1000, 4)

note the b array can have don’t cares any where or none, examples:

b = np.array(
    [
        [0,3,4,-1]
    ]
    , dtype=np.int8
)

# --OR--

b = np.array(
    [
        [2,-1,0,1]
    ]
    , dtype=np.int8
)

# --OR--

b = np.array(
    [
        [2,-1,-1,-1]
    ]
    , dtype=np.int8
)
# etc...

>Solution :

You could replace values in the main array with -1 where you have -1 in your sub array. Then you can just find where b==a

np.where(np.all(np.where((b==-1),-1,a)==b, axis=1))

Output

(array([2, 6], dtype=int64),)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading