Learn R Programming

amap (version 0.5-1)

Kmeans: K-Means Clustering

Description

Perform k-means clustering on a data matrix.

Usage

Kmeans(x, centers, iter.max = 10, nstart = 1,
         method = "euclidean")

Arguments

Value

A list with components:clusterA vector of integers indicating the cluster to which each point is allocated.centersA matrix of cluster centres.withinssThe within-cluster sum of square distances for each cluster.sizeThe number of points in each cluster.

Details

The data given by x is clustered by the k-means algorithm. When this terminates, all cluster centres are at the mean of their Voronoi sets (the set of data points which are nearest to the cluster centre).

The algorithm of Lloyd--Forgy is used; method="euclidean" should return same result as with function kmeans.

See Also

hcluster,kmeans.

Examples

Run this code
## a 2-dimensional example
x <- rbind(matrix(rnorm(100, sd = 0.3), ncol = 2),
           matrix(rnorm(100, mean = 1, sd = 0.3), ncol = 2))
colnames(x) <- c("x", "y")
(cl <- Kmeans(x, 2))
plot(x, col = cl$cluster)
points(cl$centers, col = 1:2, pch = 8, cex=2)

## random starts do help here with too many clusters
(cl <- Kmeans(x, 5, nstart = 25))
plot(x, col = cl$cluster)
points(cl$centers, col = 1:5, pch = 8)

Run the code above in your browser using DataLab