Regression Basics Many slides attributable to: Prof. Mike Hughes - - PowerPoint PPT Presentation

regression basics
SMART_READER_LITE
LIVE PREVIEW

Regression Basics Many slides attributable to: Prof. Mike Hughes - - PowerPoint PPT Presentation

Tufts COMP 135: Introduction to Machine Learning https://www.cs.tufts.edu/comp/135/2020f/ Regression Basics Many slides attributable to: Prof. Mike Hughes Erik Sudderth (UCI) Finale Doshi-Velez (Harvard) James, Witten, Hastie, Tibshirani


slide-1
SLIDE 1

Regression Basics

2

Tufts COMP 135: Introduction to Machine Learning https://www.cs.tufts.edu/comp/135/2020f/

Many slides attributable to: Erik Sudderth (UCI) Finale Doshi-Velez (Harvard) James, Witten, Hastie, Tibshirani (ISL/ESL books)

  • Prof. Mike Hughes
slide-2
SLIDE 2

Objectives for Today (day 02)

  • Understand 3 steps of a regression task
  • Training
  • Prediction
  • Evaluation
  • Metrics: Mean Squared Error vs Mean Absolute Error
  • Try two methods (focus: prediction and evaluation)
  • Linear Regression
  • K-Nearest Neighbors

3

Mike Hughes - Tufts COMP 135 - Spring 2019

slide-3
SLIDE 3

What will we learn?

4

Mike Hughes - Tufts COMP 135 - Spring 2019

Supervised Learning Unsupervised Learning Reinforcement Learning

Data, Label Pairs Performance measure Task data x label y

{xn, yn}N

n=1

Training Prediction Evaluation

slide-4
SLIDE 4

5

Mike Hughes - Tufts COMP 135 - Spring 2019

Task: Regression

Supervised Learning Unsupervised Learning Reinforcement Learning

regression

x y y

is a numeric variable e.g. sales in $$

slide-5
SLIDE 5

6

Mike Hughes - Tufts COMP 135 - Spring 2019

Regression Example: RideShares

Supervised Learning Unsupervised Learning Reinforcement Learning

regression

slide-6
SLIDE 6

Regression Example: RideShare

7

Mike Hughes - Tufts COMP 135 - Spring 2019

slide-7
SLIDE 7

Regression Example: RideShare

8

Mike Hughes - Tufts COMP 135 - Spring 2019

slide-8
SLIDE 8

Regression: Prediction Step

Goal: Predict response y well given features x

  • Input:
  • Output:

9

Mike Hughes - Tufts COMP 135 - Spring 2019

xi , [xi1, xi2, . . . xif . . . xiF ]

Entries can be real-valued, or other numeric types (e.g. integer, binary) Scalar value like 3.1 or -133.7

ˆ y(xi) ∈ R

“features” “covariates” “predictors” “attributes” “responses” “labels”

slide-9
SLIDE 9

Regression: Prediction Step

10

Mike Hughes - Tufts COMP 135 - Spring 2019

>>> # Given: pretrained regression object model >>> # Given: 2D array of features x_NF

>>> x_NF.shape (N, F) >>> yhat_N1 = model.predict(x_NF) >>> yhat_N1.shape (N,1)

slide-10
SLIDE 10

Regression: Training Step

Goal: Given a labeled dataset, learn a function that can perform prediction well

  • Input: Pairs of features and labels/responses
  • Output:

11

Mike Hughes - Tufts COMP 135 - Spring 2019

ˆ y(·) : RF → R

{xn, yn}N

n=1

slide-11
SLIDE 11

12

Mike Hughes - Tufts COMP 135 - Spring 2019

>>> # Given: 2D array of features x_NF >>> # Given: 1D array of responses/labels y_N1

>>> y_N1.shape (N, 1) >>> x_NF.shape (N, F) >>> model = RegressionModel() >>> model.fit(x_NF, y_N1)

Regression: Training Step

slide-12
SLIDE 12

Regression: Evaluation Step

Goal: Assess quality of predictions

  • Input: Pairs of predicted and “true” responses
  • Output: Scalar measure of error/quality
  • Measuring Error: lower is better
  • Measuring Quality: higher is better

13

Mike Hughes - Tufts COMP 135 - Spring 2019

{ˆ y(xn), yn}N

n=1

slide-13
SLIDE 13

