Euclidian Distance using NumPyEuclidian distance between two points on any axes is the shortest distance between them. In other words, it is the displacement length between two points. Given two points, A (a, b) and B (c, d), in a 2-dimensional plane, the Euclidian distance between A and B is given as:
Distance = √ (a-c) 2 + (b - d) 2
Let A (x1, y1, z1) and B (x2, y2, z2) be two points:
Distance = √ (x1 - x2) 2 + (y1 - y2) 2 + (z1 - z2)2
We can use the Numpy library in python to find the Euclidian distance between two vectors without mentioning the whole formula. This article discusses how we can find the Euclidian distance using the functionality of the Numpy library in python. 2. General Method without using NumPy:Output: Euclidian distance between point1 and point2: 3.0
2. Using NumPy:
1. Using linealg.norm() Method:A vector's norm is the vector's size denoted by ||vector||.
Formula:
||x||2 = √ ∑ni=1 |xi|2
Syntax:
numpy. linealg. norm (vector, ord = None, axis = None)
Code: Output: Euclidian distance between p1 and p2: 9.273618495495704
2. Using the dot() Method:
Code: Output: Euclidian distance between p1 and p2: 9.273618495495704 Understanding: Here, p1 = 4i + 7j + 9k p2 = 10i + 12j + 14k difference = p1 - p2 = (4 - 10)i + (7 - 12)j + (9 - 14)k = -6i + -5j + -5k difference. T -> transpose (doesn't change as it is a 1 * 1 matrix) = [-6, -5, -5] dot ([-6, -5, -5], [-6, -5, -5]) = (-6*-6) + (-5*-5) + (-5*-5) = 86 sqrt (sum_sq) = √86 = 9.27 3. square() and sum() Methods:These two methods are as simple as their names. One is used to calculate the sum of vectors and the other for the square of a vector. Code: Output: Euclidian distance between p1 and p2: 9.273618495495704
Next TopicHow to Parse JSON in Python
|
JavaTpoint offers too many high quality services. Mail us on h[email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected].
Duration: 1 week to 2 week