# How do I cluster data according to Hamming distance

I've a list of binary strings and I'd like to cluster them in Python, using Hamming distance as metric. I also would like to set the number of centroids (i.e. clusters) to create.

Which algorithm do you suggest?

• you can use k-nearestneighbours, with metric as the hamming distance. a simple google search yielded this result, saedsayad.com/k_nearest_neighbors.htm – Josyula Krishna Sep 5 '18 at 13:03
• Yeah, there are lot of algorithms. The problem is if their Python implementation works correctly on strings, hence the question. – 4ndrew Sep 5 '18 at 13:24
• Not an answer to the question but I would opt for a Bernoulli Mixture model in this case. – degenerate hessian Sep 5 '18 at 14:15
• @JosyulaKrishna k nearest neighbors is not a clustering algorithm. – Has QUIT--Anony-Mousse Sep 10 '18 at 20:39
• @Anony-Mousse thanks for the correction, you are right. apologies. – Josyula Krishna Sep 16 '18 at 10:14

The obvious first thing to try is hierarchical clustering.

Because it can use an arbitrary distance matrix.

Why don't you just try some? That's faster than asking on a web site...

• Indeed I've tried some, like Affinity propagation - in which I can't (don't know how to ) set a fixed number of centroids - or k-medoids (which actually works). Now I've asked here in order to find more solutions. Agglomerative Clustering is a hierarchical clustering algorithm supplied by scikit, but I don't know how to give strings as input, since it accepts couple (x,y) as elements, if I'm not wrong. – 4ndrew Sep 11 '18 at 16:36
• You can also give a distance matrix, as you probably did for affinity propagation. See the documentation. – Has QUIT--Anony-Mousse Sep 11 '18 at 17:13