Skip to content

Commit

Permalink
cleaner code
Browse files Browse the repository at this point in the history
  • Loading branch information
Nico de Vos committed Mar 15, 2019
1 parent b5b1bcb commit 7846021
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 97 deletions.
21 changes: 18 additions & 3 deletions examples/benchmark_kmodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,30 @@


def huang():
KModes(n_clusters=K, init='Huang', n_init=1, verbose=2).fit_predict(data)
KModes(
n_clusters=K,
init='Huang',
n_init=1,
verbose=2
).fit_predict(data)


def huang_ng_dissim():
KModes(n_clusters=K, init='Huang', cat_dissim=ng_dissim, n_init=1, verbose=2).fit_predict(data)
KModes(
n_clusters=K,
init='Huang',
cat_dissim=ng_dissim,
n_init=1,
verbose=2
).fit_predict(data)


def cao():
KModes(n_clusters=K, init='Cao', verbose=2).fit_predict(data)
KModes(
n_clusters=K,
init='Cao',
verbose=2
).fit_predict(data)


if __name__ == '__main__':
Expand Down
13 changes: 4 additions & 9 deletions examples/benchmark_parallel.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,10 @@ def run(task, stop):


if __name__ == '__main__':
print('Running K-Prototypes on 1 to {} cores for {} initialization tries '
'of {} clusters with {}' ' points of {} features'.format(
C, C, K, N_kproto, M))
print(f"Running K-Prototypes on 1 to {C} cores for {C} initialization tries "
f"of {K} clusters with {N_kproto} points of {M} features")
run(_kprototypes, C)

print('\nRunning K-Modes on 1 to {} cores for {} initialization tries '
'of {} clusters with {}' ' points of {} features'.format(
C, C, K, N_kmodes, M))
print(f"\nRunning K-Modes on 1 to {C} cores for {C} initialization tries "
f"of {K} clusters with {N_kmodes} points of {M} features")
run(_kmodes, C)



