在R中为kmeans设置静态中心 [英] Set static centers for kmeans in R

查看:165
本文介绍了在R中为kmeans设置静态中心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想基于预先确定的中心点(my_center_Points)对经度和纬度(my_long_lats)列表进行分组.

I want to group a list of Long and Lats (my_long_lats) based on pre determined center points (my_center_Points).

当我跑步时:-

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

k$centers 不等于 my_center_Points.

k$centers does not equal my_center_Points.

我假设k均值已将我的中心点调整为最佳中心.但是我需要的是my_center_Points不变,并围绕它们将my_long_lats分组.

I assume k-means has adjusted my center points to the optimal center. But what I need is for my_center_Points to not change and group my_long_lats around them.

在此链接 他们谈论设置初始中心,但是如何设置运行k均值后不会改变的中心?还是为此有更好的聚类算法?

In this link they talk about setting initial centers but How do I set centers that wont change once I run the k means? Or is there a better clustering algorithm for this?

我什至可以解决使中心的移动最小化的问题.

I could even settle for minimizing the movement of the centers.

我在R方面仍有很多要学习的地方,对我们的帮助非常感谢.

I still have a lot to learn in R, any help is really appreciated.

推荐答案

这里是使用geosphere库进行的计算,可以正确地计算与纬度和经度之间的距离.

Here is the calculation using the geosphere library to properly compute the distance from latitude and longitude.

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

The variable closestcenter is the result which identifies the closest center to each point.

#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) 

这篇关于在R中为kmeans设置静态中心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