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)
728x90
반응형
'스터디 > 데이터사이언스' 카테고리의 다른 글
[Data Science] Decision Tree - Overfitting (0) | 2023.04.29 |
---|---|
[Data Science] Decision Tree - Information Gain, Gain Ratio (0) | 2023.04.29 |
[Data Science] Missing Values (0) | 2023.04.21 |
[Data Science] Chi-square test. 카이제곱검정 (0) | 2023.04.20 |
[Data Science] Association Rule Mining - Excercises (0) | 2023.04.17 |