MCPcopy Create free account
hub / github.com/dblalock/bolt / kmeans

Function kmeans

experiments/python/utils.py:130–154  ·  view source on GitHub ↗
(X, k, max_iter=16, init='kmc2')

Source from the content-addressed store, hash-verified

128
129@_memory.cache
130def kmeans(X, k, max_iter=16, init='kmc2'):
131 X = X.astype(np.float32)
132
133 # if k is huge, initialize centers with cartesian product of centroids
134 # in two subspaces
135 sqrt_k = int(np.sqrt(k) + .5)
136 if k > 256 and sqrt_k ** 2 == k and init == 'subspaces':
137 print "kmeans: clustering in subspaces first; k, sqrt(k) =" \
138 " {}, {}".format(k, sqrt_k)
139 _, D = X.shape
140 centroids0, _ = kmeans(X[:, :D/2], sqrt_k, max_iter=1)
141 centroids1, _ = kmeans(X[:, D/2:], sqrt_k, max_iter=1)
142 seeds = np.empty((k, D), dtype=np.float32)
143 for i in range(sqrt_k):
144 for j in range(sqrt_k):
145 row = i * sqrt_k + j
146 seeds[row, :D/2] = centroids0[i]
147 seeds[row, D/2:] = centroids1[j]
148 elif init == 'kmc2':
149 seeds = kmc2.kmc2(X, k).astype(np.float32)
150 else:
151 raise ValueError("init parameter must be one of {'kmc2', 'subspaces'}")
152
153 estimator = cluster.MiniBatchKMeans(k, init=seeds, max_iter=max_iter).fit(X)
154 return estimator.cluster_centers_, estimator.labels_
155
156
157def orthonormalize_rows(A):

Callers 2

learn_pqFunction · 0.90
_learn_centroidsFunction · 0.90

Calls 3

formatMethod · 0.80
fitMethod · 0.80
emptyMethod · 0.45

Tested by

no test coverage detected