MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / euclidean_distance

Function euclidean_distance

maths/euclidean_distance.py:12–25  ·  view source on GitHub ↗

Calculate the distance between the two endpoints of two vectors. A vector is defined as a list, tuple, or numpy 1D array. >>> float(euclidean_distance((0, 0), (2, 2))) 2.8284271247461903 >>> float(euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2]))) 3.46410161513775

(vector_1: Vector, vector_2: Vector)

Source from the content-addressed store, hash-verified

10
11
12def euclidean_distance(vector_1: Vector, vector_2: Vector) -> VectorOut:
13 """
14 Calculate the distance between the two endpoints of two vectors.
15 A vector is defined as a list, tuple, or numpy 1D array.
16 >>> float(euclidean_distance((0, 0), (2, 2)))
17 2.8284271247461903
18 >>> float(euclidean_distance(np.array([0, 0, 0]), np.array([2, 2, 2])))
19 3.4641016151377544
20 >>> float(euclidean_distance(np.array([1, 2, 3, 4]), np.array([5, 6, 7, 8])))
21 8.0
22 >>> float(euclidean_distance([1, 2, 3, 4], [5, 6, 7, 8]))
23 8.0
24 """
25 return np.sqrt(np.sum((np.asarray(vector_1) - np.asarray(vector_2)) ** 2))
26
27
28def euclidean_distance_no_np(vector_1: Vector, vector_2: Vector) -> VectorOut:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected