1. Introduction SC
A Friendly Introduction to Logistic Regression
and the Perceptron Algorithm
Luis Serrano
Linear Regression Logistic Regression Support Vector Machines
Series of 3 videos
(Perceptron algorithm)
3. Example and algorithm SC
What is logistic regression?
What is the perceptron algorithm?
E-mail spam classifier
Spam Non-spam (ham)
Hello grandson,
I made cookies.
Love, Grandma
Buy, l0ts of money,
now, che@p buy
buy free mon3y
‘buy’
spelling mistakes
0 1 2 3
Appearances of the word “buy”
4 5 6
Rule 1: If #appearances of the word ‘buy’ > 2, then spam
spamham
0 1 2 3
Spelling mistakes
4 5 6
Rule 2: If #spelling mistakes > 3, then spam
spamham
Buy: 4, Mistakes: 3
Buy: 1, Mistakes: 2
Buy: 0, Mistakes: 2
Buy: 2, Mistakes: 1
Buy: 4, Mistakes: 1
Buy: 0, Mistakes: 3
Buy: 2, Mistakes: 3
Buy: 0, Mistakes: 1
Buy: 2, Mistakes: 4
Buy: 3, Mistakes: 2
E-mails Buy + Mistakes
7
3
2
3
5
3
5
1
6
5
Rule 3: If #buy + #mistakes > 4, then spam
1
2
3
4
1 2 3 4
Appearances of ‘buy’
Spellingmistakes
5
Rule 3: If #buy + #mistakes > 4, then spam
Buy: 2
Mistakes: 4
Buy: 2
Mistakes: 3
Buy: 4
Mistakes: 3
Buy: 3
Mistakes: 2
Buy: 4
Mistakes: 1
Buy: 3
Mistakes: 0
Buy: 2
Mistakes: 0
Buy: 2
Mistakes: 1
Buy: 1
Mistakes: 0
Buy: 1
Mistakes: 2
Classification goal: split data
Ham
Spam
Perceptron Algorithm
I’m good
Perceptron Algorithm
Get over
here!
Perceptron Algorithm
I’m good
Perceptron Algorithm
Perceptron Algorithm
Get over
here!
I’m good
Perceptron Algorithm
I’m good
Perceptron Algorithm
Get over
here!
Perceptron Algorithm
Get over
here!
Perceptron Algorithm
Get over
here!
Perceptron Algorithm
I’m good
Perceptron Algorithm
Perceptron Algorithm
Get over
here!
I’m good
I’m good
I’m good
I’m good
Perceptron Algorithm
Perceptron
algorithm
Step 3: (repeat 1000 times)
- Pick random point
- If point is correctly classified:
- Do nothing
- If point is incorrectly classified
- Move line towards point
Step 2: Pick a large number.
(number of repetitions, or epochs)
1000
Step 1: Start with a random line
with blue and red sides.
Step 4: Enjoy your line that
separates the data!
Get over
here!
I’m good
4. How to move a line SC
How to move a line
How to move a line
Pivot
How to move a line
Pivot
Rotating and translating
y = 2x + 32x + 3y = 62x + 3y + (-6) = 0
Rotating and translating
2x + 3y + (-6) = 0
Rotating and translating
2x + 3y + (-6) = 0
Rotating and translating
6/2
6/3
x=0
y=6/3
y=0
x=6/2
2x + 3y + (-6) = 0
2x + 3y + (-6) = 0
Rotating and translating
Pivot
6/2
6/3
2x + 3y + (-6) = 0
Get over
here!
Learning rate: 0.1
-0.1-0.1-0.1
1.9x + 2.9y + (-6.1) = 0 Pivot
Pivot
Perceptron Trick
Algorithm
Step 3: Pick a small number.
(learning rate)
0.01
Step 4: (repeat 1000 times)
- Pick random point
- If point is correctly classified:
- Do nothing
- If point is incorrectly classified
- Move line towards point
Step 2: Pick a large number.
(number of repetitions, or epochs)
1000
Step 1: Start with a random line
of equation ax + by + c = 0
Step 5: Enjoy your fitted line!
- If point is correctly classified
- Do nothing
- If point is incorrectly classified
- Add + 0.01 to a
- Add + 0.01 to b
- Add + 0.01 to c
Figure out!
5. Second algorithm SC
Algorithm
Step 3: Pick a small number.
(learning rate)
0.01
Step 4: (repeat 1000 times)
- Pick random point
- If point is correctly classified:
- Do nothing
- If point is incorrectly classified
- Move line towards point
Step 2: Pick a large number.
(number of repetitions, or epochs)
1000
Step 1: Start with a random line
of equation ax + by + c = 0
Step 5: Enjoy your fitted line!
- If point is correctly classified
- Do nothing
- If point is incorrectly classified
- Add + 0.01 to a
- Add + 0.01 to b
- Add + 0.01 to c
Figure out!
Perceptron
trick
Perceptron Trick
2x + 3y -6 = 0
Get over
here!
-6.1
2x + 3y -6.1= 02x + 3y -6 = 0-6.1
Perceptron Trick
2x + 3y -6.1= 01.8
Perceptron Trick
2x + 3y -6.1= 01.8
Perceptron Trick
2x + 3y -6.1= 01.8
Perceptron Trick
2x + 3y -6.1= 01.81.4
Perceptron Trick
2x + 3y -6.1= 01.4
Perceptron Trick
2x + 3y -6.1= 01.42.2
Perceptron Trick
OK, let’s do it again
2x + 3y -6.1= 02x + 3y -6 = 0-6.1
Perceptron Trick
1.8
2x + 3y -6.1= 01.8
Perceptron Trick
2x + 3y -6.1= 01.8
Perceptron Trick
2x + 3y -6.1= 01.8
Perceptron Trick
1.4
2x + 3y -6.1= 01.8
Perceptron Trick
1.4
2x + 3y -6.1= 01.81.4
Perceptron Trick
2x + 3y -6.1= 01.42.2
Perceptron Trick
2x + 3y -6.1= 01.42.2
Perceptron Trick
Perceptron Trick
2x + 3y + -6 = 0
(4,5)
Learning rate: 0.010.01
4
5-0.04
0.010.01
1.96x + 2.95y + -6.01 = 0
-0.05 -0.01
Pivot
Pivot
Perceptron Trick
ax + by + c = 0
(p,q)
Learning rate: 0.01
p
q-0.01p
(a-0.01p)x + (b-0.01q)y + (c-0.01) = 0
-0.01q -0.01
Perceptron Trick
ax + by + c = 0
(p,q)
Learning rate: 0.01
p
q+0.01p +0.01q +0.01
(a+0.01p)x + (b+0.01q)y + (c+0.01) = 0
Perceptron Trick
-2x + -3y + 6 = 0
(4,5)
Learning rate: 0.01
4
5+0.04 +0.05 +0.01
(-1.96)x + (-2.95)y + (6.01) = 0
Correctly and incorrectly classified points
Correctly
classified
Correctly
classified
Incorrectly
classified
Incorrectly
classified
Perceptron
algorithm Step 3: Pick a small number.
(learning rate)
0.01
Step 4: (repeat 1000 times)
- Pick random point (p,q)
- If point is correctly classified:
- Do nothing
- If point is incorrectly classified
- Move line towards point
Step 2: Pick a large number.
(number of repetitions, or epochs)
1000
Step 1: Start with a random line
of equation ax + by + c = 0
Step 5: Enjoy your line!
- If point is correctly classified
- Do nothing
- If point is blue, in the red area
- Subtract 0.01p to a
- Subtract 0.01q to b
- Subtract 0.01 to c
- If point is red, in the blue area
- Add 0.01p to a
- Add 0.01q to b
- Add 0.01 to c
Correctly and incorrectly classified points
Correctly
classified
Correctly
classified
Incorrectly
classified
Incorrectly
classified
Positive and negative regions
2x + 3y - 6 = 0
(3,5)
(1,1)
2(3) + 3(5) - 6 = 15
2(1) + 3(1) - 6 = -1
Positive and negative regions
2x + 3y - 6 = 0
2x + 3y - 6 > 0
Positive region
2x + 3y - 6 < 0
Negative region
Positive and negative regions
-2x - 3y + 6 = 0
-2x - 3y + 6 < 0
-2x - 3y + 6 > 0
Negative region
Positive region
1
2
3
4
1 2 3 4
Appearances of ‘buy’
Spellingmistakes
5
#buy + #mistakes = 4
#buy + #mistakes > 4
#buy + #mistakes < 4
Positive and negative regions
2x + 3y - 6 = 0
2x + 3y -6 > 0
2x + 3y - 6 < 0
Positive (red) region
Negative (blue) region
Correctly and incorrectly classified points
Correctly
classified
Correctly
classified
Incorrectly
classified
Incorrectly
classified
Perceptron
algorithm Step 3: Pick a small number.
(learning rate)
0.01
Step 4: (repeat 1000 times)
- Pick random point (p,q)
- If point is correctly classified:
- Do nothing
- If point is incorrectly classified
- Move line towards point
Step 2: Pick a large number.
(number of repetitions, or epochs)
1000
Step 1: Start with a random line
of equation ax + by + c = 0
Step 5: Enjoy your line!
- If point is correctly classified
- Do nothing
- If point is blue, on red area
- Add 0.01 p to a
- Add 0.01 q to b
- Add 0.01 to c
- If point is, red on blue area
- Subtract 0.01 p to a
- Subtract 0.01 q to b
- Subtract 0.01 to c
- If point is correctly classified
- Do nothing
- If point is blue, and ap+bq+c > 0
- Subtract 0.01p to a
- Subtract 0.01q to b
- Subtract 0.01 to c
- If point is, red and ap+bq+c < 0
- Add 0.01p to a
- Add 0.01q to b
- Add 0.01 to c
7. Gradient descent SC
Challenge 1: Gradient descent
Perceptron Error
00
00
0
0
Perceptron Error
Error = 17
2x +
3y - 6
=
0
(4,5) 2(4) + 3(5) - 6 = 17
(1,1)
2(1) + 3(1) - 6 = -1
Error = 1
Gradient Descent
Same as the perceptron algorithm!
Minimize using calculus (gradient descent)
Bad line Good line
Large error Small error
Gradient Descent
Low error
High error
Perceptron trick!
8. Log-loss error function SC
Challenge 2: Log-loss function
Continuous predictions
A friendly introduction to deep learning and neural networks
Continuous predictions
11
1
11
0
0
0
0
0
1 (spam)
0 (ham)
Continuous predictions
0.5
0.6
0.7
0.4
0.3
70% likely spam40% likely spam
0.8
0.2
Continuous predictions
2x+3y-6
=
0
2x+3y-6
=
1
2x+3y-6
=
2
2x+3y-6
=
3
2x+3y-6
=
-12x+3y-6
=
-2
2x+3y-6
=
-3
Activation function (sigmoid)
1
0
0.5
0.73
0.12
1-2
Continuous predictions
0
1
2
3
-1
-2
-3
Log-Loss Error
0.5
0.6
0.7
0.4
0.3
1 (spam)
0.7 likely spam
0.8
0.2
Log-Loss Error
0.5
0.6
0.7
0.4
0.3
1 (spam)
0.7 likely spam
0.8
0.2
Log-Loss Error
1 (spam)
0.8 likely spam
1 (spam)
0.1 likely spam
Error derivative: 1-0.8 = 0.2
Error derivative: 1-0.1 = 0.9
Log-Loss Error
0 (ham)
0.8 likely spam
Error derivative: 0-0.8 = -0.8
0 (ham)
0.1 likely spam
Error derivative: 0 - 0.1 = -0.1
I’m good
Perceptron Algorithm
Move
away!
Log-loss error
Large log-loss error Small log-loss error
Minimize using calculus (gradient descent)
Bad line Good line
Logistic regression algorithm
Logistic regression
algorithm
Step 3: Pick a small number.
(learning rate)
0.01
Step 4: (repeat 1000 times)
- Pick random point (p,q)
- If point is correctly classified:
- Move line away from point
- If point is incorrectly classified
- Move line towards point
Step 2: Pick a large number.
(number of repetitions, or epochs)
1000
Step 1: Start with a random line
of equation ax + by + c = 0
Step 5: Enjoy your fitted line!
- Add 0.01(y - y)p to a
- Add 0.01(y - y)q to b
- Add 0.01(y - y) to c
^
^
^
9. Conclusion SC
Linear Regression Logistic Regression Support Vector Machines
Series of 3 videos
Thank you!
@luis_likes_math
Subscribe, like,
share, comment!
youtube.com/c/LuisSerrano

Logistic regression