python - 仅删除该行 3D numpy 数组中包含重复项的行
问题描述
我有一个像这样的 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 中非常慢,这需要很多时间。我的原始数组也包含浮点数。任何有更好的解决方案或可以帮助我矢量化此功能的人?
解决方案
沿着每个2D block
ie的行对其进行排序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]]])
推荐阅读
- jmeter - 我们可以在 JMeter 的 IF 控制器中使用 CSV 数据集配置吗
- laravel - 使用 flush() 或 forget() 时,Laravel Session 不会被删除
- reactjs - 如何使用 React Hooks 在不同页面中显示嵌套路由..?
- javascript - 如何访问数组中对象的变量和方法 [已解决]
- linux - 远程计算机上的 ZSH 环境损坏,通过 ssh 登录后立即崩溃
- excel - 如何读取现有的 Excel 电子表格单元格(或多个单元格)
- pandas - 如何分组和合并这些火花数据框组的行
- python - NetworkX 包生成的图可以处理的具有属性的节点和边的最大数量是多少?
- prometheus - 如何使用 prometheus api 获取实例的所有指标?
- react-native - 无法读取未定义评估 App.js 加载 App.js 的属性“导航”