如何生成空间数据的k近邻矩阵?

时间:2022-04-04 20:11:36

I have a spatial dataframe with about 3000 points. I want to generate a matrix that provides the k (in this case 30) nearest neighbors for each point.

我有一个大约3000点的空间数据。我想要生成一个矩阵,它为每个点提供k(在本例中为30)的近邻。

I can do it using a loop but i feel that there should be an elegant and optimal way for spatial points dataframe class that i do not know of.

我可以用一个循环来做,但是我觉得对于空间点dataframe类,我不知道应该有一个优雅和最优的方法。

1 个解决方案

#1


4  

Probably the fastest is to use RANN package - assuming you have x and y:

可能最快的是使用RANN包——假设你有x和y:

library(RANN)
m <- as.matrix(nn(data.frame(x=x, y=y, z=rep(0,length(x))), p=30)$nn.idx)

gives you a 3000 x 30 matrix of closest neighbors. It is several orders of magnitude faster than a naive quadratic search.

给你一个3000×30矩阵的最近邻。它比单纯的二次搜索快几个数量级。

Edit: Just for completeness, it doesn't matter which ANN frontend you pick, with FNN (suggested by Spacedman) this would be

编辑:为了完整性,不管你选择哪一个ANN frontend,和FNN(由Spacedman建议),这将是。

library(FNN)
m <- get.knn(data.frame(x=x, y=y), 30)$nn.index

#1


4  

Probably the fastest is to use RANN package - assuming you have x and y:

可能最快的是使用RANN包——假设你有x和y:

library(RANN)
m <- as.matrix(nn(data.frame(x=x, y=y, z=rep(0,length(x))), p=30)$nn.idx)

gives you a 3000 x 30 matrix of closest neighbors. It is several orders of magnitude faster than a naive quadratic search.

给你一个3000×30矩阵的最近邻。它比单纯的二次搜索快几个数量级。

Edit: Just for completeness, it doesn't matter which ANN frontend you pick, with FNN (suggested by Spacedman) this would be

编辑:为了完整性,不管你选择哪一个ANN frontend,和FNN(由Spacedman建议),这将是。

library(FNN)
m <- get.knn(data.frame(x=x, y=y), 30)$nn.index