-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpart2.qmd
494 lines (317 loc) · 10.3 KB
/
part2.qmd
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
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
---
title: "Machine Learning Techniques - 2"
subtitle: "ML techniques and Good practices"
bibliography: biblio.bib
csl: nature.csl
format:
revealjs:
title-slide-attributes:
data-state: "hide-menubar"
center: true
transition: slide
slide-number: true
background-transition: fade
controls-layout: bottom-right
menu: false
css: style.css
section-divs: true
simplemenu:
barhtml:
header: "<div class='menubar'><ul class='menu'></ul><div>"
scale: 0.67
revealjs-plugins:
- simplemenu
---
## Table of Contents {data-state="hide-menubar"}
<ul class="menu"><ul>
# A: All models are wrong {data-stack-name="All models are wrong"}
## In-sample and Out-sample
In-sample error: Calculated from data used for training.
Out-sample error: Calculated from unseen data (not used in training)
2 sampling with n = 10
![](img/in_out_errors_k10.png)
## In-sample and Out-sample
Importance of validating using unseen data.
2 sampling with n = 50
![](img/in_out_errors_k50.png)
## In-sample and Out-sample
Difference are lower with larger samples.
2 sampling with n = 500
![](img/in_out_errors_k500.png)
## Mean Squared Error Decomposition
::: columns
:::: column
<div style="font-size: 60%;">
$\text{MSE} = \mathbb{E}[(Y - \hat{Y})^2]$
This can be further decomposed as:
$$
\text{MSE} = \text{Bias}^2 + \text{Variance} + \text{Irreducible Error}
$$
Where:
- $\text{Bias}(\hat{Y}) = \mathbb{E}[\hat{Y}] - Y$
- $\text{Variance} = \mathbb{E}[(\hat{Y} - \mathbb{E}[\hat{Y}])^2]$
</div>
::::
:::: column
![Bias-Variance Decomposition](img/bias-variance-targets.svg)
::::
:::
## Under and Overfitting
::: columns
:::: {.column width="60%"}
![](img/poly_19_overfit.png)
::::
:::: {.column width="40%"}
<div style="font-size: 80%;">
- degree 1: underfit
- ? data true pattern
- Higher Bias
- degree 8-9: overfit
- random error pattern
- Higher Variance
</div>
::::
:::
## Under, Over and "Okay" -fitting
<img src="img/poly_19_overfit_MSE.png" alt="" style="border: 2px solid black; display: block; margin: auto;" width="50%" height="auto">
<div style="font-size: 50%;">
::: columns
:::: column
If **decomposed** we would see
- increased variance (out) on high degree
- increased bias (out) on low degree
- We need a "bias-variance tradeoff"
::::
:::: column
The **Okay-fit** is where the model:
- Learns data pattern
- Can generalize on unseen data
- Does not learn the random error pattern
::::
:::
**WARNING**: Bias variance decomposition does not always make sense.
</div>
## Mandatory ML workflow
::: columns
:::: {.column width="40%"}
![](img/ml_schema1.svg)
::::
:::: {.column width="60%"}
- Training data → in-sample error
- Test data → out-sample error
- Test data should be without bias
- No bias = the same error model
- Not required but recommended: adversarial datasets
::::
:::
## Importance of bias in datasets
![](img/wolf_husky.png)
What is the pattern of wrong prediction ?
## Importance of Explainable AI
Testing is good but explaining is better:
![](img/wrong_husky.png)
## Importance of adversarial datasets
<div style="font-size: 80%;">
**IMPORTANT:** Adversarial datasets should come after testing.
Proper testing is done with the **same underlying error**.
- Why ? We are looking at different bias:
- related to over-learning on the random error
- not related with an error but with new data patterns
- Adversarial datasets are datasets where we can expect a new pattern in data
- Examples:
- Wolf Images from Zoo
- People from other countries
- Process in another factory
</div>
## ML workflow with model selection
VALIDATION IS NOT TESTING
::: columns
:::: {.column width="60%"}
<img src="img/ml_schema2.svg" alt="" style="border: 2px solid black; display: block; margin: auto;" width="100%" height="auto">
::::
:::: {.column width="40%"}
<div style="font-size: 80%;">
- Model selection
- Hyperparameter tuning
- Training parameters
- Introduction of validation data
- Training split
- Re-training the selected model
</div>
::::
:::
## Data-Driven vs Theory Driven
<div style="font-size: 75%;">
::: columns
:::: column
- Data-driven
- Require **lots of data**
- Leverage lots of **algorithms**
- Require **lots of computing power**
- **Importance of Testing** and Validation Framework
- **Hardly explainable**
- **Optimizing**
- Deep Learning / Scikit-learn Pipelines
- Business intelligence / NLP / Image
::::
:::: column
- Theory-driven
- Can work with **few data**
- **Understanding** of a problem
- Require **less computing power**
- **Limited importance of Testing** and Validation
- **Easily explainable**
- **Modelling**
- Field-specific methods and algorithms
- Aerodynamics / Molecule modeling / Genomics
::::
:::
</div>
# B: Learning and Evaluation {data-stack-name="Learn and Eval"}
## ML tasks for prediction
- Classification
- Regression
- Clustering
- Association
## Clustering examples
<div style="font-size: 60%;">
- means: find groups that minimize the within-cluster sum of squares (distance to the centroid)
- Clustering
- Compute and distance matrix (Euclidean)
- Apply an agglomerative clustering (neighbor joining)
</div>
<img src="img/kmans_hclust.png" alt="" style="border: 2px solid black; display: block; margin: auto;" width="100%" height="auto">
## ML tasks for data transformation
- Data Encoding: e.g (one hot, Ordinal)
- Data Embedding:
- Vector-representation of complex object
- ex: Word2Vec / Encoder deep learning architecture
- Data projection: Project onto another space
- Often based on dimensionality reduction techniques
## Learning strategy
- Supervised learning
- Unsupervised
- Reinforcement learning
- Genetic Algorithm
- Transfer learning
## Evaluation / Binary Classification
::: columns
:::: column
<img src="img/Sensitivity_and_specificity_1.01.svg.png" alt="" style="border: 2px solid black; display: block; margin: auto;" width="50%" height="auto">
::::
:::: column
<img src="img/Precisionrecall.svg.png" alt="" style="border: 2px solid black; display: block; margin: auto;" width="50%" height="auto">
::::
:::
<div style="font-size: 60%;">
source: wikipédia
</div>
## Summary metrics for Binary outcome
- Balanced single score : example F1
$$
F1 = 2 \times \frac{\text{precision} \times \text{recall}}{\text{precision} + \text{recall}}
$$
- AUC: Area under curve
- ROC: Recall = f(FPR) = f(1-specificity)
- PRC: Precision = f(Recall)
## Classification metrics
- Confusion Matrix
- Rand Index
## Regression metrics
- Correlation (pearson / spearman)
- Distance metrics
## Clustering Metrics
- Silhouette Score
- Davies-Bouldin Index
## Association Metrics
<div style="font-size: 60%;">
- Support measures the frequency of the rule in the dataset.
$$
\text{Support}(A \Rightarrow B) = \mathbb{P}(A \cap B)
$$
- Confidence measures how often items in $B$ appear in transactions that contain $A$.
$$
\text{Confidence}(A \Rightarrow B) = \mathbb{P}(B | A) = \frac{\mathbb{P}(A \cap B)}{\mathbb{P}(A)}
$$
- Lift: how much more often $A$ and $B$ occur together than expected if they were statistically independent.
$$
\text{Lift}(A \Rightarrow B) = \frac{\mathbb{P}(B | A)}{\mathbb{P}(B)} = \frac{\mathbb{P}(B \cap A)}{\mathbb{P}(A) \mathbb{P}(B)}
$$
</div>
## Projection / Mapping metrics
- Continuity: Local neighborhoods preserved ?
- Mean K-Nearest Neighbors (KNN) Error: distance to centrois before and after the projection ?
## Global Structure Preservation
- Correlation/Error over distance matrix
- Percentage of Variance Explained
# C: ML techniques {data-stack-name="ML techniques"}
## Feature Engineering
- Feature Normalization
- Feature Selection/Extraction
- Feature Transformation
- Feature Categorization
- Feature Embedding/Encoding
## Dimentionality Reduction
<div style="font-size: 80%;">
**Curse of dimensionality**: more features = more parameters
- PCA: Principal component analysis
- Principal components (PCs) are linear expressions of features
- PCs fitted so that sample variance is maximal on the first components
</div>
<img src="img/pca_cancer1.png" alt="" style="border: 2px solid black; display: block; margin: auto;" width="100%" height="auto">
## Dimensionality reduction
2 is nearly enough...
- Here samples are
- Target not used in PCA
<img src="img/pca_cancer2.png" alt="" style="border: 2px solid black; display: block; margin: auto;" width="100%" height="auto">
## Working with Non-Regular Data
<div style="font-size: 60%;">
- Imbalanced Data
- Population bias or sample bias ?
- Downsample ?
- Weighting for training and
- Use adapted algorithm (Tree-based)
- Data with Uncertainties
- Leverage statitical models (Bayesian)
- Sampling
- Averaging
- Missing Data
- Remove samples
- Use algorithm tolerating missing data
- Predict missing data
</div>
## Model Engineering Training Setup
- Loss function and Weights
- Solver / Optimizer
- Weighting
- Other Options (e.g. tree/splits)
## Validation and test
- Training, Validation and Test Dataset
- Cross-validation (K-Fold, Stratification) [scikit-learn]
- Challenging or Adversarial Test Dataset
## k-Fold cross-validation
<img src="img/grid_search_cross_validation.png" alt="" style="border: 2px solid black; display: block; margin: auto;" width="80%" height="auto">
## Class and group in cross-validation
<div style="font-size: 60%;">
- Class: target
- Group: feature
</div>
<img src="img/ml_schema3.png" alt="" style="border: 2px solid black; display: block; margin: auto;" width="70%" height="auto">
## Regularization
- Loss penalty
- L1, L2 and Elastic-Net
- Max Norm Regularization
- (Multivariate) Boundaries for optimization
- Early Stopping (for complex models)
- Drop Out (In Deep Learning)
## Ensemble Learning Methods
- Principle: Aggregate Predictions
- Bagging : Bootstrap aggregating; averages predictions to reduce overfitting. (Random Forest)
- Boosting : Sequentially focuses on misclassified instances to improve accuracy. (Gradient Boosting)
- Voting : Multiple models vote on output; majority or average wins. (Decision Forest)
- Stacking : Learns from model predictions to make a final prediction. (PCA and then regression)
## Stochastic Methods
- Changing the seed in a Random algorithm (SGD)
- Changing the seed for random initialization
- Boostraping
- Others: Monte Carlo simulation (Bayesian sampling)