python – 附近scipy.spatial.Delaunay遗漏的点数
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 附近scipy.spatial.Delaunay遗漏的点数,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2618字,纯文字阅读大概需要4分钟。
内容图文
![python – 附近scipy.spatial.Delaunay遗漏的点数](/upload/InfoBanner/zyjiaocheng/730/14198bb173f7468ab96849f83882069a.jpg)
当比较scipy(0.9.0)和matplotlib(1.0.1)Delaunay三角测量程序时,我注意到一个无法解释的行为.我的点是存储在numpy.array中的UTM坐标([[easting,northing],[easting,northing],[easting,northing]]). Scipy的边缘缺少一些我的观点,而matplotlib就在那里.有没有修复,或者我做错了什么?
import scipy
import numpy
from scipy.spatial import Delaunay
import matplotlib.delaunay
def delaunay_edges(points):
d = scipy.spatial.Delaunay(points)
s = d.vertices
return numpy.vstack((s[:,:2], s[:,1:], s[:,::-2]))
def delaunay_edges_matplotlib(points):
cens, edges, tri, neig = matplotlib.delaunay.delaunay(points[:,0], points[:,1])
return edges
points = numpy.array([[500000.25, 6220000.25],[500000.5, 6220000.5],[500001.0, 6220001.0],[500002.0, 6220003.0],[500003.0, 6220005.0]])
edges1 = delaunay_edges(points)
edges2 = delaunay_edges_matplotlib(points)
numpy.unique(edges1).shape # Some points missing, presumably nearby ones
numpy.unique(edges2).shape # Includes all points
解决方法:
scipy.spatial.Delaunay的这种行为可能与浮点运算的impresicion有关.
您可能知道,scipy.spatial.Delaunay使用C qhull库来计算Delaunay三角剖分.反过来,Qhull是Quickhull algorithm的实现,作者在this论文(1)中详细描述了这一点.您也可能知道计算机中使用的浮点运算是使用IEEE 754标准执行的(例如,您可以在Wikipedia中阅读它).根据该标准,每个有限数最简单地由三个整数描述:s =符号(零或一),c =有效数(或’系数’),q =指数.用于表示这些整数的位数因数据类型而异.因此,显而易见的是,数字轴上浮点数分布的密度不是恒定的 – 数字越大,分布越松散.即使使用Google计算器也可以看到 – 你可以从3333333333333334和get 0中减去3333333333333333.这是因为3333333333333333和3333333333333334都四舍五入到相同的浮点数.
现在,了解舍入错误,我们可能想要阅读题为“使用impresicion进行复制”的论文(1)的第4章.在本章中,描述了一种处理舍入误差的算法:
Quickhull partitions a point and determines its horizon facets by computing
whether the point is above or below a hyperplane. We have assumed that
computations return consistent results ... With floating-point arithmetic, we
cannot prevent errors from occurring, but we can repair the damage after
processing a point. We use brute force: if adjacent facets are nonconvex, one of
the facets is merged into a neighbor. Quickhull merges the facet that minimizes
the maximum distance of a vertex to the neighbor.
这就是可能发生的事情 – Quickhull无法区分两个附近的点,因此它合并了两个方面,从而成功地消除了其中一个方面.例如,要消除这些错误,您可以尝试移动坐标原点:
co = points[0]
points = points - co
edges1 = delaunay_edges(points)
edges2 = delaunay_edges_matplotlib(points)
print numpy.unique(edges1)
>>> [0 1 2 3 4]
print numpy.unique(edges2)
>>> [0 1 2 3 4]
这将计算移动到浮点数相当密集的区域.
内容总结
以上是互联网集市为您收集整理的python – 附近scipy.spatial.Delaunay遗漏的点数全部内容,希望文章能够帮你解决python – 附近scipy.spatial.Delaunay遗漏的点数所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。