How to Calculate Minkowski Distance in R?
Last Updated :
14 Jan, 2022
In this article, we are going to see how to calculate Minkowski Distance in the R Programming language.
Minkowski distance:
Minkowski distance is a distance measured between two points in N-dimensional space. It is basically a generalization of the Euclidean distance and the Manhattan distance. It is widely used in the field of Machine learning, especially in the concept to find the optimal correlation or classification of data. Minkowski distance is used in certain algorithms also like K-Nearest Neighbors, Learning Vector Quantization (LVQ), Self-Organizing Map (SOM), and K-Means Clustering.
Let us consider a 2-dimensional space having three points P1 (X1, Y1), P2 (X2, Y2), and P3 (X3, Y3), the Minkowski distance is given by ( |X1 – Y1|p + |X2 – Y2|p + |X2 – Y2|p )1/p. In R, Minkowski distance is calculated with respect to vectors.
For example,
we are given two vectors, vect1 as (4, 2, 6, 8) and vect2 as (5, 1, 7, 9). Their Minkowski distance for p = 2 is given by, ( |4 – 5|2 + |2 – 1|2 + |6 – 7|2 + |8 – 9|2 )1/2 which is equal to 2. This article focuses upon how we can calculate Minkowski distance in R.
Method 1:Using a custom function
We can calculate Minkowski distance between a pair of vectors by apply the formula,
( Σ|vector1i – vector2i|p )1/p
Here,
vector1 is the first vector
vector2 is the second vector
p is an integer
Below is the implementation in R to calculate Minkowski distance by using a custom function.
R
calculateMinkowskiDistance <- function (vect1, vect2, p) {
answer <- as.integer (0)
for (index in 0 : length (vect1))
{
temp = as.integer ( abs (vect1[index] - vect2[index]) ^ p)
answer = sum (temp, answer)
}
answer = answer ^ (1 / p)
return (answer)
}
vect1 <- c (1, 3, 5, 7)
vect2 <- c (2, 4, 6, 8)
p <- as.integer (1)
distance = calculateMinkowskiDistance (vect1, vect2, p)
print ( paste ("The Minkowski distance between vect1\
and vect2 having the value of p = ",p, " is", distance ))
p <- as.integer (2)
distance = calculateMinkowskiDistance (vect1, vect2, p)
print ( paste ("The Minkowski distance between vect1\
and vect2 having the value of p = ",p, " is", distance ))
p <- as.integer (3)
distance = calculateMinkowskiDistance (vect1, vect2, p)
print ( paste ("The Minkowski distance between vect1\
and vect2 having the value of p = ",p, " is", distance ))
p <- as.integer (4)
distance = calculateMinkowskiDistance (vect1, vect2, p)
print ( paste ("The Minkowski distance between vect1 \
and vect2 having the value of p = ",p, " is", distance ))
|
Output:
Method 2: Using inbuilt dist() function
R provides inbuilt dist function using which we can calculate six types of distances including Minkowski distance. This function accepts a two-dimensional vector or a matrix as a parameter. This function is quite useful as it calculates the Minkowski distance between each unique pair of vectors specified in a two-dimensional vector.
Syntax: dist(vect, method = “minkowski”, p = integer, diag = TRUE or FALSE, upper = TRUE or FALSE)
Parameters:
- vect: A two-dimensional vector
- method: It must be equal to “minkowski”
- p: It must be equal to an integer
- diag: logical value (TRUE or FALSE) that conveys whether the diagonal of the distance matrix should be printed by print.dist or not.
- upper: logical value (TRUE or FALSE) that conveys whether the upper triangle of the distance matrix should be printed by print.dist or not.
Return type:
It return an object of class “dist” which represents Minkowski distance between each unique pair of rows or vectors.
Note: diag and upper parameters are optional
Example 1: Implementation using vectors of equal length.
R
vect1 <- c (1, 4, 8, 9, 2, 3)
vect2 <- c (9, 4, 1, 2, 4, 7)
vect3 <- c (1, 7, 9, 3, 2, 8)
vect4 <- c (2, 1, 4, 7, 8, 9)
vect5 <- c (1, 4, 8, 3, 9, 2)
vect6 <- c (3, 7, 8, 6, 5, 9)
twoDimensionalVect <- rbind (vect1, vect2, vect3,
vect4, vect5, vect6)
print ( "Minkowski distance between each pair of vectors is: " )
cat ( "\n\n" )
dist (twoDimensionalVect, method = "minkowski" ,
diag = TRUE , upper = TRUE p = 2)
|
Output:
Note that the length of all vectors present in a two-dimensional vector has to be the same. Otherwise, the compiler will produce a warning message.
Example 2: Implementation using vectors of unequal length.
R
vect1 <- c (2, 4, 1, 9, 2, 3, 10)
vect2 <- c (4, 8, 1, 2, 4, 7)
vect3 <- c (11, 7, 9, 3, 2, 8)
vect4 <- c (21, 1, 4, 7, 8, 9)
vect5 <- c (11, 4, 8, 3, 9, 21)
vect6 <- c (6, 7, 8, 6, 5, 9)
twoDimensionalVect <- rbind (vect1, vect2,
vect3, vect4,
vect5, vect6)
print ( "Minkowski distance between each pair of vectors is: " )
cat ( "\n\n" )
dist (twoDimensionalVect, method = "minkowski" ,
diag = TRUE , upper = TRUE p = 2)
|
Output:
As you can in the output, the compiler produces a warning message when vectors are of unequal lengths.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...