
Image by Editor
In a recent article, we took a gentle tour of strategies to deal with mixed data. In real-world problems, it is common to have datasets containing a mix of data types. This typically requires particular approaches beyond classical ones that, in most cases, suit homogeneous, numerical data almost exclusively.
Now, it’s time to dive deeper into one of these strategies: Gower’s distance metric.
Gower’s Distance at a Glance
Gower’s distance metric was proposed to effectively calculate the distance or dissimilarity between two data objects, A and B, when their variables have diverse types. While traditional metrics like Manhattan and Euclidean distance can work for data points where all variables are numerical (or in some cases, dichotomical), these metrics fail when both types are mixed among variables, or other types arise, like categorical.
The rationale is pretty straightforward: a “partial distance” is calculated for each variable or feature xk, k=1,…,p, in isolation, using a suitable distance measure for that variable’s type, e.g. Euclidean for numerical, Jaccard index for nominal, and so on. Then, the overall distance between two data points is obtained by averaging variable-wise distances.
| dGower (A, B) = | ∑k=1p ωk · dk |
| ∑k=1p ωk |
where p is the total number of variables, ωk is the weight of the kth variable, and dk is the partial distance between A and B for that variable.
How about seeing how it works with a practical example?
Suppose a real estate agent needs to measure the dissimilarity between two properties, A and B, based on three variables of distinct nature:
- House price (numerical), with house A priced at 250.000 Eur and house B costing 300.000 Eur. Let’s assume the statistical range (max – min difference) of prices in their database is 200.000 Eur.
- House type (categorical): house A is categorized as “chalet” and house B as “flat”.
- Has pool (binary): house A does have a swimming pool (“Yes”) while house B doesn’t (“No”).
Gower’s distance calculation:
First, we calculate partial distances. For the first variable (price), we obtain d1, dividing the absolute value of the difference between the two houses’ prices by the price range: d1 = |250.000-300.000|/200.000 = 50.000/200.000 = 0.25.
For the categorical variable (house type), a simple, set-based rule is applied: if categories are the same, d2=0, otherwise, d2=1. Since both houses are of different types, we have d2=1.
For the third variable (has pool), the logic is similar to the previous categorical variable. Since one house has a pool and the other doesn’t, again we have d3=1.
To calculate the total distance we average the three partial distances. For simplicity, we assume all variables have equal weights, i.e. ωk=1 for all variables:
Gower-Distance(A,B) = (d1 + d2 + d3)/3 = (0.25 + 1 + 1)/3 = 0.75
For interpretability of results, Gower distance should rely on normalized partial distances, such that the overall distance always lies between 0 and 1. In this case, a distance of 0.75 means there is a substantial difference between the two houses we are comparing.
On a final remark, when to use distinct weights in the computation of Gower’s distance? The answer is: it depends. If in your real-world problem, some data variables carry more theoretical importance than others deemed more redundant, you should allocate them larger or smaller weights accordingly. It mostly depends on your specific problem needs.
