#title Decision Tree [[TableOfContents]] https://rolkra.github.io/lets-grow-trees/ https://cambiotraining.github.io/intro-machine-learning/decision-trees.html {{{ library(dplyr) library(explore) library(palmerpenguins) library(visNetwork) library(caret) mydata %>% explain_tree(target = val, out = "model") %>% visTree() library(rpart) library(visNetwork) fit <- rpart(y ~ x1 + x2 + x3, data = mydata, control = rpart.control(maxdepth=15, cp = 0.01)) visTree(fit, height = "600px", width = "1024px") tree_plot <- visTree(fit, data = mydata, nodesPopSize = TRUE, minNodeSize = 10, maxNodeSize = 30, height = "800px", width = "2048px") visSave(tree_plot, file = "C:\\R\\Plot\\tree.html") }}} ==== 예제 ==== {{{ library(rpart) library("rpart.utils") library("rpart.plot") fit<-rpart(Reliability~.,data=car.test.frame) rpart.subrules.table(fit) plotcp(fit) rpart.plot(fit, type=4) }}} ==== 예제 ==== {{{ library(rpart) model <- rpart(factor(is_out)~., data=training, method="class") plot(model, uniform=TRUE) text(model, use.n=T) library(rattle) fancyRpartPlot(model) }}} ==== 예제 ==== {{{ #install.packages("tree") library(tree) ir.tr <- tree(Species ~., iris) ir.tr ir.tr1 <- snip.tree(ir.tr, nodes = c(12, 7)) summary(ir.tr1) par(pty = "s") plot(iris[, 3],iris[, 4], type="n", xlab="petal length", ylab="petal width") text(iris[, 3], iris[, 4], c("s", "c", "v")[iris[, 5]]) partition.tree(ir.tr1, add = TRUE, cex = 1.5) # 1D example ir.tr <- tree(Petal.Width ~ Petal.Length, iris) plot(iris[,3], iris[,4], type="n", xlab="Length", ylab="Width") partition.tree(ir.tr, add = TRUE, cex = 1.5) }}} partition.tree()를 사용하면 아래와 같은 그림을 볼 수 있다. attachment:DecisionTree/partition_tree.png ==== Boosting Tree ==== {{{ library(xgboost) }}} --참고: http://freesearch.pe.kr/archives/4405 ==== 참고자료 ==== * http://www.di.fc.ul.pt/~jpn/r/tree/tree.html --> Classification & Regression Trees, 좋네요. * attachment:DecisionTree/DTreesO.pdf ([http://onepager.togaware.com/DTreesO.pdf 원본])--> R 예제가 잘 되어 있다.