简体   繁体   中英

Numpy array masking

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

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]

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))

Maybe you want something like this?

import numpy as np

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

a = np.array([1, 3])
b = np.array([2, 4])
ab_2 = np.stack([a, b]).T

# equal_cols[i, j, k] indicates whether ab_i[i, k] and ab_2[j, k] are equal.
equal_cols = ab_1[:, None] == ab_2[None]
for i in range(ab_1.shape[0]):
    for j in range(ab_2.shape[0]):
        for k in range(ab_1.shape[1]):
            assert equal_cols[i, j, k] == (ab_1[i, k] == ab_2[j, k])

# exact_matches[i, j] indicates whether ab_1[i] and ab_2[j] are an exact match.
exact_matches = np.all(equal_cols, axis=-1)
for i in range(ab_1.shape[0]):
    for j in range(ab_2.shape[0]):
        assert exact_matches[i, j] == (np.all(ab_1[i] == ab_2[j]))

# at_least_one_match[i] indicates whether ab_1[i] has at least one exact match in ab_2.
at_least_one_match = np.any(exact_matches, axis=-1)
no_matches = ~at_least_one_match

idxs = np.nonzero(no_matches)[0]
print(idxs)
# array([2, 3])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM