Skip to content

Commit

Permalink
Merge pull request #83 from rpsdm/fix_gmeans_split
Browse files Browse the repository at this point in the history
fixed GMeans trying to split clusters with one element
  • Loading branch information
collinleiber committed Nov 15, 2023
2 parents 47bc534 + 94f7b38 commit 4afb304
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 0 deletions.
2 changes: 2 additions & 0 deletions clustpy/partition/gmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ def _gmeans(X: np.ndarray, significance: float, n_clusters_init: int, max_n_clus
n_clusters_old = n_clusters
for c in range(n_clusters_old):
ids_in_cluster = np.where(labels == c)[0]
if ids_in_cluster.shape[0] < 2:
continue
# Split cluster into two
labels_split, centers_split, _ = _execute_two_means(X[ids_in_cluster], [np.arange(ids_in_cluster.shape[0])], 0,
np.array([centers[c]]), n_split_trials, random_state)
Expand Down
1 change: 1 addition & 0 deletions clustpy/partition/xmeans.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ def _execute_two_means(X: np.ndarray, ids_in_each_cluster: list, cluster_id_to_s
The resuling cluster centers,
The Kmeans error of the clustering result
"""
assert X.shape[0] >= 2, "X must contain at least 2 elements"
# Prepare cluster for splitting
old_center = centers[cluster_id_to_split, :]
reduced_centers = np.delete(centers, cluster_id_to_split, axis=0)
Expand Down

0 comments on commit 4afb304

Please sign in to comment.