# 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()
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter