Lecture 7
Lecture 7
Thomas Lavastida
University of Texas at Dallas
[email protected]
Spring 2023
Agenda
• Assignment 2 Review
• Quick review of Supervised Learning and Linear Regression
• Linear Regression in Python
• Start Regularization and Cross Validation
2
Assignment 2 Review
Supervised Learning and Regression Review
Supervised Learning
• Solution 2 – Regularization
Regularization – Intuition
• True relationship: 𝑦 = 𝛽0 + 𝛽1 𝑥 + 𝜀
𝑦 = 𝛽0 + 𝛽1 𝑥 + 𝛽2 𝑥 2 +… + 𝛽 10 𝑥 10 + 𝜀
Fewer variables
Regularization – Intuition (Cont.)
• Green: 4 3 2
𝑓 ( 𝑥 ) =− 𝑥 +7 𝑥 − 5 𝑥 − 31 𝑥 +30
• Blue: 1
𝑔 ( 𝑥 )=− 𝑓 ( 𝑥)
5
Smaller coefficients
https://www.datacamp.com/community/tutorials/towards-preventing-overfitting-regularization
Regularization – Intuition (Cont.)
• What we need
• Smaller coefficients (coefficient closer to 0)
• Fewer variables (coefficient = 0)
• Regularization
• Modify our original linear regression model
• Add terms to penalize the magnitude of coefficients
Regularization
𝑖=1
𝑛
𝑓 ( 𝝎 )=∑ ( 𝑦 𝑖 − ( 𝝎 𝑥 𝑖 +𝑏 ) ) + 𝑃𝑒𝑛𝑎𝑙𝑡𝑦(𝝎)
2
𝑖=1
Regularization – Two Methods
𝑛
𝑓 ( 𝝎 )=∑ ( 𝑦 𝑖 − ( 𝝎 𝑥 𝑖 +𝑏 ) ) + 𝑃𝑒𝑛𝑎𝑙𝑡𝑦(𝝎)
2
𝑖=1
Shrinkage Penalty
𝑛 𝑘
𝑓 ( 𝝎 )=∑ ( 𝑦 𝑖 − ( 𝝎 𝑥 𝑖 +𝑏 ) ) + 𝜆 ∑ 𝜔
2 2
𝑗
𝑖=1 𝑗=1
• – hyperparameter
• NOT estimated with other parameters
• Set “manually” before model estimation
LASSO
𝑖=1 𝑗=1
where .
• Examples
• LASSO and Ridge –
• Polynomial – degree of polynomial ()
• Model selection?
Labeled Data
• For each model, get performance
measure in test set
Training Set Test Set • Select model with best performance
in test data
Data Data
• Problem
Model Prediction and • “best model?”
Training Evaluation • “best fit for test set!”
Parameter
Estimates
• Overfitting test set
𝑀𝑆 𝐸 1 𝑀𝑆 𝐸 2 𝑀𝑆 𝐸 5
1 1 1 1
2 2 2 2
3 3 3 … 3
4 4 4 4
5 5 5 5
K-Fold Cross Validation
• Small dataset
• Involves more data to train the model
• Reliable by taking the mean of multiple
• Model selection
• Using more data to evaluate performance of each model
CV for Model Selection
• Example:
• Polynomial, grid search for degree, CV
Lowest CV score
…
Grid Search with CV
• Computationally expensive