-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredict_with_ml.R
74 lines (60 loc) · 1.21 KB
/
predict_with_ml.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
library(tree)
library(ggplot2)
library(RColorBrewer)
library(caret)
data("iris")
# set a seed to make randomness reproducable
set.seed(42)
# get 100 random indexes between 1 to 150
indexes <- sample(
x = 1:150,
size = 100
)
print(indexes)
# based on the indexes split iris data to TRAIN & TEST
train <- iris[indexes, ]
test <- iris[-indexes, ]
# create a decision trre model based on train data
model <- tree(
data = train,
formula = Species ~ .
)
# get summary of the created model
summary(model)
# plot tree with text
plot(model)
text(model)
# create a color palatte
palatte <- brewer.pal(3, "Set2")
# create a scatterplot with colored species
plot(
x = iris$Petal.Length,
y = iris$Petal.Width,
pch = 19,
col = palatte[as.numeric(iris$Species)],
main = "IRIS petal Length vs Width",
xlab = "Petal Length",
ylab = "Petal Width"
)
# partition based on species
partition.tree(
tree = model,
label = "Species",
add = TRUE
)
# Predict test data
predictions <- predict(
object = model,
newdata = test,
type = "class"
)
# create a confussion matrix
table(
x = predictions,
y = test$Species
)
# evaluate the predicting result
confusionMatrix(
data = predictions,
reference = test$Species
)