python - 仅删除在 3D numpy 数组的该行中包含重复项的行

标签 python numpy multidimensional-array

我有一个像这样的 3D numpy 数组:

>>> a
array([[[0, 1, 2],
        [0, 1, 2],
        [6, 7, 8]],
       [[6, 7, 8],
        [0, 1, 2],
        [6, 7, 8]],
       [[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]]])

我只想删除那些本身包含重复项的行。例如输出应该是这样的:

>>> remove_row_duplicates(a)
array([[[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]]])

这是我正在使用的函数:

delindices = np.empty(0, dtype=int)

for i in range(len(a)):
    _, indices = np.unique(np.around(a[i], decimals=10), axis=0, return_index=True)

    if len(indices) < len(a[i]):

        delindices = np.append(delindices, i) 

a = np.delete(a, delindices, 0)

这非常有效,但问题是现在我的数组形状类似于 (1000000,7,3)。 for 循环在 python 中非常慢,这需要很多时间。我的原始数组也包含 float 。谁有更好的解决方案或谁可以帮助我矢量化此功能?

最佳答案

为每个 2D block 沿行排序,即沿 axis=1 ,然后沿连续行查找匹配行,最后查找 any 沿相同的 axis=1 -

匹配
b = np.sort(a,axis=1)
out = a[~((b[:,1:] == b[:,:-1]).all(-1)).any(1)]

带有解释的示例运行

输入数组:

In [51]: a
Out[51]: 
array([[[0, 1, 2],
        [0, 1, 2],
        [6, 7, 8]],

       [[6, 7, 8],
        [0, 1, 2],
        [6, 7, 8]],

       [[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]]])

代码步骤:

# Sort along axis=1, i.e rows in each 2D block
In [52]: b = np.sort(a,axis=1)

In [53]: b
Out[53]: 
array([[[0, 1, 2],
        [0, 1, 2],
        [6, 7, 8]],

       [[0, 1, 2],
        [6, 7, 8],
        [6, 7, 8]],

       [[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]]])

In [54]: (b[:,1:] == b[:,:-1]).all(-1) # Look for successive matching rows
Out[54]: 
array([[ True, False],
       [False,  True],
       [False, False]])

# Look for matches along each row, which indicates presence
# of duplicate rows within each 2D block in original 2D array
In [55]: ((b[:,1:] == b[:,:-1]).all(-1)).any(1)
Out[55]: array([ True,  True, False])

# Invert those as we need to remove those cases
# Finally index with boolean indexing and get the output
In [57]: a[~((b[:,1:] == b[:,:-1]).all(-1)).any(1)]
Out[57]: 
array([[[0, 1, 2],
        [3, 4, 5],
        [6, 7, 8]]])

关于python - 仅删除在 3D numpy 数组的该行中包含重复项的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51336934/

相关文章:

比较相同的单词

python - 基于多个条件处理 numpy 数组的更有效方法是什么?

javascript - 如何在 JavaScript 中访问嵌套数组中的属性?

python - 将树状图保存为 newick 格式

python - np.array 中行提取的时间

python - 我怎样才能产生一个很好的 numpy 矩阵输出?

python - Pandas 和 HDF5 中的文件大小减小

python - 从嵌套列表创建字典的有效方法

python - Python中测试修改文件时间是否在2天内

python - 为什么 numpy 的 fromiter 函数需要指定 dtype 而其他数组创建例程不需要?