python - 删除几乎相同的行 numpy 数组
问题描述
如果我有以下 numpy 数组:
import numpy as np
arr = np.array([[285, 849],
[399, 715],
[399, 716],
[400, 715],
[400, 716]])
我将如何删除几乎相同的行?我不介意我是否以行, 或[399, 715]
.[399, 716]
结尾。例如,作为最终结果,我想得到:[400, 715]
[400, 716]
out = remove_near_identical(arr)
print(out)
[[285 849]
[399 715]]
解决方案
仅基于距离的方法:
import numpy as np
from scipy.spatial.distance import deist
arr = np.array([[285, 849],
[399, 715],
[399, 716],
[400, 715],
[400, 716]])
# get distances between every set of points
dists = cdist(arr, arr)
dists[np.isclose(dists, 0)] = np.inf # set 0 (self) distances to be large, ie. ignore
# get indices of points less than some threshold value (too close)
i, j = np.where(dists <= 1)
# get the unique indices from either i or j
# and delete all but one of these points from the original array
np.delete(arr, np.unique(i)[1:], axis=0)
>>> array([[285, 849],
[399, 715]])
推荐阅读
- python-3.x - 如何使用 groupby 计算 vwap(交易量加权平均价格)?
- php - 如何将 SQL INSERT INTO SELECT 与 codeigniter 一起使用
- excel - 如何从excel中捕获outlook中的自定义按钮单击事件?
- java - Intellij 可以构建我的项目,但 javac 无法编译/查找符号
- c++ - 如何修复 OpenCV 断言失败错误?
- javascript - 添加对象的元素并存储在对象中
- ios - TableView 框架更改 - 单元格布局错误?
- kubernetes - 如何为 Kubernetes 中的特定部署允许端口转发?
- php - 如何使用省略号构建分页
- java - 创建插件/包依赖映射