- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Calculating Euclidean distance using SciPy
Euclidean distance is the distance between two real-valued vectors. Mostly we use it to calculate the distance between two rows of data having numerical values (floating or integer values). Below is the formula to calculate Euclidean distance −
$$\mathrm{d(r,s) =\sqrt{\sum_{i=1}^{n}(s_i-r_i)^2} }$$
Here,
r and s are the two points in Euclidean n-space.
si and ri are Euclidean vectors.
n denotes the n-space.
Let’s see how we can calculate Euclidean distance between two points using SciPy library −
Example
# Importing the SciPy library from scipy.spatial import distance # Defining the points A = (1, 2, 3, 4, 5, 6) B = (7, 8, 9, 10, 11, 12) A, B # Computing the Euclidean distance euclidean_distance = distance.euclidean(A, B) print('Euclidean Distance b/w', A, 'and', B, 'is: ', euclidean_distance)
Output
((1, 2, 3, 4, 5, 6), (7, 8, 9, 10, 11, 12)) Euclidean Distance b/w (1, 2, 3, 4, 5, 6) and (7, 8, 9, 10, 11, 12) is: 1 4.696938456699069
Advertisements