在2D数组中找到一个角值
我在((0,1),(0,1))域中有一个随机分布的点。 我需要找到角值。 (沿0轴的最小值,沿1轴的最小值) (沿0轴的最大值,沿1轴的最小值) (沿0轴的最大值,最大沿1轴) (沿0轴的最小值,最大沿1轴) 问题在于同时搜索沿2个轴的最小值/最大值。 如何在numpy中实现这种模拟搜索?
import numpy as np
np.random.seed(42)
points = np.random.random((100,2))
I have a randomly distributed points inside the ((0,1), (0,1)) domain.
I need to find the corner values.
(min along 0 axis, min along 1 axis)
(max along 0 axis, min along 1 axis)
(max along 0 axis, max along 1 axis)
(min along 0 axis, max along 1 axis)
The problem is in the simultanuosly searching the min/max values along the 2 axes.
How to realize this simulataneous search in numpy?
import numpy as np
np.random.seed(42)
points = np.random.random((100,2))
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用欧几里得距离找到最接近的点。从角落到那个点,找到向量的长度是eqvalent。对于
(0,0)
这很琐碎:对于其他角落,您需要先找到它们之间的向量,例如
(0,1
)首先计算向量
点 - [0,1]
,然后在该向量中平方两个值。然后,它使用np.Argmin
在每一行上汇总,找到最小的值的参数或索引,并使用它来重新索引原始点数组来提取点坐标。nb真正的欧几里得距离也将占据正方形之和的平方根,但是由于我们只是在寻找最小的,因此我们不需要采取这一步骤。
You could use the Euclidean distance to find the closest point. Which is eqivalent to finding the length of the vector from the corner to that point. For
(0,0)
it's trivial:For the other corners you need to find the vector between them first, e.g. for
(0,1)
:What this line does is first compute the vectors
points - [0, 1]
and then squares both values in that vector. It then sums across each row, finds the argument or index, of the smallest value usingnp.argmin
and uses this to re-index the original points array to extract the point co-ords.N.B. The true Euclidean distance would also take the square root of the sum of the squares, but since we're just looking for the smallest, we don't need to take that step.