# Apply a clustering algorithm on categorical data with features of multiple values [duplicate]

Let us I have a people data like gender, age, marital status, education, employment, hobbies.

I want to make clusters of those people, having some similarity/common among them (for example they have common hobby, education, age etc.).

Here there is a sample of my dataset:

I should use an algorithm which works with categorical data like K-Prototypes but I am not sure how to specifically handle the hobbies, because that feature may have many values from 1 to N.

• Have a look at this question, there are some detailed answers: datascience.stackexchange.com/questions/22/… – Romain Reboulleau Oct 2 '19 at 6:14
• @RomainReboulleau Yes! But I can't find an example on Internet of a feature with multiple options from 1 to N as the "hobbies". – Cincue Oct 3 '19 at 3:09

K-means clustering is based on distance. Whenever you are able to define the distance between two values of a categorical feature, it is theoretically possible, yet not always straightforward, to use the algorithm.

The basic idea that I would recommend is then to give yourself distance metrics over each feature. This may not be easy. You may need to manually set the distance matrices. For example, for the Marital Status feature, assuming single is index 0, married is 1 and separated is 2, you could have the following matrix:

$$\begin{pmatrix} 0 & 0.8 & 0.3 \\ 0.8 & 0 & 0.5 \\ 0.3 & 0.5 & 0 \end{pmatrix}$$

If you can't define a relevant distance, you can just have it be 0 if both records have the same feature value, and 1 otherwise.

This would allow you to fully compute the distance between two records in your dataset. From then on, k-means algorithm can be applied as if all were numerical data.

• Kmeans cannot use arbitrary distances. Primarily, it is based on the mean which does not even optimize euclidean distance. K-means nowhere uses pairwise distances, your answer is wrong. – Has QUIT--Anony-Mousse Oct 2 '19 at 15:42
• This may not be efficient, but the mean can be deduced from distances, see this paper (among many others), which also deals with categorical data: arxiv.org/ftp/arxiv/papers/1410/1410.1106.pdf – Romain Reboulleau Oct 2 '19 at 17:00
• My answer may be incomplete, because the method is not straightforward, but computing pairwise distance may be a solution towards using k-means. For instance, from the affinity matrix based on pairwise distance, you could kernelize your data and cluster with k-means in the projected space. But I guess there are many other ways to go further, which is why I didn't develop further. – Romain Reboulleau Oct 2 '19 at 17:08
• Well, that is a different method then though. Yes, you can also use an embedding like MDE. Or you use k-medoids, which does use a distance matrix. But then don't call it k-means when you are not computing a mean because there is no mean of "hobbies". – Has QUIT--Anony-Mousse Oct 3 '19 at 1:41
• And there are a lot of bad papers. An arXiv paper from 2014 with just 2 citations usually is a warning sign that you shouldn't trust it much... Because it likely did not pass peer review. – Has QUIT--Anony-Mousse Oct 3 '19 at 1:44