R语言 如何计算曼哈顿距离
曼哈顿距离 是N维向量空间中两点之间的距离度量。它被定义为相应维度的坐标之间的绝对距离之和。
例如,在一个有两个点Point1 (x1 ,y1 )和Point2 (x2 ,y2 )的二维空间中,曼哈顿距离由|x1 - x2 | + |y1 - y2 |给出。
方法1:使用公式的方法
在R中,曼哈顿距离是相对于向量计算的。两个向量之间的曼哈顿距离由以下公式给出。
Σ|vect1i - vect2i|
其中。
- vect1是第一个矢量
- vect2是第二个向量
例如,我们给了两个向量,vect1为(3,6,8,9),vect2为(1,7,8,10)。它们的曼哈顿距离为:|3-1|+|6-7|+|8-8|+|9-10|,等于4。
下面是使用两个等长向量的实现方法。
例1 :
# Function to calculate Manhattan distance
# abs() function calculate the absolute difference
# between corresponding vector elements
# sum() function calculates the sum of the
# absolute difference between
# corresponding elements of vect1 and vect2
manhattanDistance <- function(vect1, vect2){
dist <- abs(vect1 - vect2)
dist <- sum(dist)
return(dist)
}
# Initializing a vector
vect1 <- c(3, 6, 8, 9)
# Initializing another vector
vect2 <- c(1, 7, 8, 10)
print("Manhattan distance between vect1 and vect2 is: ")
# Call the function to calculate Manhattan
# distance between vectors
manhattanDistance(vect1, vect2)
输出 。
[1] "Manhattan distance between vect1 and vect2 is: "
[1] 4
例2 :
如果两个向量的长度不相等,编译器会给出警告信息。下面是使用两个不等长的向量的实现。
# Function to calculate Manhattan distance
# abs() function calculate the absolute difference
# between corresponding vector elements
# sum() function calculates the sum of the
# absolute difference between
# corresponding elements of vect1 and vect2
manhattanDistance <- function(vect1, vect2){
dist <- abs(vect1 - vect2)
dist <- sum(dist)
return(dist)
}
# Initializing two vectors having unequal length
vect1 <- c(14, 13, 24, 18)
vect2 <- c(13, 12, 33, 11, 12)
print("Manhattan distance between vect1 and vect2 is: ")
# Call the function to calculate Manhattan distance
manhattanDistance(vect1, vect2)
输出 。
方法2:使用dist()函数
R提供了一个内置函数,使用该函数我们可以找到二维向量中每一对独特向量之间的曼哈顿距离。
语法 。
dist(2dVect, method = “manhattan”)
参数:
- 2dVect:二维向量
- method:要使用的距离测量方法。这可以是 “euclidean”, “maximum”, “manhattan”, “canberra”, “binary “中的一个。
返回类型 。
它返回一个 “dist “类的对象
例子1 :
下面是使用dist()函数查找曼哈顿距离的实现。
# Initializing a vector
vect1 < - c(1, 16, 8, 10, 100, 20)
# Initializing another vector
vect2 < - c(1, 7, 18, 90, 50, 21)
# Initializing another vector
vect3 < - c(3, 10, 11, 40, 150, 210)
# Initializing another vector
vect4 < - c(2, 1, 4, 7, 8, 10)
# Initializing another vector
vect5 < - c(1, 4, 8, 3, 100, 104)
# Initializing another vector
vect6 < - c(3, 7, 11, 23, 110, 114)
# Row bind vectors into a single matrix
twoDimensionalVect < - rbind(vect1, vect2, vect3, vect4, vect5, vect6)
print("Manhattan distance between each pair of vectors is: ")
cat("\n\n")
# Calculate Manhattan distance between vectors
# using built in dist method
# By passing two-dimensional vector as a parameter
# Since we want to calculate manhattan distance between
# each unique pair of vectors
# That is why we are passing manhattan as a method
dist(twoDimensionalVect, method="manhattan")
输出 。
例2 :
注意,二维向量下呈现的所有向量的长度要求是相同的,否则,R编译器会产生一个编译时错误。
# Initializing a vector
vect1 <- c(4, 3, 5, 7, 8, 2, 10, 12)
# Initializing another vector
vect2 <- c(5, 9, 4, 9, 7, 17)
# Initializing another vector
vect3 <- c(3, 10, 9, 11, 13, 12)
# Initializing another vector
vect4 <- c(4, 7, 6, 12, 10, 12)
# Initializing another vector
vect5 <- c(3, 5, 12, 10, 1, 17)
# Initializing another vector
vect6 <- c(4, 3, 1, 8, 7, 2)
# Using rind function to bind vectors in a 2-d vector
# Note that all vectors are not of the same length
twoDimensionalVect <- rbind(vect1, vect2, vect3, vect4, vect5, vect6)
print("Manhattan distance between each pair of vectors is: ")
cat("\n\n")
# Calculate Manhattan distance between vectors
# using built in dist method
# By passing two-dimensional vector as a parameter
# Since we want to calculate Manhattan distance
# between each pair of vectors
# That is why we are passing "manhattan" as a method
dist(twoDimensionalVect, method = "manhattan")
输出 。