简体   繁体   English

关于python和欧几里得距离计算的问题

[英]Question surrounding python and euclidean distance calculation

I'm learning python and i'm wondering if someone could help steer me in the right direction.我正在学习 python,我想知道是否有人可以帮助我朝着正确的方向前进。 I have to write some python code that will randomly generate N (N is how many the user specifies when prompted) points on a 2D plane which I believe I have done:我必须编写一些 python 代码,这些代码将在 2D 平面上随机生成 N(N 是用户在提示时指定的数量)点,我相信我已经完成了:

from random import randrange
# Example points in 2-dimensional space...
i = int(input('How many coordinates would you like to generate? \n'))

coords_list = [(randrange(100), randrange(100)) for _ in range(i)]

print(coords_list)

This creates a list of coordinate tuples: [(1,2), (3,4)] etc.这将创建一个坐标元组列表:[(1,2), (3,4)] 等。

The question then asks calculate the distance matrix between these points but I can't use numpy or anything else like that, I have to use a distance function and I've come across a euclidean equation but i'm unsure on how that will help me build the matrix.然后问题询问计算这些点之间的距离矩阵,但我不能使用 numpy 或其他类似的东西,我必须使用距离 function 并且我遇到了一个欧几里得方程,但我不确定这将如何帮助我建立矩阵。

If someone could give me some hints or tips I would be very grateful如果有人能给我一些提示或提示,我将不胜感激

Many thanks, Jack非常感谢,杰克

As per what I understand through your question is you need to create n Coordinates and then make a distance matrix over all the points in the plane.根据我对您的问题的理解,您需要创建 n 个坐标,然后在平面上的所有点上创建一个距离矩阵。

You can use scipy is a Python-based ecosystem of open-source software for mathematics, science, and engineering.您可以使用 scipy 是一个基于 Python 的数学、科学和工程开源软件生态系统。 - Scipy - Scipy

This function - scipy.spatial.distance.euclidean will hel you to calculate distance between two points in a plane.这个 function - scipy.spatial.distance.euclidean将帮助您计算平面中两点之间的距离。

And, you can create a loop over it to store it in a matrix to create your distance matrix.并且,您可以在其上创建一个循环以将其存储在矩阵中以创建距离矩阵。

In case I am missing some point from your question please let me know.如果我从你的问题中遗漏了一些观点,请告诉我。

Thanks谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM