-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathA2-MachineLearningTasks.qmd
More file actions
383 lines (262 loc) · 14.1 KB
/
A2-MachineLearningTasks.qmd
File metadata and controls
383 lines (262 loc) · 14.1 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
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
---
output: html_document
editor_options:
chunk_output_type: console
---
# Introduction to Machine Learning
**Machine Learning (ML)** is about training an algorithm that can perform certain tasks. The general steps to get the trained model include:
1. Get some data (observations/training data)
2. Establish something that should be done with such data (= task)
3. Find an (ML) algorithm that can perform the task
4. Operationalize the goal by an error metric (=loss) that is to be minimized
5. Train the algorithm to achieve a small loss on the data
6. Test the performance of the algorithm
::: callout-note
## Example
1. data = airquality
2. predict Ozone
3. algorithm = lm
4. loss = residual sum of squares
5. m = lm(Ozone \~ ., data = airquality)
6. could predict(m) to data or hold-out
:::
The goal of this course is that you can answer the following questions:
- What are tasks that we can tackle?
- What algorithm exist and how can they be set up / tuned / modified?
- How to define the loss and wow to best train the algorithms?
- How to do the performance evaluation
## Machine Learning Tasks
Typically we can define roughly three types of ML tasks:
- Supervised learning
- Unsupervised learning
- Reinforcement learning
**Supervised learning**, you train algorithms to predict something (classes = classification or values = regression) from some other data (= features), and you provide it with correct examples of the execution of the task (called training data). A linear regression is an example of supervised learning. Given $y = f(x)$ with $x$ our input feature (e.g. precipitation), $y$ our response (growth), and $f$ an unknown function that maps $x \rightarrow y$ . The goal of supervised learning is to train a ML algorithm to approximate $f$ given observed $(x_i, y_i)$ pairs.
**Unsupervised learning**, on the other hand, is when you provide the features, but no examples of the correct execution of the task. Clustering techniques are examples of unsupervised learning. (In the example above, $y$ would be unknown).
**Reinforcement learning** is a technique that mimics a game-like situation. The algorithm finds a solution through trial and error, receiving either rewards or penalties for each action. As in games, the goal is to maximize the rewards. We will talk more about this technique on the last day of the course.
For now, we will focus on the first two tasks, supervised and unsupervised learning ([here](https://www.youtube.com/watch?v=1AVrWvRvfxs) a YouTube video explaining again the difference).
### Test questions
::: {.webex-check .webex-box}
In ML, predictors (or the explaining variables) are often called features: `r webexercises::torf(TRUE)`
In supervised learning the response (y) and the features (x) are known: `r webexercises::torf(TRUE)`
In unsupervised learning, only the features are known: `r webexercises::torf(TRUE)`
In reinforcement learning an agent (ML model) is trained by interacting with an environment: `r webexercises::torf(TRUE)`
```{r}
#| results: asis
#| echo: false
opts <- c(
answer = "Both books can be downloaded for free.",
"Higher model complexity is always better for predicting."
)
cat("Have a look at the two textbooks on ML (Elements of statistical learning and introduction to statistical learning) in our further readings at the end of the GRIPS course - which of the following statements is true?", longmcq(opts))
```
:::
## Unsupervised Learning {#sec-unsupervised}
In unsupervised learning, we want to identify patterns in data without having any examples (supervision) about what the correct patterns / classes are. As an example, consider the iris data set. Here, we have 150 observations of 4 floral traits:
```{r chunk-chapter3-1-iris-plot, fig.width=10, fig.height=4, fig.cap="Trait distributions of iris dataset"}
iris = datasets::iris
colors = hcl.colors(3)
traits = as.matrix(iris[,1:4])
species = iris$Species
image(y = 1:4, x = 1:length(species) , z = traits,
ylab = "Floral trait", xlab = "Individual")
segments(50.5, 0, 50.5, 5, col = "black", lwd = 2)
segments(100.5, 0, 100.5, 5, col = "black", lwd = 2)
```
The observations are from 3 species and indeed those species tend to have different traits, meaning that the observations form 3 clusters.
```{r chunk-chapter3-2"}
pairs(traits, pch = as.integer(species), col = colors[as.integer(species)])
```
However, imagine we don't know what species are, what is basically the situation in which people in the antique have been. The people just noted that some plants have different flowers than others, and decided to give them different names. This kind of process is what unsupervised learning does.
### K-means Clustering
An example for an unsupervised learning algorithm is k-means clustering, one of the simplest and most popular unsupervised machine learning algorithms (see more on this in section "distance based algorithms").
To start with the algorithm, you first have to specify the number of clusters (for our example the number of species). Each cluster has a centroid, which is the assumed or real location representing the center of the cluster (for our example this would be how an average plant of a specific species would look like). The algorithm starts by randomly putting centroids somewhere. Afterwards each data point is assigned to the respective cluster that raises the overall in-cluster sum of squares (variance) related to the distance to the centroid least of all. After the algorithm has placed all data points into a cluster the centroids get updated. By iterating this procedure until the assignment doesn't change any longer, the algorithm can find the (locally) optimal centroids and the data points belonging to this cluster. Note that results might differ according to the initial positions of the centroids. Thus several (locally) optimal solutions might be found.
The "k" in K-means refers to the number of clusters and the 'means' refers to averaging the data-points to find the centroids.
A typical pipeline for using k-means clustering looks the same as for other algorithms. After having visualized the data, we fit a model, visualize the results and have a look at the performance by use of the confusion matrix. By setting a fixed seed, we can ensure that results are reproducible.
```{r chunk_chapter3_9}
set.seed(123)
#Reminder: traits = as.matrix(iris[,1:4]).
kc = kmeans(traits, 3)
print(kc)
```
*Visualizing the results.* Color codes true species identity, symbol shows cluster result.
```{r chunk_chapter3_10}
plot(iris[c("Sepal.Length", "Sepal.Width")],
col = colors[as.integer(species)], pch = kc$cluster)
points(kc$centers[, c("Sepal.Length", "Sepal.Width")],
col = colors, pch = 1:3, cex = 3)
```
We see that there are are some discrepancies. Confusion matrix:
```{r chunk_chapter3_11}
table(iris$Species, kc$cluster)
```
If you want to animate the clustering process, you could run
```{r chunk_chapter3_12, eval=F}
library(animation)
saveGIF(kmeans.ani(x = traits[,1:2], col = colors),
interval = 1, ani.width = 800, ani.height = 800)
```
**Elbow technique** to determine the probably best suited number of clusters:
```{r chunk_chapter3_13}
set.seed(123)
getSumSq = function(k){ kmeans(traits, k, nstart = 25)$tot.withinss }
#Perform algorithm for different cluster sizes and retrieve variance.
iris.kmeans1to10 = sapply(1:10, getSumSq)
plot(1:10, iris.kmeans1to10, type = "b", pch = 19, frame = FALSE,
xlab = "Number of clusters K",
ylab = "Total within-clusters sum of squares",
col = c("black", "red", rep("black", 8)))
```
Often, one is interested in sparse models. Furthermore, higher k than necessary tends to overfitting. At the kink in the picture, the sum of squares dropped enough and k is still low enough. But keep in mind, this is only a rule of thumb and might be wrong in some special cases.
::: column-margin
Information criteria such as AIC or BIC can be also used to select the number of clusters and control complexity.
:::
## Supervised Learning
The two most prominent branches of supervised learning are regression and classification. The basic distinction between the two is that classification is about predicting a categorical variable, and regression is about predicting a continuous variable.
### Regression
The random forest (RF) algorithm is possibly the most widely used machine learning algorithm and can be used for regression and classification. We will talk more about the algorithm later.
For the moment, we want to go through a typical workflow for a supervised regression: First, we visualize the data. Next, we fit the model and lastly we visualize the results. We will again use the iris data set that we used before. The goal is now to predict Sepal.Length based on the information about the other variables (including species).
Fitting the model:
```{r chunk_chapter3_26, results='hide', message=FALSE, warning=FALSE}
library(randomForest)
set.seed(123)
```
Sepal.Length is a numerical variable:
```{r}
str(iris)
hist(iris$Sepal.Length)
```
The randomForest can be used similar to a linear regression model, we can specify the features using the formula syntax (`~.` means that all other variables should be used as features):
```{r chunk_chapter3_27}
m1 = randomForest(Sepal.Length ~ ., data = iris) # ~.: Against all others.
print(m1)
```
::: column-margin
In statistics we would use a linear regression model:
```{r}
mLM = lm(Sepal.Length~., data = iris)
```
:::
As many other ML algorithms, the RF is not interpretable, so we don't get coefficients that connect the variables to the response. But, at least we get the variable importance which is similar to an anova, telling us which variables were the most important ones:
```{r}
varImpPlot(m1)
```
::: column-margin
Our liner model would report linear effects, however, the lm cannot keep up with the flexibility of a random forest!
```{r}
coef(mLM)
```
:::
And the finally, we can use the model to make predictions using the predict method:
```{r chunk_chapter3_28}
plot(predict(m1), iris$Sepal.Length, xlab = "Predicted", ylab = "Observed")
abline(0, 1)
```
To understand the structure of a random forest in more detail, we can use a package from GitHub.
```{r chunk_chapter3_29, message=FALSE, warning=FALSE}
#| fig-width: 12
#| fig-height: 6
reprtree:::plot.getTree(m1, iris)
```
Here, one of the regression trees is shown.
### Classification
With the random forest, we can also do classification. The steps are the same as for regression tasks, but we can additionally see how well it performed by looking at the confusion matrix. Each row of this matrix contains the instances in a predicted class and each column represents the instances in the actual class. Thus the diagonals are the correctly predicted classes and the off-diagonal elements are the falsely classified elements.
Species is a factor with three levels:
```{r}
str(iris)
```
Fitting the model (syntax is the same as for the regression task):
```{r chunk_chapter3_30}
set.seed(123)
library(randomForest)
m1 = randomForest(Species ~ ., data = iris)
print(m1)
varImpPlot(m1)
```
Predictions:
```{r}
head(predict(m1))
```
Confusion matrix:
```{r}
table(predict(m1), as.integer(iris$Species))
```
Our model made a few errors.
Visualizing results ecologically:
```{r chunk_chapter3_32}
plot(iris$Petal.Width, iris$Petal.Length, col = iris$Species, main = "Observed")
plot(iris$Petal.Width, iris$Petal.Length, col = predict(m1), main = "Predicted")
```
Visualizing one of the fitted models:
```{r chunk_chapter3_31, message=FALSE, warning=FALSE}
#| fig-width: 8
#| fig-height: 6
reprtree:::plot.getTree(m1, iris)
```
Confusion matrix:
```{r chunk_chapter3_34,echo=FALSE}
knitr::kable(table(predict(m1), iris$Species))
```
## Exercise - Supervised Learning
<!-- TODO: airqualty -->
```{r}
#| results: asis
#| echo: false
opts <- c(
answer = "Species.",
"Sepal.Width."
)
cat("Using a random forest on the iris dataset, which parameter would be more important (remember there is a function to check this) to predict Petal.Width?", webexercises::longmcq(opts))
```
::: callout-warning
#### Task: Fit random forest
A demonstration with the iris dataset:
```{r, results='hide'}
library(randomForest)
# scale your features if possible (some ML algorithms converge faster with scaled features)
iris_scaled = iris
iris_scaled[,1:4] = scale(iris_scaled[,1:4])
model = randomForest(Species~., data = iris_scaled)
```
RandomForest is not based on a specific data generating model and thus we will not get effect estimates that tell us how the input features affect the response:
```{r}
# no summary method available
print(model)
```
The confusion matrix explains where (for which species) the model makes wrong predictions / classifications on the OOB splits (OOB = out of bag). Each tree in the random forest is trained on a bootstrap of the data (bootstrap = sample with replacement from the original data, on average, each bootstrap will have 66% of the original data). Observations not used in a specific bootstrap are then used to validate the specific tree, bootstrap errors are at the end averaged for the n trees in the random forest.
While we don't get effect estimates as in a `lm`, we get the variable importance which reports how important the specific predictors are:
```{r}
varImpPlot(model)
```
Predictions
```{r}
head(predict(model))
```
The model predicts the species class for each observation
Performance:
```{r}
table(predict(model), as.integer(iris$Species))
```
**Task:**
- predict `Sepal.Length` instead of `Species` (classification -\> regression)
- Plot predicted vs observed (usually used to asses the goodness of the predictions, if the model is good, predicted and observed values should be on one diagonal line)
:::
`r hide("Click here to see the solution")`
Regression:
Random Forest automatically infers the type of the task, so we don't have to change much:
```{r, results='hide'}
model = randomForest(Sepal.Length~., data = iris_scaled)
```
The OOB error is now "% Var explained" which is very similar to a $R^2$:
```{r}
print(model)
```
Plot observed vs predicted:
```{r}
plot(iris_scaled$Sepal.Length, predict(model), xlim = c(-3, 3), ylim = c(-3, 3))
abline(a = c(0, 1))
```
Calculate $R^2$:
```{r}
cor(iris_scaled$Sepal.Length, predict(model))**2
```
`r unhide()`