R cutree on dendrogram with “centroid” and “median” methods yields more clusters than expected












2















I want to test the hierarchical clustering with "centroid" and "median" methods. I have the following R code:



library(dendextend)

iris <- datasets::iris
iris2 <- iris[,-5]
species_labels <- iris[,5]

d_iris <- dist(iris2)
hc_iris <- hclust(d_iris, method = "centroid")

dend <- as.dendrogram(hc_iris)
dend <- color_branches(dend, k=3)

plot(dend,
main = "Clustered Iris data set
(the labels give the true flower species)",
horiz = TRUE, nodePar = list(cex = .007))


The number of clusters seems to be larger than the k in the color_branches function.



enter image description here



However, if I do the cutree directly on hc_iris, which is the result of hierarchical clustering:



table(cutree(hc_iris, k=3), iris$Species)


I get 3 clusters, as expected:



   setosa versicolor virginica
50 0 0
0 50 48
0 0 2


But if I apply the cutree function on the dendrogram, the number of clusters is 34:



table(cutree(as.dendrogram(hc_iris), 3), iris$Species)
setosa versicolor virginica
4 0 0
3 0 0
3 0 0
6 0 0
2 0 0
3 0 0
10 0 0
5 0 0
4 0 0
1 0 0
1 0 0
2 0 0
1 0 0
3 0 0
2 0 0
0 3 0
0 27 9
0 12 0
0 2 0
0 3 0
0 1 3
0 2 0
0 0 9
0 0 3
0 0 6
0 0 2
0 0 3
0 0 3
0 0 3
0 0 2
0 0 2
0 0 2
0 0 1
0 0 2


This happens with both "centroid" and "median" methods.










share|improve this question




















  • 1





    I think what you are doing should work. Bug? but a work-around is this. Change your color_branches statement to dend <- color_branches(dend, clusters=cutree(hc_iris,3))

    – G5W
    Dec 31 '18 at 13:20
















2















I want to test the hierarchical clustering with "centroid" and "median" methods. I have the following R code:



library(dendextend)

iris <- datasets::iris
iris2 <- iris[,-5]
species_labels <- iris[,5]

d_iris <- dist(iris2)
hc_iris <- hclust(d_iris, method = "centroid")

dend <- as.dendrogram(hc_iris)
dend <- color_branches(dend, k=3)

plot(dend,
main = "Clustered Iris data set
(the labels give the true flower species)",
horiz = TRUE, nodePar = list(cex = .007))


The number of clusters seems to be larger than the k in the color_branches function.



enter image description here



However, if I do the cutree directly on hc_iris, which is the result of hierarchical clustering:



table(cutree(hc_iris, k=3), iris$Species)


I get 3 clusters, as expected:



   setosa versicolor virginica
50 0 0
0 50 48
0 0 2


But if I apply the cutree function on the dendrogram, the number of clusters is 34:



table(cutree(as.dendrogram(hc_iris), 3), iris$Species)
setosa versicolor virginica
4 0 0
3 0 0
3 0 0
6 0 0
2 0 0
3 0 0
10 0 0
5 0 0
4 0 0
1 0 0
1 0 0
2 0 0
1 0 0
3 0 0
2 0 0
0 3 0
0 27 9
0 12 0
0 2 0
0 3 0
0 1 3
0 2 0
0 0 9
0 0 3
0 0 6
0 0 2
0 0 3
0 0 3
0 0 3
0 0 2
0 0 2
0 0 2
0 0 1
0 0 2


This happens with both "centroid" and "median" methods.










share|improve this question




















  • 1





    I think what you are doing should work. Bug? but a work-around is this. Change your color_branches statement to dend <- color_branches(dend, clusters=cutree(hc_iris,3))

    – G5W
    Dec 31 '18 at 13:20














2












2








2








I want to test the hierarchical clustering with "centroid" and "median" methods. I have the following R code:



library(dendextend)

iris <- datasets::iris
iris2 <- iris[,-5]
species_labels <- iris[,5]

d_iris <- dist(iris2)
hc_iris <- hclust(d_iris, method = "centroid")

dend <- as.dendrogram(hc_iris)
dend <- color_branches(dend, k=3)

