K-modes algorithm is available here

I want to do clustering of my binary dataset. I need to specify the number of clusters that I need as an output:

KModes (n_clusters, init, n_init, verbose)

My dataset contains 1000 lines and 1000 rows, I want to calculate the distance between my clusters in order to know the exact number of cluster that I need to choose. I don't know how to compare between them. Probably I want to use the hamming distance because it is the most suitable distance to compare between binary data.

Any recommendations that you can give are welcomed.


The are some techniques to choose the number of clusters K.

The most common ones are The Elbow Method and The Silhouette Method.

Elbow Method

In this method, you calculate a score function with different values for K. You can use the Hamming distance like you proposed, or other scores, like dispersion.

Then, you plot them and where the function creates "an elbow" you choose the value for K.

enter image description here

Silhouette Method

This method measure the distance from points in one cluster to the other clusters. Then visually you have silhouette plots that let you choose K.


K=2, silhouette of similar heights but with different sizes. So, potential candidate. enter image description here

K=3, silhouettes of different heights. So, bad candidate. enter image description here

K=4, silhouette of similar heights and sizes. Best candidate. enter image description here

  • $\begingroup$ very thorough answer! $\endgroup$ – Valentin Calomme Dec 9 '19 at 9:28
  • $\begingroup$ Glad to hear it :) $\endgroup$ – BrunoGL Dec 10 '19 at 17:36
  • $\begingroup$ So the same techniques that apply to Kmeans, also work on Kmodes? $\endgroup$ – Carlos Mougan Jan 5 at 19:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.