Skip to content

Commit

Permalink
found a shorter example for numpy indexing
Browse files Browse the repository at this point in the history
  • Loading branch information
arogozhnikov committed May 15, 2023
1 parent 6c6447f commit b82cafa
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ def kmeans(init_centers, X, n_iterations: int):
centers = init_centers.copy()
for _ in range(n_iterations):
d = cdist(centers, X)
clusters = np.argmin(d, axis=0)
clusters = np.argmin(d, axis=0)
new_centers_sum = np.zeros_like(centers)
clstr_indices = np.tile(clusters[:, None], reps=(1, n_dim))
dim_indices = np.tile(np.arange(n_dim)[None, :], reps=(n_onservations, 1))
np.add.at(new_centers_sum, (clstr_indices, dim_indices), X)
indices_dim = np.arange(n_dim)[None, :]
np.add.at(new_centers_sum, (clusters[:, None], indices_dim), X)
cluster_counts = np.bincount(clusters, minlength=n_clusters)
centers = new_centers_sum / cluster_counts[:, None]
return centers
Expand Down

0 comments on commit b82cafa

Please sign in to comment.