plot(dend,
main = "Clustered Iris data set
(the labels give the true flower species)",
horiz = TRUE, nodePar = list(cex = .007))


The number of clusters seems to be larger than the k in the color_branches function.



enter image description here



However, if I do the cutree directly on hc_iris, which is the result of hierarchical clustering:



table(cutree(hc_iris, k=3), iris$Species)


I get 3 clusters, as expected:



   setosa versicolor virginica
50 0 0
0 50 48
0 0 2


But if I apply the cutree function on the dendrogram, the number of clusters is 34:



table(cutree(as.dendrogram(hc_iris), 3), iris$Species)
setosa versicolor virginica
4 0 0
3 0 0
3 0 0
6 0 0
2 0 0
3 0 0
10 0 0
5 0 0
4 0 0
1 0 0
1 0 0
2 0 0
1 0 0
3 0 0
2 0 0
0 3 0
0 27 9
0 12 0
0 2 0
0 3 0
0 1 3
0 2 0
0 0 9
0 0 3
0 0 6
0 0 2
0 0 3
0 0 3
0 0 3
0 0 2
0 0 2
0 0 2
0 0 1
0 0 2


This happens with both "centroid" and "median" methods.










share|improve this question
















I want to test the hierarchical clustering with "centroid" and "median" methods. I have the following R code:



library(dendextend)

iris <- datasets::iris
iris2 <- iris[,-5]
species_labels <- iris[,5]

d_iris <- dist(iris2)
hc_iris <- hclust(d_iris, method = "centroid")

dend <- as.dendrogram(hc_iris)
dend <- color_branches(dend, k=3)

plot(dend,
main = "Clustered Iris data set
(the labels give the true flower species)",
horiz = TRUE, nodePar = list(cex = .007))


The number of clusters seems to be larger than the k in the color_branches function.



enter image description here



However, if I do the cutree directly on hc_iris, which is the result of hierarchical clustering:



table(cutree(hc_iris, k=3), iris$Species)


I get 3 clusters, as expected:



   setosa versicolor virginica
50 0 0
0 50 48
0 0 2


But if I apply the cutree function on the dendrogram, the number of clusters is 34:



table(cutree(as.dendrogram(hc_iris), 3), iris$Species)
setosa versicolor virginica
4 0 0
3 0 0
3 0 0
6 0 0
2 0 0
3 0 0
10 0 0
5 0 0
4 0 0
1 0 0
1 0 0
2 0 0
1 0 0
3 0 0
2 0 0
0 3 0
0 27 9
0 12 0
0 2 0
0 3 0
0 1 3
0 2 0
0 0 9
0 0 3
0 0 6
0 0 2
0 0 3
0 0 3
0 0 3
0 0 2
0 0 2
0 0 2
0 0 1
0 0 2


This happens with both "centroid" and "median" methods.







r hierarchical-clustering dendrogram dendextend






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 31 '18 at 13:03









G5W

22.3k92041




22.3k92041










asked Dec 31 '18 at 12:58









zaigzaig

1348




1348








  • 1





    I think what you are doing should work. Bug? but a work-around is this. Change your color_branches statement to dend <- color_branches(dend, clusters=cutree(hc_iris,3))

    – G5W
    Dec 31 '18 at 13:20














  • 1





    I think what you are doing should work. Bug? but a work-around is this. Change your color_branches statement to dend <- color_branches(dend, clusters=cutree(hc_iris,3))

    – G5W
    Dec 31 '18 at 13:20








1




1





I think what you are doing should work. Bug? but a work-around is this. Change your color_branches statement to dend <- color_branches(dend, clusters=cutree(hc_iris,3))

– G5W
Dec 31 '18 at 13:20





I think what you are doing should work. Bug? but a work-around is this. Change your color_branches statement to dend <- color_branches(dend, clusters=cutree(hc_iris,3))

– G5W
Dec 31 '18 at 13:20












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53987804%2fr-cutree-on-dendrogram-with-centroid-and-median-methods-yields-more-clusters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53987804%2fr-cutree-on-dendrogram-with-centroid-and-median-methods-yields-more-clusters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas