R查找选定点的最近邻居

问题描述 投票:0回答:1

我有一个只有20个数据点的csv文件,我想知道一个新数据点的最近邻居。

我的csv文件看起来像这样

temp    rain
79        12
81        13 
79        4
61        9
60        15
45        5
34        5
100       9
101       3
59        11
58        16

所以,我想知道使用欧氏距离和KNN为点65、7找到最近邻居的正确方法。在线上可用的大多数算法都使用大型数据集,例如R的虹膜或德语,但是它很小,不需要清理,因此我觉得这些解决方案使这个问题变得过于复杂。我对R还是很陌生,所以我可能忽略了一个解决方案。感谢您抽出宝贵的时间阅读本文!

我尝试了下面的代码,但是它总是抛出错误,再次,我认为我只是使这个复杂化了

df <- read.csv("data.csv", header = FALSE, sep = ',')

head(df) 

ran <- sample(1:nrow(df), 0.9 * nrow(df)) 

nor <-function(x) { (x -min(x))/(max(x)-min(x))   }

df_train <- df[ran,] 

df_test <- df[-ran,] 
##extract 5th column of train dataset because it will be used as 'cl' argument in knn function.
df_target_category <- df[ran,2]
##extract 5th column if test dataset to measure the accuracy
df_test_category <- df[-ran,2]

library(class)

pr <- knn(df_train,df_test,cl=df_target_category,k=13)

##create confusion matrix
tab <- table(pr,df_test_category)

accuracy <- function(x){sum(diag(x)/(sum(rowSums(x)))) * 100}
accuracy(tab)
r knn
1个回答
0
投票

我不确定您的问题与KNN有什么关系。为什么不简单地计算新点到df中所有其他点的欧几里得距离,然后确定df中的哪个点最接近呢?

这是根据您提供的示例的最小示例。

# Calculate Euclidean distances of `pt` to all points in `df`
dist_to_pt <- as.matrix(dist(rbind(df, pt)))[nrow(df) + 1, 1:nrow(df)]

# Determine the point in `df` with minimal distance to `pt`
dist_to_pt[which.min(dist_to_pt)]
#       4
#4.472136

我们可以可视化旧数据和新数据

library(dplyr)
library(ggplot2)
rbind(df, pt) %>%
    mutate(
        pt_number = row_number(),
        source = ifelse(pt_number > nrow(df), "new", "ref")) %>%
    ggplot(aes(temp, rain, colour = source, label = pt_number)) +
    geom_point() +
    geom_text(position = position_nudge(y = -0.5))

enter image description here

点4是新点12在(65,7)处的最近邻居。


样本数据

df <- read.table(text =
    "temp    rain
79        12
81        13
79        4
61        9
60        15
45        5
34        5
100       9
101       3
59        11
58        16", header = T)

# New point
pt <- c(temp = 65, rain = 7)
© www.soinside.com 2019 - 2024. All rights reserved.