Decision Tree in R

PHOTO EMBED

Sun Jun 08 2025 17:53:10 GMT+0000 (Coordinated Universal Time)

Saved by @wayneinvein

# Load required packages
library(rpart)
library(rpart.plot)
library(ggplot2)
library(caret)

# Prepare data
data(iris)
set.seed(123)
index <- createDataPartition(iris$Species, p = 0.8, list = FALSE)
train <- iris[index, ]; test <- iris[-index, ]

# Train decision tree
model <- rpart(Species ~ ., data = train, method = "class")
rpart.plot(model, main = "Decision Tree", extra = 104)

# Predict and evaluate
pred <- predict(model, test, type = "class")
print(confusionMatrix(pred, test$Species))

# Visualize decision boundaries (for Sepal features)
grid <- expand.grid(
  Sepal.Length = seq(min(iris$Sepal.Length), max(iris$Sepal.Length), 0.1),
  Sepal.Width = seq(min(iris$Sepal.Width), max(iris$Sepal.Width), 0.1)
)
grid$Species <- predict(model, newdata = grid, type = "class")

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point() +
  geom_tile(data = grid, aes(fill = Species), alpha = 0.2) +
  labs(title = "Decision Tree Boundaries (Sepal Features)") +
  theme_minimal()
content_copyCOPY