点与固定目标点的平均距离
Mean distance of the points from a fixed target point
我的数据中有一些点,我正在尝试找到到目标点的平均(算术平均)距离。
我这里走两条路线:
一,使用'Distance Between Two Points'公式计算每个点到目标之间的距离,然后得到这些距离值的平均距离。
其他,求所有点的均值点,然后求这个均值点到目标点的距离。
如果我想得到所有点到目标点的平均距离,我不确定哪种方法是正确的(都给出不同的答案)?
我的目标点是绿色的,平均点是红色的。
以下是我的 R 代码:
# three data points
a <- c(1.6, 2.3, 3.4)
b <- c(3.1, 4.1, 0.5)
# target point
t_x <- 1.1
t_y <- 0.1
df <- data.frame("x" = a, "y" = b)
# mean of the distances
df$distance <- sqrt(((df$x - t_x)^2) + ((df$y - t_y)^2))
print(mean(df$distance))
# distance from the mean point to the target
mean_x <- mean(df$x)
mean_y <- mean(df$y)
print(sqrt((mean_x - t_x)^2 + (mean_y - t_y)^2))
# plotting all
ggplot(df, aes(x = x, y = y)) +
geom_point() +
coord_cartesian(xlim = c(-5,5), ylim = c(-5,5)) +
geom_point(aes(x=mean_x, y=mean_y), color = "red") +
geom_point(aes(x=t_x, y=t_y), color = "green")
只考虑一个目标点和另外两个点的情况
Target: (0,0)
Point1: (-1,0)
Point2: (1, 0)
如果取点 1 和点 2 的平均值,则得到 (0,0),因此到目标的平均距离为 0。但是每个点到目标的距离为 1,因此平均距离in 1. 总的来说这两个计算是有很大区别的
这真的取决于你如何定义问题。在此示例中,您希望答案是 0 还是 1。
这里有一个直观的解释,说明为什么您的第一种方法是正确的。假设您的目标位于 (0, 0):
t_x <- 0
t_y <- 0
现在假设我们在它周围画一些点,它们与它的距离都相同——事实上,它们都位于单位圆上,根据定义,它们与目标的距离为 1:
library(ggplot2)
t_x <- 0
t_y <- 0
rads <- seq(0, 2 * pi, length.out = 17)[-17]
df <- data.frame(x = cos(rads), y = sin(rads), xend = 0, yend = 0)
ggplot(df, aes(x, y)) +
geom_point(col = "red") +
geom_segment(aes(xend = xend, yend = yend), linetype = 2) +
coord_equal() +
geom_point(x = t_x, y = t_y, size = 5, colour = "red")
现在,毫不奇怪,由于所有点与目标的距离为 1,因此毕达哥拉斯的平均距离也将为 1:
# mean of the distances
df$distance <- sqrt(((df$x - t_x)^2) + ((df$y - t_y)^2))
print(mean(df$distance))
#> [1] 1
但现在考虑如果我们取所有 x 值的平均值会发生什么 - 它们抵消为 0。y 值也是如此,因此所有点的平均值为 (0, 0)。当你测量从 (0, 0) 到 (0, 0) 的距离时,答案当然是 0:
mean_x <- mean(df$x)
mean_y <- mean(df$y)
print(sqrt((mean_x - t_x)^2 + (mean_y - t_y)^2))
#> [1] 0
由 reprex package (v0.3.0)
于 2020-08-22 创建
我的数据中有一些点,我正在尝试找到到目标点的平均(算术平均)距离。
我这里走两条路线:
一,使用'Distance Between Two Points'公式计算每个点到目标之间的距离,然后得到这些距离值的平均距离。
其他,求所有点的均值点,然后求这个均值点到目标点的距离。
如果我想得到所有点到目标点的平均距离,我不确定哪种方法是正确的(都给出不同的答案)?
我的目标点是绿色的,平均点是红色的。
以下是我的 R 代码:
# three data points
a <- c(1.6, 2.3, 3.4)
b <- c(3.1, 4.1, 0.5)
# target point
t_x <- 1.1
t_y <- 0.1
df <- data.frame("x" = a, "y" = b)
# mean of the distances
df$distance <- sqrt(((df$x - t_x)^2) + ((df$y - t_y)^2))
print(mean(df$distance))
# distance from the mean point to the target
mean_x <- mean(df$x)
mean_y <- mean(df$y)
print(sqrt((mean_x - t_x)^2 + (mean_y - t_y)^2))
# plotting all
ggplot(df, aes(x = x, y = y)) +
geom_point() +
coord_cartesian(xlim = c(-5,5), ylim = c(-5,5)) +
geom_point(aes(x=mean_x, y=mean_y), color = "red") +
geom_point(aes(x=t_x, y=t_y), color = "green")
只考虑一个目标点和另外两个点的情况
Target: (0,0)
Point1: (-1,0)
Point2: (1, 0)
如果取点 1 和点 2 的平均值,则得到 (0,0),因此到目标的平均距离为 0。但是每个点到目标的距离为 1,因此平均距离in 1. 总的来说这两个计算是有很大区别的
这真的取决于你如何定义问题。在此示例中,您希望答案是 0 还是 1。
这里有一个直观的解释,说明为什么您的第一种方法是正确的。假设您的目标位于 (0, 0):
t_x <- 0
t_y <- 0
现在假设我们在它周围画一些点,它们与它的距离都相同——事实上,它们都位于单位圆上,根据定义,它们与目标的距离为 1:
library(ggplot2)
t_x <- 0
t_y <- 0
rads <- seq(0, 2 * pi, length.out = 17)[-17]
df <- data.frame(x = cos(rads), y = sin(rads), xend = 0, yend = 0)
ggplot(df, aes(x, y)) +
geom_point(col = "red") +
geom_segment(aes(xend = xend, yend = yend), linetype = 2) +
coord_equal() +
geom_point(x = t_x, y = t_y, size = 5, colour = "red")
现在,毫不奇怪,由于所有点与目标的距离为 1,因此毕达哥拉斯的平均距离也将为 1:
# mean of the distances
df$distance <- sqrt(((df$x - t_x)^2) + ((df$y - t_y)^2))
print(mean(df$distance))
#> [1] 1
但现在考虑如果我们取所有 x 值的平均值会发生什么 - 它们抵消为 0。y 值也是如此,因此所有点的平均值为 (0, 0)。当你测量从 (0, 0) 到 (0, 0) 的距离时,答案当然是 0:
mean_x <- mean(df$x)
mean_y <- mean(df$y)
print(sqrt((mean_x - t_x)^2 + (mean_y - t_y)^2))
#> [1] 0
由 reprex package (v0.3.0)
于 2020-08-22 创建