• Skip to secondary menu
  • Skip to main content
  • Skip to primary sidebar

Python and R Tips

Learn Data Science with Python and R

  • Home
  • Python
  • Pandas
    • Pandas 101
  • tidyverse
    • tidyverse 101
  • R
  • Linux
  • Conferences
  • Python Books
  • About
    • Privacy Policy
You are here: Home / Numpy Tips / How to Compute Euclidean distance in Numpy

How to Compute Euclidean distance in Numpy

December 26, 2022 by cmdlinetips

In this post, we will learn how to compute Euclidean distance between two points in 2-dimension and also in 3-dimension using Numpy. Euclidean distance is one of the most commonly used distance measure and it is defined as the straight-line distance between two points in Euclidean space.

Euclidean distance is calculated as the square root of the sum of the squares of the differences between the coordinates of the two points. For example, in 2-dimension the distance between two points (x1,y1) and (x2,y2) is

Euclidean_distance_2d = sqrt((x1-x2)^2 + (y1-y2)^2)

We will use two approaches, both relying Numpy’s features, to compute Euclidean distance between points.

Let us load the Numpy module.

import numpy as np
# find Numpy version
np.__version__

1.24.0

Compute Euclidean distance in Numpy

In the first approach, we will use the above Euclidean distance formula and compute the distance using Numpy functions np.square(), np.sum(), and np.sqrt().

Let us first define points in 2-dimensional space.

# Define the points
point1 = np.array([1, 2])
point2 = np.array([4, 6])
print(point1)
print(point2)

[1,2]
[4,6]

Here (x1,y1) is [1,2] and (x2,y2) is [4,6]. We can compute the square of the difference between points using np.square() as shown below.

square_diff = np.square(point1 - point2)
square_diff

array([ 9, 16])

It results us an array containing square differences. We can add all the square differences using np.sum()

# sum of the square differences
sum_square_diff = np.sum(square_diff)
sum_square_diff

25

Now by taking square root on the squared differences, we get the Eucledian distance. In our example, Eucledian distance betwee the data points in 2D is 5

# sum of the square differences
euclian_dist_2d = np.sqrt(sum_square_diff)
euclian_dist_2d

5.0


Euclidean Distance with linalg.norm

Another way to compute Eucledian distance between two points in Numpy is to use the linalg.norm function from the numpy.linalg module. This function takes an array of points and returns the Euclidean norm (or distance) of each point.

Let us use the same 2d points in the previous example and show how to use linalg.norm to compute the Euclidean distance between two points in 2D:

# Define the points
point1 = np.array([1, 2])
point2 = np.array([4, 6])

# Calculate the Euclidean distance in 2D
distance = np.linalg.norm(point1 - point2)

print(distance)  

5

We can see that we get the same Eucledian distance using. linalg.norm function as expectecd.

In the second example, we use linalg.norm to compute Euclidean distance between points in 3-dimension.

# Define the points
point1 = np.arange(3)
point2 = np.arange(7,10)
print(point1)
print(point2)

[0 1 2]
[7 8 9]
# Calculate the Euclidean distance in 3D
distance_3d = np.linalg.norm(point1 - point2)

print(distance_3d)  

12.12435565298214

Computing Euclidean distance between multiple points

We can also use linalg.norm to compute the Euclidean distance between multiple sets of points.

For example, suppose we have two sets of points in 2-dimension, and we want to compute the distance between each pair of points.

# Define the points
points1 = np.array([[1, 2], [3, 4], [5, 6]])
points2 = np.array([[4, 6], [2, 1], [7, 8]])
print(points1)
print(points2)

We can use linalg.norm() function in Numpy to compute the distances

# Calculate the Euclidean distance between each pair of points
distances = np.linalg.norm(points1 - points2, axis=1)

print(distances)  # Output: [5.0, 5.0, 5.0]

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on X (Opens in new window) X

Related posts:

Default Thumbnail9 Basic Linear Algebra Operations with NumPy Default Thumbnail3 Basic Commands to Manipulate NumPy 2d-arrays NumPy is the fundamental package for scientific computing with Python. It contains among other things: a powerful N-dimensional array object sophisticated (broadcasting) functions tools for integrating C/C++ and Fortran code useful linear algebra, Fourier transform, and random number capabilities10 Basic Arithmetic Operations with NumPy array Default ThumbnailNumpy isclose() function with examples

Filed Under: Euclidean distance, Numpy Tips Tagged With: Euclidean distance Numpy, linalg.norm Numpy

Primary Sidebar

Subscribe to Python and R Tips and Learn Data Science

Learn Pandas in Python and Tidyverse in R

Tags

Altair Basic NumPy Book Review Data Science Data Science Books Data Science Resources Data Science Roundup Data Visualization Dimensionality Reduction Dropbox Dropbox Free Space Dropbox Tips Emacs Emacs Tips ggplot2 Linux Commands Linux Tips Mac Os X Tips Maximum Likelihood Estimation in R MLE in R NumPy Pandas Pandas 101 Pandas Dataframe Pandas Data Frame pandas groupby() Pandas select columns Pandas select_dtypes Python Python 3 Python Boxplot Python Tips R rstats R Tips Seaborn Seaborn Boxplot Seaborn Catplot Shell Scripting Sparse Matrix in Python tidy evaluation tidyverse tidyverse 101 Vim Vim Tips

RSS RSS

  • How to convert row names to a column in Pandas
  • How to resize an image with PyTorch
  • Fashion-MNIST data from PyTorch
  • Pandas case_when() with multiple examples
  • An Introduction to Statistical Learning: with Applications in Python Is Here
  • 10 Tips to customize ggplot2 title text
  • 8 Plot types with Matplotlib in Python
  • PCA on S&P 500 Stock Return Data
  • Linear Regression with Matrix Decomposition Methods
  • Numpy’s random choice() function

Copyright © 2025 · Lifestyle Pro on Genesis Framework · WordPress · Log in

Go to mobile version