kmodes: K-Modes Clustering

Description Usage Arguments Details Value Author(s) References Examples

View source: R/kmodes.R

Description

Perform k-modes clustering on categorical data.

Usage

1
kmodes(data, modes, iter.max = 10, weighted = FALSE, fast = TRUE)

Arguments

data

A matrix or data frame of categorical data. Objects have to be in rows, variables in columns.

modes

Either the number of modes or a set of initial (distinct) cluster modes. If a number, a random set of (distinct) rows in data is chosen as the initial modes.

iter.max

The maximum number of iterations allowed.

weighted

Whether usual simple-matching distance between objects is used, or a weighted version of this distance.

fast

Logical Whether a fast version of the algorithm should be applied.

Details

The k-modes algorithm (Huang, 1997) an extension of the k-means algorithm by MacQueen (1967).

The data given by data is clustered by the k-modes method (Huang, 1997) which aims to partition the objects into k groups such that the distance from objects to the assigned cluster modes is minimized.

By default simple-matching distance is used to determine the dissimilarity of two objects. It is computed by counting the number of mismatches in all variables. Alternative this distance is weighted by the frequencies of the categories in data (see Huang, 1997, for details).

If an initial matrix of modes is supplied, it is possible that no object will be closest to one or more modes. In this case less cluster than supplied modes will be returned and a warning is given.

If called using fast = TRUE the reassignment of the data to clusters is done for the entire data set before recomputation of the modes is done. For computational reasons this option should be chosen unless moderate data sizes.

For clustering mixed type data it is referred to kproto.

Value

An object of class "kmodes" which is a list with components:

cluster

A vector of integers indicating the cluster to which each object is allocated.

size

The number of objects in each cluster.

modes

A matrix of cluster modes.

withindiff

The within-cluster simple-matching distance for each cluster.

iterations

The number of iterations the algorithm has run.

weighted

Whether weighted distances were used or not.

Author(s)

Christian Neumann, [email protected], Gero Szepannek, [email protected]

References

Huang, Z. (1997) A Fast Clustering Algorithm to Cluster Very Large Categorical Data Sets in Data Mining. in KDD: Techniques and Applications (H. Lu, H. Motoda and H. Luu, Eds.), pp. 21-34, World Scientific, Singapore.

MacQueen, J. (1967) Some methods for classification and analysis of multivariate observations. In Proceedings of the Fifth Berkeley Symposium on Mathematical Statistics and Probability, eds L. M. Le Cam \& J. Neyman, 1, pp. 281-297. Berkeley, CA: University of California Press.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
### a 5-dimensional toy-example:

## generate data set with two groups of data:
set.seed(1)
x <- rbind(matrix(rbinom(250, 2, 0.25), ncol = 5),
           matrix(rbinom(250, 2, 0.75), ncol = 5))
colnames(x) <- c("a", "b", "c", "d", "e")

## run algorithm on x:
(cl <- kmodes(x, 2))

## and visualize with some jitter:
plot(jitter(x), col = cl$cluster)
points(cl$modes, col = 1:5, pch = 8)

Example output

Loading required package: MASS
K-modes clustering with 2 clusters of sizes 53, 47

Cluster modes:
  a b c d e
1 0 0 0 0 0
2 2 2 2 2 2

Clustering vector:
  1   2   3   4   5   6   7   8   9  10  11  12  13  14  15  16  17  18  19  20 
  1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
 21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40 
  1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1   1 
 41  42  43  44  45  46  47  48  49  50  51  52  53  54  55  56  57  58  59  60 
  1   1   1   1   1   1   1   1   1   2   1   2   2   2   2   2   2   2   2   2 
 61  62  63  64  65  66  67  68  69  70  71  72  73  74  75  76  77  78  79  80 
  2   2   2   2   2   2   2   2   1   2   2   2   2   2   2   2   2   2   2   2 
 81  82  83  84  85  86  87  88  89  90  91  92  93  94  95  96  97  98  99 100 
  2   2   2   1   2   2   2   2   2   1   2   2   2   2   2   2   2   2   2   2 

Within cluster simple-matching distance by cluster:
[1] 116  85

Available components:
[1] "cluster"    "size"       "modes"      "withindiff" "iterations"
[6] "weighted"  

klaR documentation built on March 19, 2018, 5:03 p.m.