ML Algo Basics
ML Algo Basics
Public
Common ML Algorithms Basics »
Agenda
3 K-Nearest-Neighbor
4 Questions
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 2
Common ML Algorithms Basics
Introduction
What is Learning?
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 4
Common ML Algorithms Basics » Machine Learning Basics
• Concept Representation
– Model-based: Learner constructs a model (function, concept, . . . )
Regression
Introduction
x
−2 2 4
−10
Linear Regression
• Try to find a linear function fˆ(x) = a · x + b that fits the data best
y
10
x
−2 2 4
−10
x = 2.7, y =?
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 8
Common ML Algorithms Basics » Regression and Gradient Descent
Linear Regression
• Try to find a linear function fˆ(x) = a · x + b that fits the data best
y
10
x
−4 −2 2 4
−10
x = 2.7, y =?
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 8
Common ML Algorithms Basics » Regression and Gradient Descent
Regression
Least Squares Method
y
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 9
Common ML Algorithms Basics » Regression and Gradient Descent
1 x1 x21 · · · xm
1 b1
1 x2 x2 m
· · · x2 b2
2
T
−1 T 1 x3 x2 m
· · · x3 , ~y = b3
p~ = X X X ~y with X =
3
.. .. .. .. .. ..
. . . . . .
1 xn x2n · · · xnm bm
0
y
−2
−4
−6
−6 −4 −2 0 2 4 6
x
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 11
Common ML Algorithms Basics » Regression and Gradient Descent
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 12
Common ML Algorithms Basics » Regression and Gradient Descent
Gradient Descent
What is a Gradient? (general case)
∆y g(x2 ) − g(x1 )
grad(x0 ) = =
∆x x2 − x1
y
4
2
∆y
1
∆x
0 x1 x0 x2 x
−2 −1 0 1 2 3 4 5 6 7 8
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 13
Common ML Algorithms Basics » Regression and Gradient Descent
Gradient Descent
g(xn−1 ) − g(xn−2 )
xn = xn−1 − α ·
xn−1 − xn−2
• Otherwise we go right
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 14
Common ML Algorithms Basics » Regression and Gradient Descent
• Switch dimensions:
b error function
e=
e(an−1 , b) − e(an−2 , b)
an = an−1 − α · b = const
an−1 − an−2
y
10
x
−4 −2 2 4
−10
x = 2.7
y ≈ 8.28
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 16
Common ML Algorithms Basics » Regression and Gradient Descent
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 17
Common ML Algorithms Basics » Regression and Gradient Descent
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 18
Common ML Algorithms Basics » Regression and Gradient Descent
• Also possible:
n n
!
∂ 1 X 2 X
· (yi − (a · xi + b))2 = · −(yi − (a · xi + b))
∂b n n
i=1 i=1
Gradient for parameter b
See: [Link]
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 18
Common ML Algorithms Basics » Regression and Gradient Descent
2 Local
Global
y
Optimum
Optimum
0
−2 −1 0 1 2
x
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 19
Common ML Algorithms Basics » Regression and Gradient Descent
Rastrigin Function
A Nightmare for Gradient Descent
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 20
Common ML Algorithms Basics » Regression and Gradient Descent
Overfitting Underfitting
K-Nearest-Neighbor
Common ML Algorithms Basics » K-Nearest-Neighbor
K-Nearest-Neighbor
Overview
0.6
0.4
0.2
? x1
0.2 0.4 0.6 0.8
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 23
Common ML Algorithms Basics » K-Nearest-Neighbor
K-Nearest-Neighbor
Distance Metric
⇒ Distance Metrics
• d is a distance function d : (i, j) −→ [0; +∞)
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 24
Common ML Algorithms Basics » K-Nearest-Neighbor
Distance Metrics
Some Examples for Continuous Values
• lr metric:
p
!1/r
X
d(i, j) = (xik − xjk )r
k=1
x2
5
p √
d(i, j) = (7 − 4)2 + (4 − 2)2 = 13
4
a n
de
3 ucli
E
Manhattan
2
d(i, j) = |7 − 4| + |4 − 2| = 5
1
0 x1
0 1 2 3 4 5 6 7 8 9 10
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 26
Common ML Algorithms Basics » K-Nearest-Neighbor
• Simple-Matching-Distance
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 27
Common ML Algorithms Basics » K-Nearest-Neighbor
~b Pn
~
a · i=1 api · bi
cos ^(~a, ~b) = = pPn Pn
k~ak2 k~bk2 2
i=1 (ai ) · i=1 (bi )
2
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 28
Common ML Algorithms Basics » K-Nearest-Neighbor
x2 j1 is closer to i
since
j~2 cos(α1 ) > cos(α2 )
1 —————————
~i cos(0) = 1
cos(90) = 0
j~1
α2
α1
x1
1
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 29
Common ML Algorithms Basics » K-Nearest-Neighbor
• Look at the first k entries and count the occurrences of each class
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 31
Common ML Algorithms Basics » K-Nearest-Neighbor
x1 x2 c d(i, jl )
0.50913594 0.17425407 1 0.06391650
0.38712087 0.12727304 0 0.06686030
0.52462468 0.17319311 1 0.07814578
0.42848188 0.22721030 0 0.08015273
0.46872483 0.03031198 0 0.12114389
0.52246121 0.25967888 1 0.13145373
0.56865867 0.20662444 1 0.13147702
0.53451705 0.25122332 1 0.13186847
0.58228139 0.11870052 1 0.13593389
0.58674444 0.13294919 1 0.13780338
.. .. .. ..
. . . .
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 32
Common ML Algorithms Basics » K-Nearest-Neighbor
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 33
Common ML Algorithms Basics » K-Nearest-Neighbor
K-Nearest-Neighbor
Use Case: Information Retrieval
K-Nearest-Neighbor
Use Case: Information Retrieval (Ctd.)
Docs ? Query
„SAP HANA Memory Sizes“
Some MAGIC
Retrieved
Documents
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 35
Common ML Algorithms Basics » K-Nearest-Neighbor
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 36
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees (Ctd.)
• Switch dimensions
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 37
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees
Example
X Y
Data point 0 0.54 0.93 0.8
Data point 1 0.96 0.86
0.6
Y
Data point 2 0.42 0.67
Data point 3 0.11 0.53
Data point 4 0.64 0.29 0.4
Data point 5 0.27 0.75
Data point 6 0.81 0.63 0.2 0.4 0.6 0.8 1
X
• How to draw the cuts?
– Mean of max and min value ⇒ vcut = (min + max)/2
• When to stop?
– Split until there are maximum two points per box
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 38
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees
Example: The first Cut
• Cut X-axis
Root
X > 0.53
No: Yes:
Node 1 Node 2
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 39
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees
Example: The first Cut (Ctd.)
1
Y
0.8
0.6
0.4
0.2
Node 1 Node 2
X
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 40
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees
Example: Consecutive Cuts
Root
X > 0.53
No: Yes:
Node 1 Node 2
Y > 0.64 Y > 0.61
Yes:
No: Yes: No:
Node 6
Node 3 Node 4 Node 5
X > 0.75
No: Yes:
Node 7 Node 8
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 41
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees
Example: Consecutive Cuts (Ctd.)
1
Y
0.8
N4 N7 N8
0.6
???
0.4
(0.38,0.55)
0.2
N3 N5 X
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 42
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees
Example: Predicting
Root
X > 0.53
No: Yes:
Node 1 Node 2
Y > 0.64 Y > 0.61
Yes:
No: Yes: No:
Node 6
Node 3 Node 4 Node 5
X > 0.75
No: Yes:
Node 7 Node 8
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 43
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees
Example: Predicting (Ctd.)
1
Y
0.8
N4 N7 N8
0.6
Nearest ???
0.4 Neighbor
0.2
N3 N5 X
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 44
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees
Example: Predicting (Ctd.)
Root
X > 0.53
No: Yes:
Node 1 Node 2
Y > 0.64 Y > 0.61
Yes:
No: Yes: No:
Node 6
Node 3 Node 4 Node 5
X > 0.75
No: Yes:
Node 7 Node 8
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 45
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees
Example: Predicting (Ctd.)
1
Y
0.8
N4 N7 N8
0.6
NN ???
0.4
0.2
N3 N5 X
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 46
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees
Example: Predicting (Ctd.)
Root
X > 0.53
No: Yes:
Node 1 Node 2
Y > 0.64 Y > 0.61
Yes:
No: Yes: No:
Node 6
Node 3 Node 4 Node 5
X > 0.75
No: Yes:
Node 7 Node 8
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 47
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees
Example: Predicting (Ctd.)
1
Y
0.8
N4 NN N7 N8
0.6
???
0.4
0.2
N3 N5 X
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 48
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees
Example: Predicting (Ctd.)
Root
X > 0.53
No: Yes:
Node 1 Node 2
Y > 0.64 Y > 0.61
Yes:
No: Yes: No:
Node 6
Node 3 Node 4 Node 5
X > 0.75
No: Yes:
Node 7 Node 8
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 49
Common ML Algorithms Basics » K-Nearest-Neighbor
KD-Trees
A very complex Example
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 50
Common ML Algorithms Basics » K-Nearest-Neighbor
Feature Weighting
• Idea:
– use a weight for each attribute to denote its importance
– E. g. Weighted Euclidean Distance
v
u p
uX
d(i, j) = t wp · (xik − xjk )2
k=1
RELIEF Algorithm
Feature Weighting
Basic idea:
In a local environment around an example x a good attribute p
should
• allow to discriminate x from all examples of different classes
• have the same value for all examples of the same class as x
1
wp ←− wp + · (dp (m, x) − dp (h, x))
r
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 53
Common ML Algorithms Basics » K-Nearest-Neighbor
Instance Weighting
How to choose k?
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 56
Common ML Algorithms Basics » K-Nearest-Neighbor
X
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 57
Common ML Algorithms Basics » K-Nearest-Neighbor
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 58
Common ML Algorithms Basics » K-Nearest-Neighbor
20
15 best k: 8 or 9
Error
10
5
0 5 10 15 20 25 30 35 40 45 50
k
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 59
Common ML Algorithms Basics » K-Nearest-Neighbor
K-Nearest-Neighbor
Advantages and Disadvantages
• Advantages:
– Easy and comprehensible
– It is easy to add new training items (no retraining required)
• Disadvantages:
– Calculating distances to all points is costly
(⇒ Subsampling, KD-Trees)
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 60
Common ML Algorithms Basics » Questions
Any Questions?
c 2018 SAP SE or an SAP affiliate company. All rights reserved. | Public Daniel Wehner 61
Thank you for your attention!
Contact:
Daniel Wehner, SAP SE