forked from gjwgit/iris
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.R
More file actions
78 lines (55 loc) · 2.25 KB
/
demo.R
File metadata and controls
78 lines (55 loc) · 2.25 KB
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
########################################################################
# Introduce the concept of decision tree model through MLHub
#
# Copyright 2018 Graham.Williams@togaware.com
cat("==========================
Predict Iris Plant Species
==========================
Below we show the predictions of species using the pre-built model.
")
# Load required packages.
suppressMessages(
{
library(rpart) # Model: decision tree rpart().
library(magrittr) # Data pipelines: %>% %<>% %T>% equals().
library(dplyr) # Wrangling: tbl_df(), group_by(), print().
library(rattle) # Support: normVarNames(), riskchart(), errorMatrix().
})
#-----------------------------------------------------------------------
# Load the pre-built model.
#-----------------------------------------------------------------------
load("iris_rpart_caret_model.RData")
model <- m$finalModel
set.seed(1427)
# Load a sample dataset, predict, and display a sample of predictions.
read.csv("data.csv") %T>%
assign('ds', ., envir=.GlobalEnv) %>%
predict(model, newdata=., type="class") %>%
as.data.frame() %>%
cbind(Actual=ds$Species) %>%
set_names(c("Predicted", "Actual")) %>%
select(Actual, Predicted) %>%
mutate(Error=ifelse(Predicted==Actual, "", "<----")) %T>%
{sample_n(., 15) %>% print()} ->
ev
#-----------------------------------------------------------------------
# Produce confusion matrix using Rattle.
#-----------------------------------------------------------------------
cat("\nPress Enter to continue on to the Confusion Matrix: ")
invisible(readChar("stdin", 1))
cat("
================
Confusion Matrix
================
A confusion matrix summarises the performance of the model on this
dataset. The figures here are percentages, aggregating the actual versus
predicted outcomes. The Error column represents the class error.
")
per <- errorMatrix(ev$Actual, ev$Predicted) %T>% print()
# Calculate the overall error percentage.
cat(sprintf("\nOverall error: %.0f%%\n", 100-sum(diag(per), na.rm=TRUE)))
# Calculate the averaged class error percentage.
cat(sprintf("Average class error: %.0f%%\n", mean(per[,"Error"], na.rm=TRUE)))
# No risk chart as we have a multiclass outcome.
cat("\nPress Enter to finish the demonstration: ")
invisible(readChar("stdin", 1))