Visualizing errors

14

Mike Hughes - Tufts COMP 135 - Spring 2019

slide-14
SLIDE 14

Regression: Evaluation Metrics

  • mean squared error
  • mean absolute error

15

Mike Hughes - Tufts COMP 135 - Spring 2019

1 N

N

X

n=1

|yn − ˆ yn| 1 N

N

X

n=1

(yn − ˆ yn)2

slide-15
SLIDE 15

Regression: Evaluation Metrics

16

Mike Hughes - Tufts COMP 135 - Spring 2019

https://scikit-learn.org/stable/modules/model_evaluation.html

slide-16
SLIDE 16

Linear Regression

Parameters: Prediction: Training: find weights and bias that minimize error

17

Mike Hughes - Tufts COMP 135 - Spring 2019

ˆ y(xi) ,

F

X

f=1

wfxif + b

w = [w1, w2, . . . wf . . . wF ] b

weight vector bias scalar

slide-17
SLIDE 17

Linear Regression predictions as a function of one feature are linear

18

Mike Hughes - Tufts COMP 135 - Spring 2019

slide-18
SLIDE 18

Linear Regression: Training

19

Mike Hughes - Tufts COMP 135 - Spring 2019

min

w,b N

X

n=1

⇣ yn − ˆ y(xn, w, b) ⌘2

Optimization problem: “Least Squares” Goal:

Want to find the weight coefficients w and intercept/bias b that minimizing the mean squared error on the N training examples

slide-19
SLIDE 19

20

Mike Hughes - Tufts COMP 135 - Spring 2019

Linear Regression: Training

min

w,b N

X

n=1

⇣ yn − ˆ y(xn, w, b) ⌘2

Optimization problem: “Least Squares”

An exact solution for optimal values of w, b exists! Formula with many features (F >= 1): We will cover and derive this in next class

[w1 . . . wF b]T = ( ˜ XT ˜ X)−1 ˜ XT y

˜ X =     x11 . . . x1F 1 x21 . . . x2F 1 . . . xN1 . . . xNF 1    

slide-20
SLIDE 20

Nearest Neighbor Regression

21

Mike Hughes - Tufts COMP 135 - Spring 2019

Parameters: none Prediction:

  • find “nearest” training vector to given input x
  • predict y value of this neighbor

Training: none needed (use training data as lookup table)

slide-21
SLIDE 21

K nearest neighbor regression

22

Mike Hughes - Tufts COMP 135 - Spring 2019

Parameters: K : number of neighbors Prediction:

  • find K “nearest” training vectors to input x
  • predict average y of this neighborhood

Training: none needed (use training data as lookup table)

slide-22
SLIDE 22

Nearest Neighbor predictions as a function of one feature are piecewise constant

23

Mike Hughes - Tufts COMP 135 - Spring 2019

slide-23
SLIDE 23

Distance metrics

  • Euclidean
  • Manhattan
  • Many others are possible

24

Mike Hughes - Tufts COMP 135 - Spring 2019

dist(x, x0) = v u u t

F

X

f=1

(xf − x0

f)2

dist(x, x0) =

F

X

f=1

|xf − x0

f|

slide-24
SLIDE 24

Error vs Model Complexity

25

Mike Hughes - Tufts COMP 135 - Spring 2019 Credit: Fig 2.4 ESL textbook

slide-25
SLIDE 25

Summary of Methods

26

Mike Hughes - Tufts COMP 135 - Spring 2019

Function class flexibility Knobs to tune How to interpret? Linear Regression Linear Penalize weights (more next week) Inspect weights K Nearest Neighbors Regression Piecewise constant Number of Neighbors Distance metric Inspect neighbors

slide-26
SLIDE 26

Objectives for Today (day 02)

  • Understand 3 steps of a regression task
  • Training
  • Prediction
  • Evaluation
  • Mean Squared Error
  • Mean Absolute Error
  • Try two methods (focus: prediction and evaluation)
  • Linear Regression
  • K-Nearest Neighbors

27

Mike Hughes - Tufts COMP 135 - Spring 2019

  • Chosen performance metric should

be integrated at training

  • Mean squared error is “easy”, but

not always the right thing to do

slide-27
SLIDE 27

Breakout! Lab for day02

28

Mike Hughes - Tufts COMP 135 - Spring 2019