-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeModel.R
More file actions
29 lines (21 loc) · 747 Bytes
/
TreeModel.R
File metadata and controls
29 lines (21 loc) · 747 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
library(rpart)
library(rpart.plot)
data(iris)
set.seed(123)
# Using sample_frac directly to get a fraction of the data
training_indices <- sample(nrow(iris), 0.7 * nrow(iris))
train_data <- iris[training_indices, ]
test_data <- iris[-training_indices, ]
# Check the structure of train_data
#str(train_data)
treemodel <- rpart(Species ~., data = train_data, method = "class")
rpart.plot(treemodel)
prediction <- predict(treemodel, newdata = test_data)
print(prediction)
# Extract predicted class probabilities
predicted_class <- colnames(prediction)[max.col(prediction)]
print(predicted_class)
conf_matrix <- table(predicted_class, test_data$Species)
print(conf_matrix)
accuracy <- sum(diag(conf_matrix)) / sum(conf_matrix)
print(accuracy)