Clustering clusters in Python OR merge clusters to reduce number of groups (Python)

Viewed 386

I'm dealing with a set of 173k points labelized into 160 groups. I'd like to reduce this number of groups/clusters by merging the closest (to 9 or 10 groups). I've searched for sklearn or alike libraries, but with no success.

I guess it's simply clustering by knn not points but groups of same labelized points.

As graphics are most of the time better explanations, here is a simplified version of what I'd like : merging cluster 0 and cluster 1 as they are close in distance

Thanks for the help

2 Answers

Have you tried k-Means Clustering?

There, you can define the number of clusters n_clusters - which could be 10 in your case.

You have:

  • X - data
  • labels - current cluster labels list

To cluster clusters just add labels as a new column:

from sklearn.preprocess import scale
X = pd.DateFarme(X)
weight = 1
X['current_labels'] = scale(labels) * weight
# cluster again:

To merge cluster 3 to 2:

X['current_labels'] = labels
X[X['current_labels'] == 3] = 2
Related