python-scipyHow can I calculate the Euclidean distance between two points using Python and SciPy?
The Euclidean distance between two points can be calculated using Python and SciPy. The following example code block will calculate the Euclidean distance between two points (x1, y1)
and (x2, y2)
:
from scipy.spatial import distance
x1 = 5
y1 = 6
x2 = 3
y2 = 4
euclidean_distance = distance.euclidean((x1, y1), (x2, y2))
print(euclidean_distance)
The output of this code will be:
2.8284271247461903
The code can be broken down into the following parts:
from scipy.spatial import distance
: This imports thedistance
module from SciPy.x1 = 5
andy1 = 6
: These set the x and y coordinates of the first point.x2 = 3
andy2 = 4
: These set the x and y coordinates of the second point.euclidean_distance = distance.euclidean((x1, y1), (x2, y2))
: This calculates the Euclidean distance between the two points.print(euclidean_distance)
: This prints the result of the calculation.
Helpful links
More of Python Scipy
- How do I create a 2D array of zeros using Python and NumPy?
- How do I create a zero matrix using Python and Numpy?
- How do I use the NumPy transpose function in Python?
- How do I create a numpy array of zeros using Python?
- How can I check if a certain version of Python is compatible with SciPy?
- How do I use Python and SciPy to create a tutorial PDF?
- How can I use Python and SciPy to find the zeros of a function?
- How do I use the scipy ttest_ind function in Python?
- How do I use the trapz function in Python SciPy?
- How do I create an array of zeros with the same shape as an existing array using Python and NumPy?
See more codes...