Learn R Programming

lava (version 1.9.3)

wkm: Weighted K-means

Description

Weighted K-means via Lloyd's algorithm

Usage

wkm(
  x,
  mu,
  data,
  weights = rep(1, NROW(x)),
  iter.max = 20,
  n.start = 5,
  init = "kmpp",
  ...
)

Arguments

x

Data (or formula)

mu

Initial centers (or number centers chosen randomly among x)

data

optional data frmae

weights

Optional weights

iter.max

Max number of iterations

n.start

Number of restarts

init

method to create initial centres (default kmeans++)

...

Additional arguments to lower level functions

Author

Klaus K. Holst

Examples

Run this code
## Two well-separated Gaussian blobs in 2-D
set.seed(1)
x <- rbind(matrix(rnorm(100, mean = -3), ncol = 2),
           matrix(rnorm(100, mean =  3), ncol = 2))
res <- wkm(x, mu = 2)
table(res$cluster)
res$center

## Supply explicit initial centers (as a list)
res2 <- wkm(x, mu = list(c(-3, -3), c(3, 3)))

## Weighted clustering: up-weight the second blob
w <- c(rep(1, 50), rep(10, 50))
res3 <- wkm(x, mu = 2, weights = w)

## Formula interface on a data.frame
wkm(~ Sepal.Length + Sepal.Width, data = iris, mu = 3)

Run the code above in your browser using DataLab