G03EFF (PDF version)
G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

NAG Library Routine Document


Note:  before using this routine, please read the Users' Note for your implementation to check the interpretation of bold italicised terms and other implementation-dependent details.

+ Contents

    1  Purpose
    7  Accuracy

1  Purpose

G03EFF performs K-means cluster analysis.

2  Specification

REAL (KIND=nag_wp)  X(LDX,M), CMEANS(LDC,NVAR), WT(*), CSS(K), CSW(K), WK(N+2*K)

3  Description

Given n objects with p variables measured on each object, xij, for i=1,2,,n and j=1,2,,p, G03EFF allocates each object to one of K groups or clusters to minimize the within-cluster sum of squares:
k=1KiSkj=1p xij-x-kj 2,
where Sk is the set of objects in the kth cluster and x-kj is the mean for the variable j over cluster k. This is often known as K-means clustering.
In addition to the data matrix, a K by p matrix giving the initial cluster centres for the K clusters is required. The objects are then initially allocated to the cluster with the nearest cluster mean. Given the initial allocation, the procedure is to iteratively search for the K-partition with locally optimal within-cluster sum of squares by moving points from one cluster to another.
Optionally, weights for each object, wi, can be used so that the clustering is based on within-cluster weighted sums of squares:
k=1KiSkj=1pwi xij-x~kj 2,
where x~kj is the weighted mean for variable j over cluster k.
The routine is based on the algorithm of Hartigan and Wong (1979).

4  References

Everitt B S (1974) Cluster Analysis Heinemann
Hartigan J A and Wong M A (1979) Algorithm AS 136: A K-means clustering algorithm Appl. Statist. 28 100–108
Kendall M G and Stuart A (1976) The Advanced Theory of Statistics (Volume 3) (3rd Edition) Griffin
Krzanowski W J (1990) Principles of Multivariate Analysis Oxford University Press

5  Parameters

1:     WEIGHT – CHARACTER(1)Input
On entry: indicates if weights are to be used.
No weights are used.
Weights are used and must be supplied in WT.
Constraint: WEIGHT='U' or 'W'.
2:     N – INTEGERInput
On entry: n, the number of objects.
Constraint: N>1.
3:     M – INTEGERInput
On entry: the total number of variables in array X.
Constraint: MNVAR.
4:     X(LDX,M) – REAL (KIND=nag_wp) arrayInput
On entry: Xij must contain the value of the jth variable for the ith object, for i=1,2,,n and j=1,2,,M.
5:     LDX – INTEGERInput
On entry: the first dimension of the array X as declared in the (sub)program from which G03EFF is called.
Constraint: LDXN.
6:     ISX(M) – INTEGER arrayInput
On entry: ISXj indicates whether or not the jth variable is to be included in the analysis. If ISXj>0, the variable contained in the jth column of X is included, for j=1,2,,M.
Constraint: ISXj>0 for NVAR values of j.
7:     NVAR – INTEGERInput
On entry: p, the number of variables included in the sums of squares calculations.
Constraint: 1NVARM.
8:     K – INTEGERInput
On entry: K, the number of clusters.
Constraint: K2.
9:     CMEANS(LDC,NVAR) – REAL (KIND=nag_wp) arrayInput/Output
On entry: CMEANSij must contain the value of the jth variable for the ith initial cluster centre, for i=1,2,,K and j=1,2,,p.
On exit: CMEANSij contains the value of the jth variable for the ith computed cluster centre, for i=1,2,,K and j=1,2,,p.
10:   LDC – INTEGERInput
On entry: the first dimension of the array CMEANS as declared in the (sub)program from which G03EFF is called.
Constraint: LDCK.
11:   WT(*) – REAL (KIND=nag_wp) arrayInput
Note: the dimension of the array WT must be at least N if WEIGHT='W', and at least 1 otherwise.
On entry: if WEIGHT='W', the first n elements of WT must contain the weights to be used.
If WTi=0.0, the ith observation is not included in the analysis. The effective number of observation is the sum of the weights.
If WEIGHT='U', WT is not referenced and the effective number of observations is n.
Constraint: if WEIGHT='W', WTi0.0 and WTi>0.0 for at least two values of i, for i=1,2,,n.
12:   INC(N) – INTEGER arrayOutput
On exit: INCi contains the cluster to which the ith object has been allocated, for i=1,2,,n.
13:   NIC(K) – INTEGER arrayOutput
On exit: NICi contains the number of objects in the ith cluster, for i=1,2,,K.
14:   CSS(K) – REAL (KIND=nag_wp) arrayOutput
On exit: CSSi contains the within-cluster (weighted) sum of squares of the ith cluster, for i=1,2,,K.
15:   CSW(K) – REAL (KIND=nag_wp) arrayOutput
On exit: CSWi contains the within-cluster sum of weights of the ith cluster, for i=1,2,,K. If WEIGHT='U', the sum of weights is the number of objects in the cluster.
16:   MAXIT – INTEGERInput
On entry: the maximum number of iterations allowed in the analysis.
Suggested value: MAXIT=10.
Constraint: MAXIT>0.
17:   IWK(N+3×K) – INTEGER arrayWorkspace
18:   WK(N+2×K) – REAL (KIND=nag_wp) arrayWorkspace
19:   IFAIL – INTEGERInput/Output
On entry: IFAIL must be set to 0, -1​ or ​1. If you are unfamiliar with this parameter you should refer to Section 3.3 in the Essential Introduction for details.
For environments where it might be inappropriate to halt program execution when an error is detected, the value -1​ or ​1 is recommended. If the output of error messages is undesirable, then the value 1 is recommended. Otherwise, if you are not familiar with this parameter, the recommended value is 0. When the value -1​ or ​1 is used it is essential to test the value of IFAIL on exit.
On exit: IFAIL=0 unless the routine detects an error or a warning has been flagged (see Section 6).

6  Error Indicators and Warnings

If on entry IFAIL=0 or -1, explanatory error messages are output on the current error message unit (as defined by X04AAF).
Errors or warnings detected by the routine:
On entry,WEIGHT'W' or 'U',
On entry,WEIGHT='W' and a value of WTi<0.0 for some i,
orWEIGHT='W' and WTi=0.0 for all or all but one values of i.
On entry,the number of positive values in ISX does not equal NVAR.
On entry, at least one cluster is empty after the initial assignment. Try a different set of initial cluster centres in CMEANS and also consider decreasing the value of K. The empty clusters may be found by examining the values in NIC.
Convergence has not been achieved within the maximum number of iterations given by MAXIT. Try increasing MAXIT and, if possible, use the returned values in CMEANS as the initial cluster centres.

7  Accuracy

G03EFF produces clusters that are locally optimal; the within-cluster sum of squares may not be decreased by transferring a point from one cluster to another, but different partitions may have the same or smaller within-cluster sum of squares.

8  Further Comments

The time per iteration is approximately proportional to npK.

9  Example

The data consists of observations of five variables on twenty soils (see Hartigan and Wong (1979)). The data is read in, the K-means clustering performed and the results printed.

9.1  Program Text

Program Text (g03effe.f90)

9.2  Program Data

Program Data (g03effe.d)

9.3  Program Results

Program Results (g03effe.r)

G03EFF (PDF version)
G03 Chapter Contents
G03 Chapter Introduction
NAG Library Manual

© The Numerical Algorithms Group Ltd, Oxford, UK. 2012