使用欧几里得距离添加元素

发布于 2025-02-14 00:06:00 字数 833 浏览 0 评论 0原文

我有一个有关凸壳的问题:

我有一个包含凸壳顶点的字典,所以它是一个键和一个具有不同数字的二维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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

全部不再 2025-02-21 00:06:00

如评论中所述,影响len(hullpoints)的线路不在循环之外,因此循环将是无限的。

您可以通过缩进更新行来修复它(注意:其他线路的尝试修复):

while len(hullpoints) < 10:
    x = hullpoints
    new_point = (x[0] + x[1])/2
    print(new_point)
    hullpoints = np.append(hullpoints, new_point)

但是,仍然存在问题:所有其他点都将是相同的值:x [0 ]x [1]

一个也许更简单的方法是:

# reproducible init
ch = {
    '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]),
}

n = 10
ch2 = {
    k: tuple([np.linspace(x[0], x[-1], n) for x in v])
    for k, v in ch.items()
}

这可以替换每个段x通过肢体之间的线性间距。如果您确定X始终具有2个组件(例如在示例中),则可以编写np.linspace(*x,n)而不是np.linspace (x [0],x [-1],n)

结果,带有示例中的值,您可以得到:

>>> ch2
{'key1': (array([2.7       , 2.74444444, 2.78888889, 2.83333333, 2.87777778,
         2.92222222, 2.96666667, 3.01111111, 3.05555556, 3.1       ]),
  array([2.8       , 2.77777778, 2.75555556, 2.73333333, 2.71111111,
         2.68888889, 2.66666667, 2.64444444, 2.62222222, 2.6       ])),
 'key2': (array([4.7       , 4.75555556, 4.81111111, 4.86666667, 4.92222222,
         4.97777778, 5.03333333, 5.08888889, 5.14444444, 5.2       ]),
  array([3.8       , 3.55555556, 3.31111111, 3.06666667, 2.82222222,
         2.57777778, 2.33333333, 2.08888889, 1.84444444, 1.6       ]),
  array([4.8       , 4.33333333, 3.86666667, 3.4       , 2.93333333,
         2.46666667, 2.        , 1.53333333, 1.06666667, 0.6       ])),
 'key3': (array([2.7       , 2.74444444, 2.78888889, 2.83333333, 2.87777778,
         2.92222222, 2.96666667, 3.01111111, 3.05555556, 3.1       ]),
  array([2.8       , 2.77777778, 2.75555556, 2.73333333, 2.71111111,
         2.68888889, 2.66666667, 2.64444444, 2.62222222, 2.6       ]),
  array([1.7       , 1.96666667, 2.23333333, 2.5       , 2.76666667,
         3.03333333, 3.3       , 3.56666667, 3.83333333, 4.1       ]))}

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):

while len(hullpoints) < 10:
    x = hullpoints
    new_point = (x[0] + x[1])/2
    print(new_point)
    hullpoints = np.append(hullpoints, new_point)

However, there are still problems with this: all additional points will be all the same value: the midpoint between x[0] and x[1].

A perhaps simpler approach is:

# reproducible init
ch = {
    '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]),
}

n = 10
ch2 = {
    k: tuple([np.linspace(x[0], x[-1], n) for x in v])
    for k, v in ch.items()
}

This replaces each segment x by an even linear spacing between the extremities. If you are sure x has always 2 components (like in your example), you can write np.linspace(*x, n) instead of np.linspace(x[0], x[-1], n).

Result, with the values from your example, you get:

>>> ch2
{'key1': (array([2.7       , 2.74444444, 2.78888889, 2.83333333, 2.87777778,
         2.92222222, 2.96666667, 3.01111111, 3.05555556, 3.1       ]),
  array([2.8       , 2.77777778, 2.75555556, 2.73333333, 2.71111111,
         2.68888889, 2.66666667, 2.64444444, 2.62222222, 2.6       ])),
 'key2': (array([4.7       , 4.75555556, 4.81111111, 4.86666667, 4.92222222,
         4.97777778, 5.03333333, 5.08888889, 5.14444444, 5.2       ]),
  array([3.8       , 3.55555556, 3.31111111, 3.06666667, 2.82222222,
         2.57777778, 2.33333333, 2.08888889, 1.84444444, 1.6       ]),
  array([4.8       , 4.33333333, 3.86666667, 3.4       , 2.93333333,
         2.46666667, 2.        , 1.53333333, 1.06666667, 0.6       ])),
 'key3': (array([2.7       , 2.74444444, 2.78888889, 2.83333333, 2.87777778,
         2.92222222, 2.96666667, 3.01111111, 3.05555556, 3.1       ]),
  array([2.8       , 2.77777778, 2.75555556, 2.73333333, 2.71111111,
         2.68888889, 2.66666667, 2.64444444, 2.62222222, 2.6       ]),
  array([1.7       , 1.96666667, 2.23333333, 2.5       , 2.76666667,
         3.03333333, 3.3       , 3.56666667, 3.83333333, 4.1       ]))}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文