-
Notifications
You must be signed in to change notification settings - Fork 10
/
ml1.Rmd
194 lines (147 loc) · 4.26 KB
/
ml1.Rmd
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
---
title: "machine learning in R"
author: "David Josephs"
---
# Setup
First, we will load all required libraries for these examples:
```{r, message = F, warning = F}
library(caret)
library(FNN)
library(fastNaiveBayes)
library(tidyverse)
library(doParallel)
library(foreach)
library(functional)
library(ROCR)
```
## Data Loading
```{r}
dataurl <- "https://archive.ics.uci.edu/ml/machine-learning-databases/wine/wine.data"
wine <- read_csv(dataurl, col_names = F)
tail(wine)
```
### Data Fixing
We want good column names, so we will follow the names here: [wine analysis link](http://dataaspirant.com/2017/01/09/knn-implementation-r-using-caret-package/)
```{r}
good_cols <- c("class",
"alcohol",
'malic_acid',
'ash',
'alkalinity',
'magnesium',
'total_phenols',
'flavanoids',
'nonflavonoids_phenols',
'proanthocyanins',
'color_intensity',
'hue',
'dilution',
'proline'
)
fix_cols <- function(df){
colnames(df) <- good_cols
df$class <- (df$class)
df
}
wine <- fix_cols(wine)
```
## Train test split
```{r}
set.seed(12345)
## WARNING: Danger function
split <- function(df, p = 0.75, list = FALSE, ...) {
train_ind <- createDataPartition(df[[1]], p = p, list = list)
cat("creating training dataset...\n")
training <<- df[train_ind, ]
cat("completed training dataset, creating test set\n")
test <<- df[-train_ind, ]
cat("done")
}
split(wine)
```
# Exploratory data analysis
Do this
# Picking a knn model
## what is knn
## Picking a value for k
There are other methods which we will explore later using k-folds CV
For now, we will use the ASE to find the best value for k
```{r}
train_knn <- function(k) {
knn(train = training[-1], test = test[-1], cl = as.factor(training$class), k)
}
```
### Metrics for classification
We can't use ASE, so what do we do?
```{r}
conmat <- function(predicted, expected){
cm <- as.matrix(table(Actual = as.factor(expected$class), Predicted = predicted))
cm
}
f1_score <- function(predicted, expected, positive.class="1") {
cm = conmat(predicted, expected)
precision <- diag(cm) / colSums(cm)
recall <- diag(cm) / rowSums(cm)
f1 <- ifelse(precision + recall == 0, 0, 2 * precision * recall / (precision + recall))
#Assuming that F1 is zero when it's not possible compute it
f1[is.na(f1)] <- 0
#Binary F1 or Multi-class macro-averaged F1
ifelse(nlevels(expected) == 2, f1[positive.class], mean(f1))
}
accuracy <- function(predicted, expected){
cm <- conmat(predicted, expected)
sum(diag(cm)/length(test$class))
}
```
lets test it out
```{r}
3 %>% train_knn %>% conmat(test)
3 %>% train_knn %>% accuracy(test)
3 %>% train_knn %>% f1_score(test)
```
### Tuning in parallel!
Lets write a function that then gets us the accuracy and the f1-score for a given model:
```{r}
library(glue)
get_scores <- function(k){
predictions <- train_knn(k)
f1 <- f1_score(predictions,test)
acc <- accuracy(predictions,test)
scores <- c(accuracy = acc, f1 = f1)
scores
}
get_scores(3)
```
Now its time to get wild, we are going to use the foreach and doparallel libraries to get the scores for everyone!
```{r}
registerDoParallel(detectCores() -1)
# parallel KNN for k 1:33
# see how it is literally instantaneous
foreach(i = 1:33, .combine = "rbind", .multicombine = T) %dopar% get_scores(i) %>% as_tibble -> scores
scores$index <- 1:33
library(ggplot2)
library(reshape2)
ggplot(data = melt(scores, id.vars = "index", variable.name = "metric"), aes(index, value)) + geom_line(aes(color = metric))
```
So it looks like we will pick 11 or 13 nearest neighbors. Now lets make this whole process into a little pipeline
## Pipeline: KNN
```{r}
readfile <- function(url) read_csv(url, col_names = F)
split2 <- function(df, p = 0.75, list = FALSE, ...) {
train_ind <- createDataPartition(df[[1]], p = p, list = list)
training <- df[train_ind, ]
test <- df[-train_ind, ]
list(training = training, test = test)
}
res <- split2(wine)
make_knn <- function(k) {
function(lst) {
training <- lst$training
test <- lst$test
train_knn(k)
}
}
knn11 <- make_knn(11)
knn_pipeline <- Compose(readfile, fix_cols, split2,knn11)
knn_pipeline(dataurl)
```