-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathstat_gap.py
More file actions
60 lines (45 loc) · 1.85 KB
/
Copy pathstat_gap.py
File metadata and controls
60 lines (45 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
Compute the gap statistic for evaluating k-means clustering.
Gap statistic defined in Tibshirani, Walther, Hastie:
Estimating the number of clusters in a data set via the gap statistic
J. R. Statist. Soc. B (2001) 63, Part 2, pp 411-423
Author: Chao Huang (chaohuang.stat@gmail.com)
Last update: 2017-08-14
"""
import numpy as np
import scipy
import scipy.cluster.vq
import scipy.spatial.distance
dst = scipy.spatial.distance.euclidean
"""
installed all the libraries above
"""
def gap(data, refs=None, nrefs=20, ks=range(1, 3)):
"""
Compute the Gap statistic for for evaluating k-means clustering.
Either give a precomputed set of reference distributions in refs as an (n,m,k) scipy array,
or state the number k of reference distributions in nrefs for automatic generation with a
uniformed distribution within the bounding box of data.
Give the list of k-values for which you want to compute the statistic in ks.
"""
shape = data.shape
if refs is None:
tops = np.amax(data, axis=0)
bots = np.amin(data, axis=0)
dists = scipy.matrix(scipy.diag(tops-bots))
rands = scipy.random.random_sample(size=(shape[0], shape[1], nrefs))
for i in range(nrefs):
rands[:, :, i] = rands[:, :, i]*dists+bots
else:
rands = refs
gaps = scipy.zeros((len(ks),))
for (i, k) in enumerate(ks):
(kmc, kml) = scipy.cluster.vq.kmeans2(data, k)
disp = sum([dst(data[m, :], kmc[kml[m], :]) for m in range(shape[0])])
refdisps = scipy.zeros((rands.shape[2],))
for j in range(rands.shape[2]):
(kmc, kml) = scipy.cluster.vq.kmeans2(rands[:, :, j], k)
refdisps[j] = sum([dst(rands[m, :, j], kmc[kml[m], :]) for m in range(shape[0])])
gaps[i] = scipy.mean(scipy.log(refdisps))-scipy.log(disp)
k_opt = gaps.argmax() + 1
return k_opt