Hello. Is it possible to choose the distance in the kmeans algorithm? I have m vectors of n components and I want to cluster them using kmeans algorithm but I want to use the Mahalanobis distance or another distance. How can I do it in R? If I use kmeans, I have no option to choose the distance. Thanks in advance, Arnau.
You do realize that Mahalanobis distance is just Euclidean distance on some linear transformation of the variables? So all you need to do is to transform the data you pass to kmeans to 'sphere' the Mahalanobis distance. The K means *algorithms* do depend on Euclidean distance (e.g. in choosing the cluster centres as the centroids), so your initial question makes little sense. You can of course use the criterion with other distances, but you need to develop other algorithms to do so. On Sun, 9 Jul 2006, Arnau Mir wrote:> Hello. > > Is it possible to choose the distance in the kmeans algorithm? > > I have m vectors of n components and I want to cluster them using kmeans > algorithm but I want to use the Mahalanobis distance or another distance. > > How can I do it in R? > If I use kmeans, I have no option to choose the distance. > > Thanks in advance, > > Arnau.-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
> >Hello. > >Is it possible to choose the distance in the kmeans algorithm? > >I have m vectors of n components and I want to cluster them using kmeans >algorithm but I want to use the Mahalanobis distance or another distance. > >How can I do it in R? >If I use kmeans, I have no option to choose the distance. > >Thanks in advance, > >Arnau. > > > >You can use Kmeans from the amap package with several distance measures. # example for L1 and L2: x <- matrix(c(0,0,0,1.5,1,-1), ncol=2, byrow=TRUE) require(amap) Kmeans(x, x[2:3,], method="manhattan") Kmeans(x, x[2:3,], method="euclidean") Cheers, Timo -- Timo Becker Phonetics Austrian Academy of Sciences Acoustics Research Institute