Minkowski distance in Python



Minkowski distance is a metric in a normed vector space that measures the distance between two or more vectors. This is typically used in machine learning to find distance similarity. The formula to calculate the Minkowski distance is -

$$\mathrm{D= \big[\sum_{i=1}^{n}|r_i-s_i|^p\big]^{1/p}}$$

Where,

  • xi and yi: Two points in n-dimensional space
  • p: Parameter that determines the type of distance being calculated. For example, if p=1, it is the Manhattan distance, and if p=2, it represents Euclidean distance.

For example, if we consider the following vector's as input -

x = (0, 3, 1, 4)
y = (2, 9, 3, 7)
p = 5

The Minkowski distance between the vectors is -

6.047

Minkowski Distance Using Python

To calculate Minkowski distance in Python, we use the math module and decimal module to perform precise floating-point arithmetic as we calculate fractional roots.

Example

In the example program below, we define functions to calculate the pth root and then to calculate the Minkowski distance -

from math import *
from decimal import Decimal

#Function to calculate the pth root
def my_p_root(value, root):
   my_root_value = 1 / float(root)
   return round (Decimal(value) **
   Decimal(my_root_value), 3)
   
#Function to calculate Minkowski Distance
def my_minkowski_distance(x, y, p_value):
   return (my_p_root(sum(pow(abs(a-b), p_value)
      for a, b in zip(x, y)), p_value))

x = (0, 3, 1, 4)
y = (2, 9, 3, 7)
p = 5
print("The Distance is :",my_minkowski_distance(x, y, p))

The output returned by the above code is as follows -

The Distance is : 6.047

Minkowski Distance Using SciPy

SciPy provides us with a function named minkowski that returns the Minkowski Distance between two points.

Example 1

Let's see how we can calculate the Minkowski distance between two points using the SciPy library -

# Importing the SciPy library
from scipy.spatial import distance
# Defining the points
x = (0, 3, 1, 4)
y = (2, 9, 3, 7)
x, y
# Computing the Minkowski distance
minkowski_distance = distance.minkowski(x, y, p=5)
print('Minkowski Distance b/w', x, 'and', y, 'is: ', minkowski_distance)

The output returned by the above code is as follows -

Minkowski Distance b/w (0, 3, 1, 4) and (2, 9, 3, 7) is:  6.046645611773954

Example 2

We have calculated the Minkowski distance with order(p) = 3. But when the order is 2, it will represent the Euclidean distance, whereas when the order is 1, it will represent the Manhattan distance. Let's understand it with the following example -

# Importing the SciPy library
from scipy.spatial import distance
# Defining the points
x = (0, 3, 1, 4)
y = (2, 9, 3, 7)
x, y

# minkowski and manhattan distance
manhattan_distance = distance.minkowski(x, y, p=1)
print('Minkowski Distance of order(P)1 -Manhattan Distance: ',manhattan_distance)

# minkowski and euclidean distance
euclidean_distance = distance.minkowski(x, y, p=2)
print('Minkowski Distance of order(P)2 - Euclidean Distance: ',euclidean_distance)

The output returned by the above code is as follows -

Minkowski Distance of order(P)1 -Manhattan Distance:  13.0
Minkowski Distance of order(P)2 - Euclidean Distance:  7.280109889280518
Updated on: 2025-06-11T14:47:47+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements