2017-12-27 1441 views
1

我想根据预先确定的中心点(my_center_Points)将Long和Lats(my_long_lats)列表分组。在R中设置kmeans的静态中心

当我运行: -

k <- kmeans(as.matrix(my_long_lats), centers = as.matrix(my_center_Points)) 

k$centers不等于 my_center_Points。

我假设k-means已将我的中心点调整到最佳中心。但是我需要的是my_center_Points不会改变它们并将my_long_lats分组。

在这link 他们谈论设置初始中心,但是如何设置中心,不会改变一旦我运行k的手段?还是有更好的聚类算法呢?

我甚至可以决定尽量减少中心的移动。

我还有很多要在R学习,任何帮助真的很感激。

+2

也许你需要一个距离度量,而不是点之间的欧几里得距离? – jsb

回答

1

centers会在执行kmeans聚类后自动进行评估。实际上,确定centers是划分成群集群的关键点。我认为这可以帮助你的几个选项。

  1. 限制iter.max。你可以在kmeans函数调用中将其设置为1。这并不能保证固定中心,但如果你正在处理大量的数据集,变化将会减少。

  2. 使用虚拟数据。您可以在选定的centers附近的实际数据集中添加多个dummy数据。这将会沿着预先确定的centers增加额外的重量。最有可能的centers将保持不变。

+0

#2似乎也会对我很好。谢谢! – Coopa

1

这里是使用geosphere库来计算距离经纬度的距离的计算。

变量closestcenter是标识距离每个点最近的中心的结果。

#define random data 
centers<-data.frame(x=c(44,44, 50, 50), y=c(44, 50, 44, 50)) 
pts<-data.frame(x=runif(25, 40, 55), y=runif(25, 40, 55)) 

#allocate space 
distance<-matrix(-1, nrow = length(pts$x), ncol= length(centers$x)) 

library(geosphere) 
#calculate the dist matrix - the define centers to each point 
#columns represent centers and the rows are the data points 
dm<-apply(data.frame(1:length(centers$x)), 1, function(x){ replace(distance[,x], 1:length(pts$x), distGeo(centers[x,], pts))}) 

#find the column with the smallest distance 
closestcenter<-apply(dm, 1, which.min) 

#color code the original data for verification 
colors<-c("black", "red", "blue", "green") 
plot(pts , col=colors[closestcenter], pch=19) 
+0

是的这种方法更符合我的需求,谢谢! – Coopa