gpt4 book ai didi

R,计算两个数据集的最小欧氏距离,并自动标记

转载 作者:行者123 更新时间:2023-12-04 01:56:10 25 4
gpt4 key购买 nike

我正在使用 Euclidean Distance有一对数据集。首先是我的数据。

centers <- data.frame(x_ce = c(300,180,450,500),
y_ce = c(23,15,10,20),
center = c('a','b','c','d'))

points <- data.frame(point = c('p1','p2','p3','p4'),
x_p = c(160,600,400,245),
y_p = c(7,23,56,12))

我的目标是,对于points中的每个点,找到centers中所有中心的最小距离,并附加中心将名称命名为 points 数据集(显然是最小的),并使此过程自动化。

所以我从基础开始:

#Euclidean distance
sqrt(sum((x-y)^2))

事实上,我已经想到了它应该如何工作,但我无法管理如何使其自动化。

  1. 选择一行,以及所有行的中心
  2. 计算行与每行中心之间的欧氏距离
  3. 选择最小的距离
  4. 贴上距离最小的标签
  5. 对第二行重复...直到 points
  6. 结束

所以我设法手动完成,让所有步骤自动完成:

# 1.  
x = (points[1,2:3]) # select the first of points
y1 = (centers[1,1:2]) # select the first center
y2 = (centers[2,1:2]) # select the second center
y3 = (centers[3,1:2]) # select the third center
y4 = (centers[4,1:2]) # select the fourth center

# 2.
# then the distances
distances <- data.frame(distance = c(
sqrt(sum((x-y1)^2)),
sqrt(sum((x-y2)^2)),
sqrt(sum((x-y3)^2)),
sqrt(sum((x-y4)^2))),
center = centers$center
)

# 3.
# then I choose the row with the smallest distance
d <- distances[which(distances$distance==min(distances$distance)),]

# 4.
# last, I put the label near the point
cbind(points[1,],d)

# 5.
# then I restart for the second point

问题是我无法自动管理它。你有没有想过让这个过程对 points 的每个点自动进行?此外,我是否在重新发明轮子,即它是否存在我不知道的更快的过程(作为函数)?

最佳答案

centers <- data.frame(x_ce = c(300,180,450,500),
y_ce = c(23,15,10,20),
center = c('a','b','c','d'))

points <- data.frame(point = c('p1','p2','p3','p4'),
x_p = c(160,600,400,245),
y_p = c(7,23,56,12))

library(tidyverse)

points %>%
mutate(c = list(centers)) %>%
unnest() %>% # create all posible combinations of points and centers as a dataframe
rowwise() %>% # for each combination
mutate(d = sqrt(sum((c(x_p,y_p)-c(x_ce,y_ce))^2))) %>% # calculate distance
ungroup() %>% # forget the grouping
group_by(point, x_p, y_p) %>% # for each point
summarise(closest_center = center[d == min(d)]) %>% # keep the closest center
ungroup() # forget the grouping

# # A tibble: 4 x 4
# point x_p y_p closest_center
# <fct> <dbl> <dbl> <fct>
# 1 p1 160 7 b
# 2 p2 600 23 d
# 3 p3 400 56 c
# 4 p4 245 12 a

关于R,计算两个数据集的最小欧氏距离,并自动标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50486890/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com