Created
January 22, 2018 19:12
-
-
Save GuiMarthe/170903b6c47a2ea5c6b7a8e8a14624e8 to your computer and use it in GitHub Desktop.
A multiclass decision tree example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| library(rpart) | |
| library(tidyverse) | |
| library(ggdendro) | |
| ggplot(data = iris, | |
| aes(Sepal.Length, Petal.Length, color = Species))+ | |
| geom_point() | |
| dt <- rpart(Species ~ Sepal.Length + Petal.Length, | |
| data = iris, | |
| method = 'class') | |
| printcp(dt) | |
| plotcp(dt) #cross validation error | |
| ddata <- dendro_data(dt) | |
| ggplot() + | |
| geom_segment(data = ddata$segments, | |
| aes(x = x, y = y, xend = xend, yend = yend)) + | |
| geom_text(data = ddata$labels, | |
| aes(x = x, y = y, label = label), size = 3, vjust = 0) + | |
| geom_text(data = ddata$leaf_labels, | |
| aes(x = x, y = y, label = label), size = 3, vjust = 1) + | |
| theme_void() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment