使用欧几里得距离添加元素
我有一个有关凸壳的问题:
我有一个包含凸壳顶点的字典,所以它是一个键和一个具有不同数字的二维numpy阵列。问题是我希望所有人都有相同的数字。
我可以通过将点“添加”到凸船体中来实现。可以在欧几里得距离最大的凸船体点之间插入该点。我会在循环中添加这些点,直到达到所需的金额(n = 10):
'key1': {([2.7, 3.1],
[2.8, 2.6])}
'key2': {([4.7, 5.2],
[3.8, 1.6], [4.8, 0.6])}
'key3': {([2.7, 3.1],
[2.8, 2.6], [1.7, 4.1])}
我尝试这样尝试:
enter code hfor x in hullpoints:
while len(hullpoints) < 10:
new_point = (x[0] + x[1])/2
print(new_point)
hullpoints = hullpoints.np.append(new_point)
但是这只是将我送入无尽的循环。
我也尝试与Eucledian距离一起玩:
dist = cdist(hullpoints, hullpoints, metric='euclidean')
bestpair = np.unravel_index(hdist.argmax(), hdist.shape)
new_point = (hullpoints[bestpair[0]] + hullpoints[bestpair[1]])/2
但是我不知道该从这里去哪里。 我希望有人可以帮助我。
br
I have a question regarding convex hulls:
I have a dictionary containing the vertices of convex hulls, so it's a key and a 2-dimensional numpy array with varying numbers. The problem is that I would like all of them have the same number.
This I could achieve by "adding" points to the convex hull. This points could be inserted between the points of the convex hull with the largest euclidean distance. I would add those points in a loop until I would reach the desired amount (N=10):
'key1': {([2.7, 3.1],
[2.8, 2.6])}
'key2': {([4.7, 5.2],
[3.8, 1.6], [4.8, 0.6])}
'key3': {([2.7, 3.1],
[2.8, 2.6], [1.7, 4.1])}
I tried it like this:
enter code hfor x in hullpoints:
while len(hullpoints) < 10:
new_point = (x[0] + x[1])/2
print(new_point)
hullpoints = hullpoints.np.append(new_point)
but this just sends me in an endless loop.
I tried playing around with eucledian distances as well:
dist = cdist(hullpoints, hullpoints, metric='euclidean')
bestpair = np.unravel_index(hdist.argmax(), hdist.shape)
new_point = (hullpoints[bestpair[0]] + hullpoints[bestpair[1]])/2
but I don't know where to go from here.
I hope someone can help me with this.
BR
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如评论中所述,影响
len(hullpoints)
的线路不在循环之外,因此循环将是无限的。您可以通过缩进更新行来修复它(注意:其他线路的尝试修复):
但是,仍然存在问题:所有其他点都将是相同的值:
x [0 ]
和x [1]
。一个也许更简单的方法是:
这可以替换每个段
x
通过肢体之间的线性间距。如果您确定X
始终具有2个组件(例如在示例中),则可以编写np.linspace(*x,n)
而不是np.linspace (x [0],x [-1],n)
。结果,带有示例中的值,您可以得到:
As said in the comment, the line that affects
len(hullpoints)
is outside the while loop, so the loop will be endless.You can fix it by indenting the update line (note: attempted fix for the other lines as well):
However, there are still problems with this: all additional points will be all the same value: the midpoint between
x[0]
andx[1]
.A perhaps simpler approach is:
This replaces each segment
x
by an even linear spacing between the extremities. If you are surex
has always 2 components (like in your example), you can writenp.linspace(*x, n)
instead ofnp.linspace(x[0], x[-1], n)
.Result, with the values from your example, you get: