-
Notifications
You must be signed in to change notification settings - Fork 0
/
IrisDescTree.R
84 lines (64 loc) · 1.51 KB
/
IrisDescTree.R
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Load the data
data(iris)
# Set a seed to make the randomness reproducable
set.seed(42)
# Randomly sample 100 of 150 row indexes
indexes <- sample(
x = 1:150,
size = 100)
# Inspect the random indexes
print(indexes)
# Create a training set from indexes
train <- iris[indexes, ]
# Create a test set from remaining indexes
test <- iris[-indexes, ]
# Load the decision tree package
library(tree)
# Train a decision tree model
model <- tree(
formula = Species ~ .,
data = train)
# Inspect the model
summary(model)
# Visualize the decision tree model
plot(model)
text(model)
# Load color brewer library
library(RColorBrewer)
# Create a color palette
palette <- brewer.pal(3, "Set2")
# Create a scatterplot colored by species
plot(
x = iris$Petal.Length,
y = iris$Petal.Width,
pch = 19,
col = palette[as.numeric(iris$Species)],
main = "Iris Petal Length vs. Width",
xlab = "Petal Length (cm)",
ylab = "Petal Width (cm)")
# Plot the decision boundaries
partition.tree(
tree = model,
label = "Species",
add = TRUE)
# Predict with the model
predictions <- predict(
object = model,
newdata = test,
type = "class")
# Create a confusion matrix
table(
x = predictions,
y = test$Species)
# Load the caret package
library(caret)
# Evaluate the prediction results
confusionMatrix(
data = predictions,
reference = test$Species)
# Set working directory
setwd("/Users/randallhall/Desktop")
# Save tree
save(model, file = "Tree.RData")
# Save training data
save(train, file = "Train.RData")