# Convert categorical data to numerical data to compute a distance then

I want to calculate euclidean distances for a dataset I have. However, there are two attributes which are not numbers. One attribute is country(10 different countries), the other one is race(3 races). I think I need to convert these two categories into numbers somehow. I am thinking to use 0, 1, 3 to represents different races. But, I don't think this could give me meaning results. What are the common techniques could be used to transform these attributes?

• Why do you need Euclidean distances? They wouldn't often make sense for categorical variables.
– EdM
Sep 18, 2015 at 19:44
• Why do you need distances at all? What do you want to do with them? If you have mixed types of variables, people typically use Gower's distance (eg, see here). Sep 18, 2015 at 19:57
• Gower coefficient is often used as the (dis)similarity between data points when attributes are mixed (categorical, continuous). But generally it is not a very good idea to try to compute a distance on such mixed attributes because of the problematic issue of reasonable weighting them. Sep 19, 2015 at 8:33

Why should a comparison between races 0 and 1 be considered less dissimilar than comparisons between races 0 and 3 or races 1 and 3?

Consider instead using Gower's generalised similarity measure:

$$d_{jk} = \frac{\sum_{i=1}^m w_{i}d_{ijk}}{\sum_{i=1}^m w_{i}}$$

(here expressed as a dissimilarity as will become apparent shortly). $d_{jk}$ is the dissimilarity between samples $j$ and $k$ where $i$ indexes the $m$ variables in the dataset. The $w_{i}$ are weights that can be assigned to each variable to downweight their contributions to the dissimilarity.

For continuous variables we compute

$$d_{ijk} = 1 - \frac{|x_{ij} - x_{ik}|}{R_i}$$

where $R_i$ is the range of the the $i$th variable. This implies a standardisation of the continuous variables to account for differences in scales. The 1- bit here turns this into a dissimilarity.

For categorical data $d_{ijk}$ is computed as

$$d_{ijk} = \left \{ \begin{array}{lr} 0 & : x_{ij} = x_{ik}\\ 1 & : x_{ij} \neq x_{ik} \end{array} \right.$$

which basically means that if $j$ and $k$ match on the $i$th category then 0 is added to the cumulative dissimilarity over the $m$ variables. If $j$ and $k$ do not match on the $i$th variable then 1 is added to the dissimilarity.

As the dissimilarity is computed for each pair of samples variable by variable, you can use mixed data such as you describe, with each comparison contributing the same amount of information to the final dissimilarity (assuming $w_i = w \: \forall \: i$).

How you should do this (and whether you should do it at all) depends a lot on why you need a dissimilarity measure and what "similarity" means for you.

If you just have categorical data, then @Gavin gave an excellent method that will work very well for e.g. cluster analysis.

But perhaps you have more? You say you have 10 countries. Countries are differently similar to each other: The United States is more like Canada than it is like Laos, for instance. But how dissimilar countries are depends on the characteristics that you are interested in. There is lots of available data on countries and you might be able to use that data to get a better dissimilarity measure. You might even do some preprocessing (e.g. a factor analysis) to get that.