2

I am a newbie in numpy. I have 2 2d arrays. I would like to find indices of arr2 in arr1. Please advice me.

    arr1 = [[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9],
            [4, 5, 6],
            [1, 2, 3]]

    arr2 = [[1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]]
    
    desired_output = [0, 1, 2, 1, 0]

1 Answer 1

2

One way to achieve this.

If any row of arr1 were not found in arr2, then at that location in pos will have value -1 for simplicity.

This heavily uses numpy broadcasting and indexing. Feel free to ask for further clarifications.

Original example:

import numpy as np
arr1 = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9],
                 [4, 5, 6],
                 [1, 2, 3]])
arr2 = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])

inds = arr1 == arr2[:, None]
row_sums = inds.sum(axis = 2)
i, j = np.where(row_sums == 3) # Check which rows match in all 3 columns

pos = np.ones(arr1.shape[0], dtype = 'int64') * -1
pos[j] = i
pos
array([0, 1, 2, 1, 0])

Example 2:

import numpy as np
arr1 = np.array([[1, 2, 4],
                 [4, 5, 6],
                 [7, 8, 9],
                 [4, 1, 6],
                 [1, 2, 3]])
arr2 = np.array([[1, 2, 3],
                 [4, 5, 6],
                 [7, 8, 9]])

inds = arr1 == arr2[:, None]
row_sums = inds.sum(axis = 2)
i, j = np.where(row_sums == 3)

pos = np.ones(arr1.shape[0], dtype = 'int64') * -1
pos[j] = i
pos
array([-1,  1,  2, -1,  0])

If you have more number of columns just change the line i, j = np.where(row_sums == 3) to i, j = np.where(row_sums == arr1.shape[1]).

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.