# The right distance for the clustering. Maybe Mahalanobis?

I have to do a cluster analysis and I'm asking which distance should I used.

I know that 99% of the clustering are made using a euclidean distance, but I heard about the Mahalanobis distance and it seems to be better because it takes into account the covariance matrix of the data.

Question : Why the Mahalanobis distance isn't more used ?

For instance with this data (70% of the variance within these 2 Dim) :

The euclidean distance doesn't fit, so does the Mahalanobis distance can better fit ?

Edit : By the euclidean distance doesn't fit I mean the clusters which become apparent haven't a circle shape

• I don't have much to offer except that Mahalanobis distance basically measures the distance between group means. It's good for prediction which cluster a new data point will be in – shadowtalker Dec 19 '13 at 18:56
• Ophelie, if shown above is the real data you want to cluster then I would bet there is hardly any clusters at all. If you don't mind apophenia just cluster as you did, by eye. – ttnphns Dec 19 '13 at 19:38
• Why the Mahalanobis distance isn't more used? In most cases of clustering, using Mahalanobis in place of Euclidean is not much gain. Mahalanobis is Euclidean attuned to the ellipsoid shape of the data cloud. Ellipsoid or circular - the clusters in the cloud can be any shape and orientation. I would be nice to use Mahalanobis if one knew these characteristics (in a form of covariance matrix) for each separate cluster. But you can't know it beforehand! Sooner than the clusters are discovered. – ttnphns Dec 19 '13 at 19:53
• I must agree with @ttnphns, I do not think these data are well-suited for clustering. – Ellis Valentiner Dec 19 '13 at 20:16

The distance measure you use for cluster analysis should depend on your data. For example, in Ecology we frequently use data on species presence/absence/abundance of ecological communities, and use distance (i.e., similarity) measures such as the Sorensen and Bray-Curtis measures.

There should not be anything specifically against using Mahalanobis distance. Euclidean distance may be the most intuitive to use, and perhaps for the field that you are in, it generally works well. However, it does not work well for all datasets. One thing you can do is try different distance measures and different clustering techniques, and compare cophenetic correlations across analyses to see what is showing the pattern best-supported by the data; also, look at the resulting clusters to see what makes sense and is explainable based on existing literature in your field.

Also, there is a relevant post on CrossValidated here - also, a google search for "non-euclidean distance cluster analysis" looks like it brings up some useful results.

Hope that helps a bit!

• Another thing to consider is why you want to use cluster analysis - if you know the groups already, perhaps a classification analysis might be more informative? or a multivariate comparison to test for significant differences? – mtreg Dec 19 '13 at 16:03
• I don't already know the groups – Ophelie Dec 19 '13 at 16:05
• Okay, then Cluster Analysis seems like it would make sense. I apologize - I thought you did since you circled the groups in your diagram, but now I see that you are identifying he patterns that you see through visual representation of the data. – mtreg Dec 19 '13 at 16:07
• Ok. But which distance should I used in this case for instance ? – Ophelie Dec 19 '13 at 16:17
• The best distance measure depends on the data. If I were you, I would try doing the Cluster Analysis with a few different distance measures (at least Euclidean and Mahanalnobis), and see which are the most interpretable, and might be supported by the literature in your field. If you can post about the nature of your data, I or somebody else on here could help think of what distance measures might be most appropriate. Also, read up on the available distance measures in your stats package to see what might make the most sense also. – mtreg Dec 19 '13 at 16:31

Maybe have a look at correlation clustering, which is meant to find clusters that have a non-spherical shape.

If you want to give Mahalanobis a try, note that Gaussian Mixture Model EM clustering does use Mahalanobis distance.