13 changes: 10 additions & 3 deletions kmodes/kmodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ def k_modes_single(X, n_clusters, n_points, n_attrs, max_iter, dissim, init, ini
init = np.atleast_2d(init).T
assert init.shape[0] == n_clusters, \
"Wrong number of initial centroids in init ({}, should be {})." \
.format(init.shape[0], n_clusters)
.format(init.shape[0], n_clusters)
assert init.shape[1] == n_attrs, \
"Wrong number of attributes in init ({}, should be {})." \
.format(init.shape[1], n_attrs)
.format(init.shape[1], n_attrs)
centroids = np.asarray(init, dtype=np.uint16)
else:
raise NotImplementedError
Expand Down Expand Up @@ -220,7 +220,14 @@ def k_modes_single(X, n_clusters, n_points, n_attrs, max_iter, dissim, init, ini
cost = np.Inf
while itr <= max_iter and not converged:
itr += 1
centroids, moves = _k_modes_iter(X, centroids, cl_attr_freq, membship, dissim, random_state)
centroids, moves = _k_modes_iter(
X,
centroids,
cl_attr_freq,
membship,
dissim,
random_state
)
# All points seen in this iteration
labels, ncost = _labels_cost(X, centroids, dissim, membship)
converged = (moves == 0) or (ncost >= cost)
Expand Down
4 changes: 2 additions & 2 deletions kmodes/kprototypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,13 @@ def k_prototypes_single(Xnum, Xcat, nnumattrs, ncatattrs, n_clusters, n_points,
"({}, should be {}).".format(init[0].shape[0], n_clusters)
assert init[0].shape[1] == nnumattrs, \
"Wrong number of numerical attributes in init ({}, should be {})." \
.format(init[0].shape[1], nnumattrs)
.format(init[0].shape[1], nnumattrs)
assert init[1].shape[0] == n_clusters, \
"Wrong number of initial categorical centroids in init ({}, " \
"should be {}).".format(init[1].shape[0], n_clusters)
assert init[1].shape[1] == ncatattrs, \
"Wrong number of categorical attributes in init ({}, should be {})." \
.format(init[1].shape[1], ncatattrs)
.format(init[1].shape[1], ncatattrs)
centroids = [np.asarray(init[0], dtype=np.float64),
np.asarray(init[1], dtype=np.uint16)]
else:
Expand Down
132 changes: 66 additions & 66 deletions kmodes/tests/test_kmodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,62 +13,62 @@


SOYBEAN = np.array([
[4,0,2,1,1,1,0,1,0,2,1,1,0,2,2,0,0,0,1,0,3,1,1,1,0,0,0,0,4,0,0,0,0,0,0,'D1'],
[5,0,2,1,0,3,1,1,1,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 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 'D1'],
[7, 0, 0, 2, 1, 0, 2, 1, 0, 0, 1, 1, 0, 2, 2, 0, 0, 0, 1, 1, 0, 3, 0, 0, 0, 2, 1, 0, 4, 0, 0, 0, 0, 0, 0, 'D2'],
[0, 1, 2, 0, 0, 1, 1, 1, 1, 2, 1, 0, 0, 2, 2, 0, 0, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 3, 4, 0, 0, 0, 0, 0, 0, 'D3'],
[2, 1, 2, 1, 1, 3, 1, 2, 1, 1, 1, 1, 0, 2, 2, 0, 0, 0, 1, 1, 1, 2, 0, 1, 0, 0, 0, 3, 4, 0, 0, 0, 0, 0, 1, 'D4'],
])
# Drop target column
SOYBEAN2 = SOYBEAN2[:, :35]
Expand Down Expand Up @@ -193,21 +193,21 @@ def test_kmodes_empty_init_cluster_edge_case(self):
# Edge case from: https://github.com/nicodv/kmodes/issues/106,
# due to negative values in all-integer data.
init_vals = np.array([
[ 14, 0, 16, 0, -1, -1, 158, 115],
[ 2, 0, 3, 3, 127, 105, 295, 197],
[ 10, 2, 12, 3, 136, 20, 77, 42],
[ 2, 0, 3, 4, 127, 55, 150, 63],
[ 1, 0, 21, 5, 39, -1, 124, 90],
[ 17, 2, 12, 3, 22, 175, 242, 164],
[ 5, 1, 7, -1, -1, -1, 69, 38],
[ 3, 3, 6, -1, -1, -1, 267, 175],
[ 1, 0, 21, 4, 71, -1, 276, 196],
[ 11, 2, 12, 5, -1, -1, 209, 148],
[ 2, 0, 3, 5, 127, 105, 375, 263],
[ 2, 0, 3, 4, 28, 105, 16, 8],
[ 13, 2, 12, -1, -1, -1, 263, 187],
[ 6, 2, 6, 4, 21, 20, 370, 256],
[ 10, 2, 12, 3, 136, 137, 59, 31]
[14, 0, 16, 0, -1, -1, 158, 115],
[2, 0, 3, 3, 127, 105, 295, 197],
[10, 2, 12, 3, 136, 20, 77, 42],
[2, 0, 3, 4, 127, 55, 150, 63],
[1, 0, 21, 5, 39, -1, 124, 90],
[17, 2, 12, 3, 22, 175, 242, 164],
[5, 1, 7, -1, -1, -1, 69, 38],
[3, 3, 6, -1, -1, -1, 267, 175],
[1, 0, 21, 4, 71, -1, 276, 196],
[11, 2, 12, 5, -1, -1, 209, 148],
[2, 0, 3, 5, 127, 105, 375, 263],
[2, 0, 3, 4, 28, 105, 16, 8],
[13, 2, 12, -1, -1, -1, 263, 187],
[6, 2, 6, 4, 21, 20, 370, 256],
[10, 2, 12, 3, 136, 137, 59, 31]
])
data = np.hstack((init_vals, init_vals))
kmodes_init = KModes(n_clusters=15, init='Huang', verbose=2)
Expand Down

0 comments on commit 7846021

Please sign in to comment.