Hi, i have the following problem: I am using k-means algorithm for clustering. But instead of using randomized centers, I would like to use centers created by hierarchical clustering. So I want to apply "hclust" on my data set (in this case the iris data), getting a solution by "cutree", calculating the means/centers of the resulting clusters and use these centers as starting points for k-means clusterng. But I have no idea how I calculate the centers of the clusters and how to use them as starting points for the k-means algorithm. Hope you can help. Thanks in advance. Mario -- View this message in context: http://r.789695.n4.nabble.com/Using-centers-of-hierarchical-clustering-for-k-means-tp4690704.html Sent from the R help mailing list archive at Nabble.com.
David L Carlson
2014-May-16 17:07 UTC
[R] Using centers of hierarchical clustering for k-means
This should get you started> set.seed(42) > x <- matrix(rnorm(200, 25, 5), 40, 5) > x.clus <- hclust(dist(x)) > x.g4 <- cutree(x.clus, 4) > x.cent <- aggregate(x, list(x.g4), mean) > x.km <- kmeans(x, x.cent[,-1]) > xtabs(~x.g4+x.km$cluster)x.km$cluster x.g4 1 2 3 4 1 10 0 1 0 2 0 12 2 0 3 0 2 10 0 4 0 0 0 3 ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf Of marioger Sent: Friday, May 16, 2014 7:29 AM To: r-help at r-project.org Subject: [R] Using centers of hierarchical clustering for k-means Hi, i have the following problem: I am using k-means algorithm for clustering. But instead of using randomized centers, I would like to use centers created by hierarchical clustering. So I want to apply "hclust" on my data set (in this case the iris data), getting a solution by "cutree", calculating the means/centers of the resulting clusters and use these centers as starting points for k-means clusterng. But I have no idea how I calculate the centers of the clusters and how to use them as starting points for the k-means algorithm. Hope you can help. Thanks in advance. Mario -- View this message in context: http://r.789695.n4.nabble.com/Using-centers-of-hierarchical-clustering-for-k-means-tp4690704.html Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Sarah Goslee
2014-May-16 17:09 UTC
[R] Using centers of hierarchical clustering for k-means
Well, you could use the group membership from the clustering along with, for instance, aggregate() to get the mean values for each cluster, and pass those to kmeans() using the centers argument as described in the help file. Unless you want medoids instead of centroids, since you didn't specify? If you post a reproducible example including the code for the parts you know how to do, you might get someone willing to fill in the code for the unknown parts. Sarah On Fri, May 16, 2014 at 8:29 AM, marioger <mario_wiegand at gmx.de> wrote:> Hi, > > i have the following problem: I am using k-means algorithm for clustering. > But instead of using randomized centers, I would like to use centers created > by hierarchical clustering. So I want to apply "hclust" on my data set (in > this case the iris data), getting a solution by "cutree", calculating the > means/centers of the resulting clusters and use these centers as starting > points for k-means clusterng. But I have no idea how I calculate the centers > of the clusters and how to use them as starting points for the k-means > algorithm. > > Hope you can help. Thanks in advance. > > Mario > >-- Sarah Goslee http://www.functionaldiversity.org