繁体   English   中英

Numpy 数组掩码

[英]Numpy array masking

我想问一个关于 numpy array masking 的问题。

例如给出下面的数组:

a b
1 2
3 4
5 6
6 5

我有另一个数组是

a b
1 2
3 4

我想比较两个数组并从第一个数组中找到不同行的索引号。

例如:表 1 与表 2 的行号 3,4 不同,结果为:

a b
5 6
6 5

array1 中行的索引是 2 和 3。

我尝试了很多方法,但无法得出最终答案。 我会很感激你的回答

使用np.where是一个选项

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)

输出:

[1 3]

我们可以做一个vectorized norm差来计算两个矩阵之间的相似度,然后选择非零索引,

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

也许你想要这样的东西?

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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