Home > Back-end >  Numpy array masking
Numpy array masking

Time:12-18

I would like to ask a question with numpy array masking.

For instance given the array below:

a b
1 2
3 4
5 6
6 5

I have another array which is

a b
1 2
3 4

I want to compare two arrays and find the index numbers of the different rows from first array.

For example: the table 1 different than table 2 in rows number 3,4 which yields:

a b
5 6
6 5

and index of the rows in array1 are 2 and 3.

I have tried bunch of methods but could not reach the final answer. I would appreciate for your answer

CodePudding user response:

Using np.where is an option

import numpy as np

def getIndices(a,b):
    return np.where(np.logical_or(a[:,0] != b[:,0], a[:,1] != b[:,1]))[0]

a = np.array([[1,2],[3,4],[5,6],[1,0]])
b = np.array([[1,2],[3,5],[5,6]])

# set both arrays to the same shape by adding different rows by adding one to the values of the rows of the smaller array
# There is probably a way better way to do this
if len(a) > len(b):
    r = len(a)-len(b)
    for i in range(r):
        b = np.append(b, [[a[len(b)][0] 1,a[len(b)][1] 1]], axis=0)
elif len(a) < len(b):
    r = len(b)-len(a)
    for i in range(r):
        a = np.append(a, [[b[len(a)][0] 1,b[len(a)][1] 1]], axis=0)

# get the indices of the rows where the values are different
indices = getIndices(a,b)
print(indices)

Output:

[1 3]

CodePudding user response:

We can do a vectorized norm difference to compute the similarity between the two matrix and then choose the non-zero indices,

np.where(~(np.abs(a - b[:,None]).sum(-1)==0).any(0))
  • Related