본문 바로가기
스터디/데이터사이언스

[Data Science] Decision Tree in R

by 궁금한 준이 2023. 4. 28.
728x90
반응형
반응형

iris dataset과 rpart를 이용한 예제이다.

iris 데이터의 70%를 이용하여 decision tree를 학습하고 나머지 30%로 test한다.

rm(list=ls())

library(rpart)
library(rattle)
library(caret)

getAccF1 <- function(cm) {
  # Calculate accuracy
  acc <- sum(diag(cm))/sum(cm)
  
  # Calculate precision, recall, and F1-score
  prec <- cm[1,1]/sum(cm[,1])
  rec <- cm[1,1]/sum(cm[1,])
  f1 <- 2 * prec * rec / (prec + rec)
  
  # Return accuracy and F1-score as a named list
  return(list(Accuracy = acc, F1_score = f1))
}

data(iris)

set.seed(42)
# split the training set with 70%
iris.train <- sample(nrow(iris), round(0.7 * nrow(iris)), replace = FALSE)

# train the decision tree
iris.dtree <- rpart(Species~., data=iris, subset=iris.train)

# visualizing the decision tree
drawTreeNodes(iris.dtree)

# contingency matrix
cm <- table(predict(iris.dtree, iris[-iris.train, ], type="class"),
            iris[-iris.train, "Species"])
            
getAccF1(cm)

drawTreeNodes 로 얻은 decision tree
contigency matrix (test set)

 

728x90
반응형