基于一列中的公共值从两个或多个2d numpy数组创建交集

2024-09-29 21:43:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我有3个纽比重排与以下结构。 第一列是某个位置(整数),第二列是分数(浮点)。在

输入:

a = [[1, 5.41],
     [2, 5.42],
     [3, 12.32],
     dtype=[('position', '<i4'), ('score', '<f4')])
     ]

b = [[3, 8.41],
     [6, 7.42],
     [4, 6.32],
     dtype=[('position', '<i4'), ('score', '<f4')])
     ]

c = [[3, 7.41],
     [7, 6.42],
     [1, 5.32],
     dtype=[('position', '<i4'), ('score', '<f4')])
     ]

所有3个数组包含相同数量的元素。
我正在寻找一种有效的方法,将这三个2d数组组合成一个基于position列的数组。在

上述示例的输出arary应如下所示:

输出:

^{pr2}$

只有位置为3的行在输出数组中,因为这个位置出现在所有3个输入数组中。在

更新:我的天真方法是以下步骤:

  1. 创建我的3个输入数组的第一列的向量。在
  2. 使用intersect1D得到这3个向量的交集。在
  3. 以某种方式检索所有3个输入数组的向量索引。在
  4. 使用从3个输入数组中筛选出的行创建新数组。在

更新2: 每个位置值可以在一个、两个或所有三个输入数组中。在我的输出数组中,我只想包含出现在所有3个输入数组中的位置值的行。在


Tags: 方法元素数量position整数数组结构向量
1条回答
网友
1楼 · 发布于 2024-09-29 21:43:09

这里有一种方法,我相信它应该相当快。我想你要做的第一件事就是计算每个位置的出现次数。此函数将处理:

def count_positions(positions):
    positions = np.sort(positions)
    diff = np.ones(len(positions), 'bool')
    diff[:-1] = positions[1:] != positions[:-1]
    count = diff.nonzero()[0]
    count[1:] = count[1:] - count[:-1]
    count[0] += 1
    uniqPositions = positions[diff]
    return uniqPositions, count

现在使用上面的函数形式,您只需要选择出现3次的位置:

^{pr2}$

我们将使用搜索排序,以便对a b和c进行排序:

a.sort(order='position')
b.sort(order='position')
c.sort(order='position')

现在,我们可以通过用户搜索排序来查找每个数组中的位置,从而找到我们的每个uniqpo:

new_array = np.empty((len(uinqPos), 4))
new_array[:, 0] = uinqPos
index = a['position'].searchsorted(uinqPos)
new_array[:, 1] = a['score'][index]
index = b['position'].searchsorted(uinqPos)
new_array[:, 2] = b['score'][index]
index = c['position'].searchsorted(uinqPos)
new_array[:, 3] = c['score'][index]

使用字典可能有一个更优雅的解决方案,但我首先想到了这个,所以我将把它留给其他人。在

相关问题 更多 >

    热门问题