Introduction Supervised Machine Learning
Introduction Supervised Machine Learning
Supervised Machine
Learning: Regression
MC. Luis Angel Galaviz Alejos
MS. LUIS ANGEL GALAVIZ ALEJOS
Introduction
Artificial Intelligence
Data Science end to end
Get data Build Model Deploy Model
Machine Learning Types
Supervised Unsupervised Reinforced
Classification
Regression
Setup
1. Install Python Anaconda
2. Open Spyder by your OS file explorer
Coding Warmup – Problem #1
Problem #1 Sum of elements of an array
We have an vector/array u with N elements. Write a python program
which finds the sum of all its elements.
𝑢 = [ 𝑢1 , 𝑢2 , … , 𝑢𝑖 , … , 𝑢𝑛−1 , 𝑢𝑁 ] 𝑆𝑢𝑚 1, 1, 1, 1 =4
𝑁
𝑆𝑢𝑚([2, −1, 3, 5, 0]) = 9
𝑆𝑢𝑚 𝑢 = 𝑢𝑖
𝑖=1
𝑁 𝑆𝑢𝑚 3 =3
𝑆𝑢𝑚 𝑢 = 𝑢1 + 𝑢𝑖
𝑖=2
𝑆𝑢𝑚([]) = 0
𝑁
𝑆𝑢𝑚 𝑢 = 𝑢1 + 𝑢2 + 𝑢𝑖
𝑖=3
𝑆𝑢𝑚 𝑢 = 𝑢1 + 𝑢2 + … + 𝑢𝑖 + … + 𝑢𝑁−1 + 𝑢𝑁
Coding Warmup – Compare with numpy
Time Spent (ms)
N
Sum Numpy Sum *
10,000 1.037 0.0
50,000 3.989 0.0
100,000 7.933 0.0
200,000 14.962 0.0
300,000 21.936 0.0
400,000 29.924 0.0
* Please be aware that the fact that numpy spent 0 ms means that python’s time package do not has
enough resolution to be able to measure numpy’s sum elapsed time
Coding Warmup – Measuring Error
Problem #2 Measuring Error
We were doing an experiment on our electric engineering lab. According to our
Ohm’s law calculations the voltage should be 100 volts. We assume that our
calculated value is correct. We measured the voltage 10 times with two distinct
voltmeters. Which voltmeter is better?
# Red Yellow
1 104 100
2 103 106
3 97 95
4 103 104
5 97 100
6 102 94
7 98 101
8 98 100
9 100 99
10 101 100
Coding Warmup – Measuring Error
∆𝑦 = 𝑦𝑚𝑒𝑎𝑠𝑢𝑟𝑒𝑑 − 𝑦𝑐𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑒𝑑 𝑦𝑐𝑎𝑙𝑐𝑢𝑙𝑎𝑡𝑒𝑑 = 100 𝑣𝑜𝑙𝑡𝑠
𝑎 = 𝐿𝑖𝑛𝑒 𝑠𝑙𝑜𝑝𝑒
𝑏 = 𝐿𝑖𝑛𝑒 𝑖𝑛𝑡𝑒𝑟𝑐𝑒𝑝𝑡
𝑥 = 𝐼𝑛𝑑𝑒𝑝𝑒𝑛𝑑𝑒𝑛𝑡 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒
𝑓 𝑥 = 𝑦 = 𝐷𝑒𝑝𝑒𝑛𝑑𝑒𝑛𝑡 𝑣𝑎𝑟𝑖𝑎𝑏𝑙𝑒
https://en.wikipedia.org/wiki/Linear_equation
Linear Regression: A Noisy Line
Imagine we have a real world process. Assume
that we are sure the process could be described by
a linear model. However, given measurement
errors we cannot observe 𝒇 𝒙 real values,
instead we observe it with an additional error:
𝑓 𝑥 = 𝑎𝑥 + 𝑏 + 𝐸𝑟𝑟𝑜𝑟
𝑓 𝑥 = 𝑎𝑥 + 𝑏 + 𝑁(𝑚𝑒𝑎𝑛, 𝑣𝑎𝑟)
https://giphy.com/gifs/gradient-O9rcZVmRcEGqI/fullscreen
Linear Regression – Least Squares Regression
We should minimize Residuals Squared Error
SE = σ𝑵 ෝ𝒊 )𝟐
𝒊=𝟏(𝒚𝒊 − 𝒚
Recall that
ෝ𝒊 = 𝒂𝒙𝒊 + 𝒃
𝒚
Substituting 𝑦ො𝑖
𝑵
𝒎𝒊𝒏𝒂,𝒃 ( 𝒚𝒊 − (𝒂𝒙𝒊 + 𝒃) 𝟐 )
𝒊=𝟏
We calculate MSE derivative. We have two variables 𝒂 and 𝒃, hence, two derivatives
𝝏(𝑺𝑬) 𝝏 𝟐
= 𝝏𝒂 σ𝑵
𝒊=𝟏 𝒚𝒊 − 𝒂𝒙𝒊 + 𝒃 ) = σ𝑵
𝒊=𝟏 𝟐(𝒚𝒊 − 𝒂𝒙𝒊 + 𝒃 )(−𝒙𝒊 )
𝝏𝒂
𝝏(𝑺𝑬) 𝝏 𝟐
= σ𝑵
𝒊=𝟏 𝒚𝒊 − 𝒂𝒙𝒊 + 𝒃 ) = σ𝑵
𝒊=𝟏 𝟐(𝒚𝒊 − 𝒂𝒙𝒊 + 𝒃 )(−𝟏)
𝝏𝒃 𝝏𝒃
Linear Regression – LSR Derivatives
𝝏(𝑴𝑺𝑬) 𝝏(𝑴𝑺𝑬)
In order to find the minimum, we set and to zero
𝝏𝒂 𝝏𝒂
𝝏(𝑴𝑺𝑬)
= 𝟎 = σ𝑵 𝑵 𝟐
𝒊=𝟏 𝟐(𝒚𝒊 − 𝒂𝒙𝒊 + 𝒃 )(−𝒙𝒊 ) = σ𝒊=𝟏 𝟐(−𝒙𝒊 𝒚𝒊 + 𝒂𝒙𝒊 + 𝒃𝒙𝒊 )
𝝏𝒂
− σ 𝒙𝒊 𝒚𝒊 + σ 𝒂𝒙𝟐𝒊 + σ 𝒃𝒙𝒊 = 𝟎
𝒂 σ 𝒙𝟐𝒊 + 𝒃 σ 𝒙𝒊 = σ 𝒙𝒊 𝒚𝒊
𝟏) 𝐚 σ 𝒙𝟐𝒊 + 𝒃 σ 𝒙𝒊 = σ 𝒙𝒊 𝒚𝒊
𝝏(𝑴𝑺𝑬)
= 𝟎 = σ𝑵
𝒊=𝟏 𝟐(𝒚𝒊 − 𝒂𝒙𝒊 + 𝒃 )(−𝟏) = σ 𝟐(− 𝒚𝒊 + 𝒂𝒙𝒊 + 𝒃)
𝝏𝒃
− 𝒚𝒊 + 𝒂𝒙𝒊 + 𝒃𝒙𝒊 = 𝟎
σ 𝒂𝒙𝒊 + σ 𝒃 = σ 𝒚𝒊
𝟐) 𝒂 𝒙𝒊 + 𝒃𝑵 = 𝒚𝒊
Linear Regression – LSR Solve
We solve Eq 1 and Eq 2 Simultaneously
𝟏) 𝒂 σ 𝒙𝟐𝒊 + 𝒃 σ 𝒙𝒊 = σ 𝒙𝒊 𝒚𝒊
𝐂 = 𝒙𝟐𝒊 𝐃 = 𝒙𝒊 𝐄 = 𝒙𝒊 𝒚𝒊 𝐅 = 𝒚𝒊
𝟐) 𝒂 𝒙𝒊 + 𝒃𝑵 = 𝒚𝒊
𝑏𝐷2 − 𝑏𝑁𝐶 = 𝐷𝐸 − 𝐶𝐹
𝑏 𝐷2 − 𝑁𝐶 = 𝐷𝐸 − 𝐶𝐹
𝟏)𝒂𝑪 + 𝒃𝑫 = 𝑬 𝟏)𝑫[𝒂𝑪 + 𝒃𝑫 = 𝑬] 𝐷𝐸 − 𝐶𝐹
𝑏=
𝟐) 𝒂𝑫 + 𝒃𝑵 = 𝑭 𝟐) − 𝑪[𝒂𝑫 + 𝒃𝑵 = 𝑭] 𝐷2 − 𝑁𝐶
σ 𝒙𝒊 σ 𝒙𝒊 𝒚𝒊 − σ 𝒙𝟐𝒊 σ 𝒚𝒊
𝑏=
σ 𝒙𝒊 σ 𝒙𝒊 − 𝑵 σ 𝒙𝟐𝒊
𝑵 σ 𝒙 𝒊 𝒚 𝒊 − σ 𝒙 𝒊 σ 𝒚𝒊 σ 𝒙𝟐𝒊 σ 𝒚𝒊 − σ 𝒙𝒊 σ 𝒙𝒊 𝒚𝒊
𝒂= 𝟐 𝒃= 𝟐
𝑵 σ 𝒙𝟐𝒊 − (σ 𝒙𝒊 ) 𝑵 σ 𝒙𝟐𝒊 − (σ 𝒙𝒊 )
Recall that
𝟏 𝟏 𝟏 𝟏
𝒙𝒊 𝒚𝒊 = 𝒙𝒚 ഥ
𝒙𝒊 = 𝒙 ഥ
𝒚𝒊 = 𝒚 𝒙𝟐𝒊 = 𝒙𝟐
𝑵 𝑵 𝑵 𝑵
1
ൗ 2
We multiply both solutions by 1 𝑁
ൗ𝑁2
ഥ𝒚
𝒙𝒚 − 𝒙 ഥ 𝒙𝟐 𝒚
ഥ−𝒙
ഥ 𝒙𝒚
𝒂= 𝒃=
𝒙𝟐 − 𝒙
ഥ𝟐 𝒙𝟐 − 𝒙
ഥ𝟐
Linear Regression – Problem #1
Learning 𝒇(𝒙) = 𝟐𝒙 + 𝟏 + 𝑵(𝟎, 𝟐)
Linear Regression – Problem #2
Learning 𝒇(𝒙) = 𝑺𝒊𝒏(𝒙) + 𝑵(𝟎, 𝟎. 𝟏)
K Nearest Neighbors
(KNN)
KNN – Graphical Intuition
https://www.youtube.com/watch?v=3lp5CmSwrHI
KNN – Graphical Intuition
https://www.youtube.com/watch?v=3lp5CmSwrHI
KNN – Simulated Data
https://www.youtube.com/watch?v=3lp5CmSwrHI