R语言 如何计算Minkowski距离

R语言 如何计算Minkowski距离

在这篇文章中,我们将看到如何在R编程语言中计算闵科夫斯基距离。

闵科夫斯基距离

闵科夫斯基距离是在N维空间的两点之间测量的距离。它基本上是对欧几里得距离和曼哈顿距离的概括。它被广泛用于机器学习领域,特别是在寻找数据的最佳相关性或分类的概念中。闵科夫斯基距离也被用于某些算法中,如K-Nearest Neighbors、Learning Vector Quantization(LVQ)、Self-Organizing Map(SOM)和K-Means Clustering。

让我们考虑一个有三个点P1 (X1 , Y1 ), P2 (X2 , Y2 ), 和P3 (X3, Y3 )的二维空间,闵可夫斯基距离由( |X1 - Y1 |p + |X2 - Y2 |p + |X2 - Y2 |p )1/p 得到。 在R中,闵可夫斯基距离是相对于向量计算的。

如何在R语言中计算Minkowski距离?

比如说

我们有两个向量,向量1为(4, 2, 6, 8),向量2为(5, 1, 7, 9)。在p=2的情况下,它们的闵可夫斯基距离由( |4 – 5|2 + |2 – 1|2 + |6 – 7|2 + |8 – 9|2 )1/2 给出,等于2. 本文主要讨论如何在R中计算闵可夫斯基距离。

方法1:使用自定义函数

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

Σ|vector1i – vector2i |p )1/p

这里。

矢量1是第一个矢量

向量2是第二个向量

p是一个整数

下面是使用自定义函数计算闵可夫斯基距离的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语言中计算Minkowski距离?

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

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

语法: dist(vect, method = “minkowski”, p = integer, diag = TRUE or FALSE, upper = TRUE or FALSE)

参数。

  • vect。一个二维向量
  • 方法。它必须等于 “Minkowski”。
  • p:必须等于一个整数
  • diag:逻辑值(TRUE或FALSE),表示是否应通过print.dist打印距离矩阵的对角线。
  • upper:逻辑值(TRUE或FALSE),表达距离矩阵的上三角是否应该被print.dist打印。

返回类型。

它返回一个 “dist “类的对象,代表每一对独特的行或向量之间的明斯基距离。

注意: diag和upper参数是可选的

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

# 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语言中计算Minkowski距离?

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

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

# 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)

输出

如何在R语言中计算Minkowski距离?

正如你在输出中所看到的,当向量的长度不相等时,编译器会产生一个警告信息。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程