【问题标题】:Set static centers for kmeans in R在 R 中为 kmeans 设置静态中心
【发布时间】:2018-06-08 10:28:48
【问题描述】:

我想根据预先确定的中心点 (my_center_Points) 对经纬度列表 (my_long_lats) 进行分组。

当我跑步时:-

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 中还有很多东西要学,非常感谢任何帮助。

【问题讨论】:

  • 也许您需要一个距离度量,例如点之间的欧几里得距离?

标签: r k-means geosphere


【解决方案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) 

【讨论】:

  • 是的,这个方法更符合我的需要,谢谢!
【解决方案2】:

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

  1. 限制iter.max。您可以在 kmeans 函数调用中将其设置为 1。这不能保证中心保持固定,但如果您处理大型数据集,更改会更少。

  2. 使用虚拟数据。您可以在所选centers 周围的实际数据集中添加许多dummy 数据。这将使预先确定的centers 更加重要。 centers 很可能会保持不变。

【讨论】:

  • #2 似乎也适合我。谢谢!
猜你喜欢
  • 2017-09-14
  • 2021-02-04
  • 2015-03-06
  • 2014-02-22
  • 1970-01-01
  • 1970-01-01
  • 2012-04-02
  • 2015-06-05
  • 2016-08-18
相关资源
最近更新 更多