📜  如何计算 R 中的闵可夫斯基距离?

📅  最后修改于: 2022-05-13 01:54:23.779000             🧑  作者: Mango

如何计算 R 中的闵可夫斯基距离?

在本文中,我们将了解如何在 R 编程语言中计算闵可夫斯基距离。

闵可夫斯基距离:

Minkowski 距离是 N 维空间中两点之间的距离。它基本上是欧几里得距离和曼哈顿距离的概括。它广泛用于机器学习领域,特别是在寻找数据的最佳相关性或分类的概念中。 Minkowski 距离也用于某些算法,例如 K-Nearest Neighbors、学习向量量化 (LVQ)、自组织图 (SOM) 和 K-Means 聚类。

让我们考虑一个具有三个点 P 1 (X 1 , Y 1 )、P 2 (X 2 , Y 2 ) 和 P 3 (X 3, Y 3 ) 的二维空间,Minkowski 距离由 ( | X 1 – Y 1 | p + |X 2 – Y 2 | p + |X 2 – Y 2 | p ) 1/p 。在 R 中,Minkowski 距离是相对于向量计算的。

例如,

我们有两个向量,vect1 为 (4, 2, 6, 8),vect2 为 (5, 1, 7, 9)。 p = 2 时的 Minkowski 距离由 (|4 – 5| 2 + |2 – 1| 2 + |6 – 7| 2 + |8 – 9| 2 ) 1/2给出,等于 2。这文章重点介绍了如何计算 R 中的 Minkowski 距离。

方法一:使用自定义函数

我们可以通过应用公式计算一对向量之间的闵可夫斯基距离,

下面是 R 中使用自定义函数计算 Minkowski 距离的实现。

R
# R program to illustrate how to
# calculate Minkowski distance
# using a custom function
 
 # Custom function to calculate Minkowski distance
 calculateMinkowskiDistance <- function(vect1, vect2, p) {
    
   # Initializing answer variable as 0
   answer <- as.integer(0)
    
   # Iterating over the length of the vector
   # Using for-in loop
   for (index in 0 : length(vect1))
   { 
      # temp stores the absolute difference raised to power p
      temp = as.integer(abs(vect1[index] - vect2[index]) ^ p)
       
      # Updating answer variable
      answer = sum(temp, answer)
   }
    
   # The final answer would be answer raised to
   # power 1 / p
   answer = answer ^ (1 / p)
    
   # Return the answer
   return(answer)
}
 
# Initializing a vector
vect1 <- c(1, 3, 5, 7)
 
# Initializing another vector
vect2 <- c(2, 4, 6, 8)
 
# Set p equal to 4
p <- as.integer(1)
 
# Call the function to calculate MinkowskiDistance
distance = calculateMinkowskiDistance(vect1, vect2, p)
 
# Print the calculated distance
print(paste("The Minkowski distance between vect1\
and vect2 having the value of p =",p, "is", distance ))
 
# Set p equal to 5
p <- as.integer(2)
 
# Call the function to calculate MinkowskiDistance
distance = calculateMinkowskiDistance(vect1, vect2, p)
 
# Print the calculated distance
print(paste("The Minkowski distance between vect1\
and vect2 having the value of p =",p, "is", distance ))
 
# Set p equal to 5
p <- as.integer(3)
 
# Call the function to calculate MinkowskiDistance
distance = calculateMinkowskiDistance(vect1, vect2, p)
 
# Print the calculated distance
print(paste("The Minkowski distance between vect1\
and vect2 having the value of p =",p, "is", distance ))
 
# Set p equal to 5
p <- as.integer(4)
 
# Call the function to calculate MinkowskiDistance
distance = calculateMinkowskiDistance(vect1, vect2, p)
 
# Print the calculated distance
print(paste("The Minkowski distance between vect1 \
and vect2 having the value of p =",p, "is", distance ))


R
# R program to illustrate how to calculate
# Minkowski distance By using inbuilt dist()
# function
 
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
 
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
 
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
 
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
 
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3,
                            vect4, vect5, vect6)
 
print("Minkowski distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Minkowski distance between vectors
# using built in dist method
# By passing two-dimensional vector as a parameter
# Since we want to calculate Minkowski distance
# between each unique pair of vectors
# That is why we are passing Minkowski as a method
dist(twoDimensionalVect, method = "minkowski",
     diag = TRUE, upper = TRUE p = 2)


R
# R program to illustrate
# how to calculate Minkowski distance
# By using inbuilt dist() function
 
# Initializing a vector
# Note that the length of vec1 is one
# more than the other vectors
vect1 <- c(2, 4, 1, 9, 2, 3, 10)
 
# Initializing another vector
vect2 <- c(4, 8, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(11, 7, 9, 3, 2, 8)
 
# Initializing another vector
vect4 <- c(21, 1, 4, 7, 8, 9)
 
# Initializing another vector
vect5 <- c(11, 4, 8, 3, 9, 21)
 
# Initializing another vector
vect6 <- c(6, 7, 8, 6, 5, 9)
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2,
                            vect3, vect4,
                            vect5, vect6)
 
print("Minkowski distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Minkowski distance between
# vectors using built in dist method
# By passing two-dimensional vector as a parameter
# Since we want to calculate Minkowski
# distance between each unique pair of vectors
# That is why we are passing Minkowski as a method
dist(twoDimensionalVect, method = "minkowski",
     diag = TRUE, upper = TRUE p = 2)


输出:

方法 2:使用内置 dist()函数

R 提供了内置的 dist函数,我们可以使用它计算六种类型的距离,包括 Minkowski 距离。此函数接受二维向量或矩阵作为参数。此函数非常有用,因为它计算二维向量中指定的每对唯一向量之间的 Minkowski 距离。

示例 1:使用等长向量的实现。

R

# R program to illustrate how to calculate
# Minkowski distance By using inbuilt dist()
# function
 
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
 
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
 
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
 
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
 
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3,
                            vect4, vect5, vect6)
 
print("Minkowski distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Minkowski distance between vectors
# using built in dist method
# By passing two-dimensional vector as a parameter
# Since we want to calculate Minkowski distance
# between each unique pair of vectors
# That is why we are passing Minkowski as a method
dist(twoDimensionalVect, method = "minkowski",
     diag = TRUE, upper = TRUE p = 2)

输出:

请注意,二维向量中存在的所有向量的长度必须相同。否则,编译器会产生警告信息。

示例 2:使用长度不等的向量实现。

R

# R program to illustrate
# how to calculate Minkowski distance
# By using inbuilt dist() function
 
# Initializing a vector
# Note that the length of vec1 is one
# more than the other vectors
vect1 <- c(2, 4, 1, 9, 2, 3, 10)
 
# Initializing another vector
vect2 <- c(4, 8, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(11, 7, 9, 3, 2, 8)
 
# Initializing another vector
vect4 <- c(21, 1, 4, 7, 8, 9)
 
# Initializing another vector
vect5 <- c(11, 4, 8, 3, 9, 21)
 
# Initializing another vector
vect6 <- c(6, 7, 8, 6, 5, 9)
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2,
                            vect3, vect4,
                            vect5, vect6)
 
print("Minkowski distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Minkowski distance between
# vectors using built in dist method
# By passing two-dimensional vector as a parameter
# Since we want to calculate Minkowski
# distance between each unique pair of vectors
# That is why we are passing Minkowski as a method
dist(twoDimensionalVect, method = "minkowski",
     diag = TRUE, upper = TRUE p = 2)

输出:

正如您在输出中可以看到的那样,当向量长度不等时,编译器会生成一条警告消息。