R k means user center algorithm

I have a 2D dataset imported into R - (x,y). I want to perform k-means clustering in this dataset, but I would like to set specific coordinates as starting centers. For example, I would like to start with 5 centers with their meanings (5, 10), (3, 8), (46, 22), (87, 66), (39, 41).

I saw the center parameter in the kmeans function, but I don't understand how to set my values ​​as centers.

kmeans(data, centers = ...) # what to set here?
+4
source share
2 answers

The parameter centersaccepts either an integer k, in which case krandom points from dataare selected as the initial centers or the matrix of initial centers with as many columns as data. Try the following:

x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
                 matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
kmeans(x,centers=3)
kmeans(x,centers=x[1:3,])
+3

matrix, :

data = matrix(c(1.1,1,0.97,0.99,0.95,0.8,0.91,2.1,2,2.4,4.1,4.4,4.5,3.9,1.5,1.2,1.7,2.6,2.7,2.44), ncol=2)

C1 (x1=1,y1=3) C2 (x2=2,y2=4) ( , , 3 ):

km = kmeans(data, centers=matrix(c(1,2,3,4),ncol=2))

:   df = transform (as.data.frame(data), group = as.character(km $cluster))    (ggplot2)

ggplot(df, aes(V1, V2, color=group)) + geom_point()

enter image description here

+2

All Articles