简体   繁体   English

使用R仅查找一个点(不是整个矩阵)的k-最近邻居

[英]Finding k-nearest neighbor for only one point (not an entire matrix) using R

Is there a package or a simple way to serach k-nearest neighbor (specially with kd tree) for one point using R? 是否有一个包或一种简单的方法来使用R来搜索一个点的k-最近邻居(特别是kd树)? All the packages who provide this function (example RANN or FNN...) compute the knn for all the points in a matrix, I need to do it for only one point. 提供此功能的所有包(例如RANN或FNN ...)计算矩阵中所有点的knn,我只需要一个点。

For example I have a matrix with 10 points "A" to "E" and I want to find for "A" the 2 nearest neighbors between the 4 other points ("B" to "E") without doing the same calculation for all the rows in the dataset (without computing knn for "B", "C", "D", "E") 例如,我有一个10点“A”到“E”的矩阵,我想在“A”中找到4个其他点(“B”到“E”)之间的2个最近邻居,而不对所有人做同样的计算数据集中的行(不计算“B”,“C”,“D”,“E”的knn)

I hope my question is clear, my english is not good. 我希望我的问题很明确,我的英语不好。

Thank you for help, 谢谢你的帮助,

If I understand correctly, you can do this with the FNN package: 如果我理解正确,您可以使用FNN包执行此操作:

> library(FNN)
> X <- matrix(runif(100), 5, 5)
> X
          [,1]      [,2]      [,3]      [,4]      [,5]
[1,] 0.7475301 0.6725876 0.2511358 0.5048512 0.1196027
[2,] 0.5777907 0.6337206 0.8334608 0.5067914 0.6410024
[3,] 0.5488786 0.9613076 0.2217271 0.6906149 0.7396482
[4,] 0.8230380 0.8596784 0.6348114 0.6211107 0.3089131
[5,] 0.6531433 0.8682462 0.2555402 0.2443061 0.5292509
> knnx.dist(X[-1,], X[1, , drop=FALSE], k=2)
          [,1]     [,2]
[1,] 0.4870996 0.531889
> knnx.index(X[-1,], X[1, , drop=FALSE], k=2)
     [,1] [,2]
[1,]    3    4

Note that the result of knnx.index relates to the matrix passed to the function so that 3, and 4 actually means rows 4 and 5 the original data set. 请注意,knnx.index的结果与传递给函数的矩阵有关,因此3和4实际上意味着第4和第5行是原始数据集。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM