R:用不同方法计算两个向量之间的欧氏距离时结果不同
R: results differ when calculating Euclidean distance between two vectors with different methods
假设我有两个向量。
x1 = c(-1, 2, 3)
x2 = c(4, 0, -3)
为了计算欧氏距离,我使用了三种不同的方法
1-内置函数norm
s = cbind(x1, x2)
norm(s, "2")
#[1] 5.797896
2-手算
sqrt(sum(x2 - x1) ^ 2)
#[1] 8.062258
3-自定义函数
lpnorm <- function(x, p){
n <- sum(abs(x) ^ p) ^ (1 / p)
return(n)
}
lpnorm(s, 2)
#[1] 6.244998
为什么我得到不同的结果?
如果我错了,如何解决这个问题?
你需要s = x2 - x1
.
norm(s, "2")
#[1] 8.062258
sqrt(sum(s ^ 2)) ## or: sqrt(c(crossprod(s)))
#[1] 8.062258
lpnorm(s, 2)
#[1] 8.062258
如果您定义s = cbind(x1, x2)
,您列出的选项中的none将计算x1
和x2
之间的欧氏距离,但我们仍然可以得到它们输出相同的值。在这种情况下,它们是向量的 L2 范数 c(x1, x2)
.
norm(s, "F")
#[1] 6.244998
sqrt(sum(s ^ 2))
#[1] 6.244998
lpnorm(s, 2)
#[1] 6.244998
最后,norm
不是计算距离的常用方法。它确实适用于矩阵范数。当您执行 norm(cbind(x1, x2), "2")
时,它会计算 L2 矩阵范数,它是矩阵 cbind(x1, x2)
.
的最大奇异值
So my problem is with defining s
. Ok, what if I have more than three vectors?
在这种情况下,您需要成对的欧几里得矩阵。参见函数 ?dist
.
I have the train sets (containing three or more rows) and one test set (one row). So, I would like to calculate the Euclidean distance or may be other distances. This is the reason why I want to make sure about the distance calculation.
您想要一个向量与许多其他向量之间的距离,结果是一个向量?
set.seed(0)
X_train <- matrix(runif(10), 5, 2)
x_test <- runif(2)
S <- t(X_train) - x_test
apply(S, 2, norm, "2") ## don't try other types than "2"
#[1] 0.8349220 0.7217628 0.8012416 0.6841445 0.9462961
apply(S, 2, lpnorm, 2)
#[1] 0.8349220 0.7217628 0.8012416 0.6841445 0.9462961
sqrt(colSums(S ^ 2)) ## only for L2-norm
#[1] 0.8349220 0.7217628 0.8012416 0.6841445 0.9462961
我要再次强调 norm
会在向量上失败,除非 type = "2"
。 ?norm
明确表示此函数适用于 matrix。 norm
所做的与您自定义的 lpnorm
函数有很大不同。 lpnorm
用于 向量范数 ,norm
用于 矩阵范数 。即使 "L2" 对于矩阵和向量也有不同的含义。
假设我有两个向量。
x1 = c(-1, 2, 3)
x2 = c(4, 0, -3)
为了计算欧氏距离,我使用了三种不同的方法
1-内置函数norm
s = cbind(x1, x2)
norm(s, "2")
#[1] 5.797896
2-手算
sqrt(sum(x2 - x1) ^ 2)
#[1] 8.062258
3-自定义函数
lpnorm <- function(x, p){
n <- sum(abs(x) ^ p) ^ (1 / p)
return(n)
}
lpnorm(s, 2)
#[1] 6.244998
为什么我得到不同的结果?
如果我错了,如何解决这个问题?
你需要s = x2 - x1
.
norm(s, "2")
#[1] 8.062258
sqrt(sum(s ^ 2)) ## or: sqrt(c(crossprod(s)))
#[1] 8.062258
lpnorm(s, 2)
#[1] 8.062258
如果您定义s = cbind(x1, x2)
,您列出的选项中的none将计算x1
和x2
之间的欧氏距离,但我们仍然可以得到它们输出相同的值。在这种情况下,它们是向量的 L2 范数 c(x1, x2)
.
norm(s, "F")
#[1] 6.244998
sqrt(sum(s ^ 2))
#[1] 6.244998
lpnorm(s, 2)
#[1] 6.244998
最后,norm
不是计算距离的常用方法。它确实适用于矩阵范数。当您执行 norm(cbind(x1, x2), "2")
时,它会计算 L2 矩阵范数,它是矩阵 cbind(x1, x2)
.
So my problem is with defining
s
. Ok, what if I have more than three vectors?
在这种情况下,您需要成对的欧几里得矩阵。参见函数 ?dist
.
I have the train sets (containing three or more rows) and one test set (one row). So, I would like to calculate the Euclidean distance or may be other distances. This is the reason why I want to make sure about the distance calculation.
您想要一个向量与许多其他向量之间的距离,结果是一个向量?
set.seed(0)
X_train <- matrix(runif(10), 5, 2)
x_test <- runif(2)
S <- t(X_train) - x_test
apply(S, 2, norm, "2") ## don't try other types than "2"
#[1] 0.8349220 0.7217628 0.8012416 0.6841445 0.9462961
apply(S, 2, lpnorm, 2)
#[1] 0.8349220 0.7217628 0.8012416 0.6841445 0.9462961
sqrt(colSums(S ^ 2)) ## only for L2-norm
#[1] 0.8349220 0.7217628 0.8012416 0.6841445 0.9462961
我要再次强调 norm
会在向量上失败,除非 type = "2"
。 ?norm
明确表示此函数适用于 matrix。 norm
所做的与您自定义的 lpnorm
函数有很大不同。 lpnorm
用于 向量范数 ,norm
用于 矩阵范数 。即使 "L2" 对于矩阵和向量也有不同的含义。