【问题标题】:Numpy to get the exact arguments of duplicated elements in a 2D arrayNumpy 获取二维数组中重复元素的确切参数
【发布时间】:2020-03-03 04:10:09
【问题描述】:

我有两个二维数组 ab。我想在b 中找到a精确 索引。我遵循了here提出的解决方案。

问题是我的数组包含重复项,您可以在此处看到:

# The shape of b is (50, 2)
b = np.array([[ 0,  1],[ 2,  3],[ 4,  5],[ 6,  7], [ 0,  1],
             [10, 11], [12, 13], [14, 15], [16, 17], [10, 11],
             [20, 21], [22, 23], [24, 25], [26, 27], [20, 21],
             [30, 31], [32, 33], [34, 35], [36, 37], [30, 31],
             [40, 41], [42, 43], [44, 45], [46, 47], [40, 41],
             [50, 51], [52, 53], [54, 55], [56, 57], [50, 51],
             [60, 61], [62, 63], [64, 65], [66, 67], [60, 61],
             [70, 71], [72, 73], [74, 75], [76, 77], [70, 71],
             [80, 81], [82, 83], [84, 85], [86, 87], [80, 81],
             [90, 91], [92, 93], [94, 95], [96, 97], [90, 91]])

# The shape of a is (20,2)
a = np.array([[ 0,  1],[ 2,  3], [ 4,  5],[ 6,  7],[ 0,  1],
       [50, 51],[52, 53], [54, 55], [56, 57], [50, 51],
       [20, 21], [22, 23], [24, 25], [26, 27], [20, 21],
       [70, 71], [72, 73], [74, 75], [76, 77], [70, 71]])

现在当我尝试这样的事情时:

# See the link above approach 2
def view1D(a, b): # a, b are arrays
    a = np.ascontiguousarray(a)
    b = np.ascontiguousarray(b)
    void_dt = np.dtype((np.void, a.dtype.itemsize * a.shape[1]))
    return a.view(void_dt).ravel(),  b.view(void_dt).ravel()

def argwhere_nd_searchsorted(a,b):
    A,B = view1D(a,b)
    sidxB = B.argsort()
    mask = np.isin(A,B)
    cm = A[mask]
    idx0 = np.flatnonzero(mask)
    idx1 = sidxB[np.searchsorted(B,cm, sorter=sidxB)]
    return idx0, idx1 # idx0 : indices in A, idx1 : indices in B

args0, args1 = argwhere_nd_searchsorted(a,b)

结果:

#args0
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,17, 18, 19])

#args1
 array([ 0,
  1,
  2,
  3,
  0, # this sould be 4
 25,
 26,
 27,
 28,
 25, # this sould be 29
 10,
 11,
 12,
 13,
 10,# this should be 14
 39,# this should be 35
 36,
 37,
 38,
 39])
# if we check
np.equal(b[args1],a).all() # This returns True

如您所见,args1 突出显示的索引中的问题重复出现。我的预期结果显示在注释行中。

感谢任何帮助

【问题讨论】:

  • 有重复。那么,如何进行比赛呢?你将如何决定与哪一个匹配?
  • @Divakar:假设我想更新数组b 如下:b[args1] = another array 这将更新重复点的一个实例两次,而其他的保持不变。有些我正在寻找这个问题的解决方案。
  • 不要以为你明白我的意思。你在问 - I want to match rows of array a in b and get a row indices map using numpy.。现在 a 和 b 中都有重复的行。因此,我之前的评论。
  • @Divakar:我更新了这个问题。我的意思是,在代码args1 中不应包含重复的第一个重复点的索引
  • 不确定,但您的问题可能会通过掩码解决。所以,你可以使用isin_nd来获取掩码,它可以用来掩码并赋值给b

标签: python arrays numpy


【解决方案1】:

我们可以再添加一列 ID 来表示行中的重复项,然后使用相同的步骤。我们将使用 pandas 来获取这些 ID,这样更容易。因此,只需执行 -

import pandas as pd

def assign_duplbl(a):
    df = pd.DataFrame(a)
    df['num'] = 1
    return df.groupby(list(range(a.shape[1]))).cumsum().values

a1 = np.hstack((a,assign_duplbl(a)))
b1 = np.hstack((b,assign_duplbl(b)))
args0, args1 = argwhere_nd_searchsorted(a1,b1)

【讨论】:

  • 答案的第一个版本工作正常。但是编辑过的,我得到一个错误:range' object is not callable
  • @IamNotaMathematician 请查看编辑后的代码。
  • 作为建议,您还可以考虑增强您的答案here 以解决重复问题。
  • @IamNotaMathematician 处理重复项的方法可能因具体情况而异。就像遇到第一个重复项后有人可能想跳过一样。在您的情况下,您要考虑它们出现的顺序。所以,我会离开它。它已经通过问题与该问答相关联。所以,我认为这已经足够了。
  • 是的,我同意,但在我的情况下,我有一个带有可选参数 consider_duplicatedargwhere_nd_sortedsearch 函数版本,默认为 False
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-05-14
  • 2020-02-16
  • 2017-02-25
  • 2018-03-30
  • 2021-07-20
  • 2021-07-10
  • 1970-01-01
相关资源
最近更新 更多