1
$\begingroup$

I need to perform clustering in a given dataset. There are atrributes with numerical as well as categorical values.

What is the best way to convert categorical to numeric value ?

as an example one field is colour and the values are red, green, blue so can I assign a mapping like:

red : 1, green : 2, blue : 3 or red : 11, green : 25, blue : 30

and if I provide a mapping like this will this affect the Euclidian distance for clustering ?

or is there any other way ?

$\endgroup$
5
$\begingroup$

Converting the categorical data into numerical data isn't really meaningful. Different mappings will give you different solutions.

There is an extension of K-means algorithm for categorical data called k-modes. You can read about K-modes in detail here. This article explains the difference between K-modes and converting data into numeric vectors and then running K-means.

$\endgroup$
1
$\begingroup$

I would definitely checkout this question first: K-Means clustering for mixed numeric and categorical data

In case it doesn't help, here is my explanation:

In the case where you have mixed data types (i.e. numerical and categorical), you have several options:

  • turn numerical data into categorical data

You can do that by using binning. If you want to use K-Means for categorical data, you can use hamming distance instead of Euclidean distance.

  • turn categorical data into numerical

Categorical data can be ordered or not. Let's say that you have 'one', 'two', and 'three' as categorical data. Of course, you could transpose them as 1, 2, and 3.

But in most cases, categorical data cannot be ordered nicely. So you can transform into numerical data by using one-hot encoding

  • Combine both using K-prototypes

K-prototypes computes the distance between instances by combining the Euclidean distance between the numerical features and the hamming distance between the categorical features.

$\endgroup$
-1
$\begingroup$

Try to categorize them as efficiently based on what you care about.

If you want to stay true to the color wheel than just use the RGB values for the colors you are using.

However, if not then you can use any mapping, but be careful because it will affect your Euclidean distance even after you normalize the feature (which is highly recommended for K-means).

$\endgroup$
-1
$\begingroup$

You can't really use k means with categorical/nominal data because it's not ranked. In other words, you can't say "green is greater than red" or "green is less than blue." Even if you did want to assign rankings to them (treating them as ordinal), you can't say what the difference between them is. I would recommend using hierarchical clustering or recursive partitioning instead.

$\endgroup$
  • $\begingroup$ K-means is not solely for ranking. It is for grouping. Thus, different categories will group separately, which is what you want. In fact, many tutorials shows very good results using K-means on categorical datasets such as color. But, i do agree that recursive partitioning is a good method to start this problem. $\endgroup$ – JahKnows Mar 28 '17 at 15:22

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