【发布时间】:2014-06-15 20:48:50
【问题描述】:
我有两个浮点数列表,我想计算它们之间的差值。
我最初使用 numpy 编写了以下代码:
aprows = allpoints.view([('',allpoints.dtype)]*allpoints.shape[1])
rprows = toberemovedpoints.view([('',toberemovedpoints.dtype)]*toberemovedpoints.shape[1])
diff = setdiff1d(aprows, rprows).view(allpoints.dtype).reshape(-1, 2)
这适用于整数之类的东西。如果 2d 点的浮点坐标是某些几何计算的结果,则存在有限精度和舍入误差的问题,导致设置的差异错过了一些等式。现在我使用了慢得多的方法:
diff = []
for a in allpoints:
remove = False
for p in toberemovedpoints:
if norm(p-a) < 0.1:
remove = True
if not remove:
diff.append(a)
return array(diff)
但是有没有办法用 numpy 写这个并恢复速度?
请注意,我希望剩余的点仍然具有完整的精度,所以首先对数字进行四舍五入然后做一个设定的差异可能不是前进的方向(或者是吗?:))
编辑添加了一个基于 scipy.KDTree 的解决方案,似乎可行:
def remove_points_fast(allpoints, toberemovedpoints):
diff = []
removed = 0
# prepare a KDTree
from scipy.spatial import KDTree
tree = KDTree(toberemovedpoints, leafsize=allpoints.shape[0]+1)
for p in allpoints:
distance, ndx = tree.query([p], k=1)
if distance < 0.1:
removed += 1
else:
diff.append(p)
return array(diff), removed
【问题讨论】:
-
from scipy.spatial import cKDTree as KDTree是一个更快的 cython KDtree。
标签: python numpy floating-point-precision set-difference