-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
ckmeans.py
128 lines (93 loc) · 3.3 KB
/
ckmeans.py
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
def zeroes_float(size):
return [0.0]*size
def zeroes_int(size):
return [0]*size
def zeroes_float_array(size):
output = []
for i in range(size[0]):
output += [[0.0]*size[1]]
return output
def zeroes_int_array(size):
output = []
for i in range(size[0]):
output += [[0]*size[1]]
return output
def ssq(j, i, sum_x, sum_x_sq):
if (j > 0):
muji = (sum_x[i] - sum_x[j-1]) / (i - j + 1)
sji = sum_x_sq[i] - sum_x_sq[j-1] - (i - j + 1) * muji ** 2
else:
sji = sum_x_sq[i] - sum_x[i] ** 2 / (i+1)
return 0 if sji < 0 else sji
def fill_row_k(imin, imax, k, S, J, sum_x, sum_x_sq, N):
if imin > imax: return
i = (imin+imax) // 2
S[k][i] = S[k-1][i-1]
J[k][i] = i
jlow = k
if imin > k:
jlow = int(max(jlow, J[k][imin-1]))
jlow = int(max(jlow, J[k-1][i]))
jhigh = i-1
if imax < N-1:
jhigh = int(min(jhigh, J[k][imax+1]))
for j in range(jhigh, jlow-1, -1):
sji = ssq(j, i, sum_x, sum_x_sq)
if sji + S[k-1][jlow-1] >= S[k][i]: break
# Examine the lower bound of the cluster border
# compute s(jlow, i)
sjlowi = ssq(jlow, i, sum_x, sum_x_sq)
SSQ_jlow = sjlowi + S[k-1][jlow-1]
if SSQ_jlow < S[k][i]:
S[k][i] = SSQ_jlow
J[k][i] = jlow
jlow += 1
SSQ_j = sji + S[k-1][j-1]
if SSQ_j < S[k][i]:
S[k][i] = SSQ_j
J[k][i] = j
fill_row_k(imin, i-1, k, S, J, sum_x, sum_x_sq, N)
fill_row_k(i+1, imax, k, S, J, sum_x, sum_x_sq, N)
def fill_dp_matrix(data, S, J, K, N):
sum_x = zeroes_float(N)
sum_x_sq = zeroes_float(N)
# median. used to shift the values of x to improve numerical stability
shift = data[N//2]
for i in range(N):
if i == 0:
sum_x[0] = data[0] - shift
sum_x_sq[0] = (data[0] - shift) ** 2
else:
sum_x[i] = sum_x[i-1] + data[i] - shift
sum_x_sq[i] = sum_x_sq[i-1] + (data[i] - shift) ** 2
S[0][i] = ssq(0, i, sum_x, sum_x_sq)
J[0][i] = 0
for k in range(1, K):
if (k < K-1):
imin = max(1, k)
else:
imin = N-1
fill_row_k(imin, N-1, k, S, J, sum_x, sum_x_sq, N)
def ckmeans(data, n_clusters):
if n_clusters <= 0:
raise ValueError("Cannot classify into 0 or less clusters")
if n_clusters > len(data):
raise ValueError("Cannot generate more classes than there are data values")
# if there's only one value, return it; there's no sensible way to split
# it. This means that len(ckmeans([data], 2)) may not == 2. Is that OK?
unique = len(set(data))
if unique == 1:
return [data]
data.sort()
n = len(data)
S = zeroes_float_array((n_clusters, n))
J = zeroes_int_array((n_clusters, n))
fill_dp_matrix(data, S, J, n_clusters, n)
clusters = []
cluster_right = n-1
for cluster in range(n_clusters-1, -1, -1):
cluster_left = int(J[cluster][cluster_right])
clusters.append(data[cluster_left:cluster_right+1])
if cluster > 0:
cluster_right = cluster_left - 1
return list(reversed(clusters))