85
Machine Learning: Introduction to Supervised Learning

Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Machine Learning: Introduction to Supervised

Learning

Page 2: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Announcements

● Please fill out course evaluations!● Can pick up exams after class

● Extra exams will be in Gates 160

● Criteria posted online later today● All regrade requests must be made by this

Friday

● Aubrey's Office Hours:● Wednesday: 12-5PM● Thursday: 12-5PM● Friday: 12-5PM (for regrade requests)

Page 3: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet Price

500 $100,000

800 $200,000

400 $120,000

550 $180,000

700 $250,000

900 $300,000

300 $120,000

500 $200,000

475 $120,000

● Given housing price data, how can we make generalizations from that data in order to predict housing prices?

Page 4: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 5: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 6: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

600?

Page 7: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

600 ft2?

Page 8: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

600 ft2?

Page 9: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

600 ft2?

$250,000

Page 10: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

● The line we picked “looked good”, but how do we decide what line to pick?

Page 11: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 12: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 13: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 14: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Picking Lines

● One idea is to minimize the error between our data points and line we choose.

Page 15: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 16: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 17: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 18: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Picking Lines

● One idea is to minimize the error between our data points and line we choose.● Error = sum of the squared distances

between the line and our data points

● How do formulate this mathematically?

Page 19: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Picking Lines● The function for a line is:

total error=∑ ( yn− f (xn))2

f (x)=ax+b

● Let's represent our data points symbolically as:

square footage=xnprice= yn

● The error between one of our data points and our prediction for that data point is:

error=( yn− f (xn))2

Page 20: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Picking Lines

● The overall error of our line is:

total error=∑ ( yn− f (xn))2

● Now that we have a function for our error, we can evaluate the quality of a line we choose

● This still doesn't tell us how to pick a line

Page 21: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Calculus to the Rescue!

● Answer: Some relatively straight forward calculus will give us a closed form solution for both b and a.● We can now pick the “best” line!

– Are we done? (Hint: NO)

Page 22: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Calculus to the Rescue!

● No! 3 questions remain:

Page 23: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Calculus to the Rescue!

● No! 3 questions remain:

1) Does our data really fit a line?

Page 24: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 25: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 26: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 27: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Calculus to the Rescue!

● No! 3 questions remain:

1) Does our data really fit a line?

2) Did we pick the right error function?

Page 28: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 29: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 30: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

The squared error of this element will have a huge

impact on the line

The squared error of this element will have a huge

impact on the line

Page 31: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Visualizing Squared Error

Square Feet

Price

Page 32: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Visualizing Squared Error

Page 33: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Visualizing Squared Error

Page 34: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Visualizing Squared Error

Squared error puts a lot of emphasis on outliers.

Squared error puts a lot of emphasis on outliers.

Page 35: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Calculus to the Rescue!

● No! 3 questions remain:

1) Does our data really fit a line?

2) Did we pick the right error function?

3) Is square footage the best way to predict housing prices?

Page 36: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 37: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 38: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Buying Houses

Square Feet

Price

Page 39: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Supervised Learning

● Supervised Learning is a form of Machine Learning in which you have labeled training data and you train a model to predict these labels

Alzheimer's?

YES/NO

Page 40: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Other Forms of Learning

● Unsupervised Learning: Given training data without labels tell me something interesting about the data

http://tagc.univ-mrs.fr/tagc/images/dputhier/tb2.jpghttp://upload.wikimedia.org/wikipedia/commons/thumb/1/15/GaussianScatterPCA.png/220px-GaussianScatterPCA.png

Page 41: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Other Forms of Learning

● Reinforcement Learning: Given an environment and an objective function, learn a model to make choices in the environment.

http://www.dacya.ucm.es/jam/images/MountainCar.jpg

Page 42: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Machine Learning

● When posed with a problem in Machine Learning you have to answer 3 questions:

1) How do I represent my data?– Square footage? Number of bathrooms? Quality

of nearby schools?

2) What type of function do I want to learn?– Line? Parabola?– What is our Error function?

3) How do I optimize my model?– Gradient Ascent? Solve directly?– Matrix factorization?

Page 43: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Other Questions

● What I don't have very much training data?● What if you have extra “unlabeled” data?

– e.g. MRI scans of undiagnosed patients● What if you have other types of supervision?

– e.g. I know these two instances belong to the same cluster, I just don't know which cluster they belong to.

● What if you know something about the function you're going to learn?– e.g. Most of the features are not useful.

Page 44: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Introduction to Perceptrons

Page 45: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Handwriting Analysis

● Train a computer to recognize images of handwritten numbers 0 – 9.

● Large training and test set available (MNIST Handwritten Digit Database)

Page 46: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Classification Problem

● The problem we are considering is a classification problem: given an image, we need to figure what class it belongs to (0, 1, 2, 3, etc.).

● Just to be concrete, we're going to write a program with:● Input: Image of a handwritten digit● Output: Prediction for what digit the image

is

Page 47: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Question 1: How do we represent an image?

Page 48: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up
Page 49: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up
Page 50: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

x1

x2

x3

x4

x5

x6

x7

x8

x9

x10

x11

x12

x13

x14

x15

x16

x17

x18

x19

x20

x21

x22

x23

x24

x25

Page 51: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

x1

x2

x3

x4

x5

x6

...

xn

Page 52: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Question 2: What function are we trying to optimize?

Page 53: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

x1

x2

x3

x4

x5

x6

...

xn

+1

+

w1

w2

w1

w3

w4

w5

w6

w6

wn

w0

YES

NO

> 0

≤ 0

Page 54: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

x1

x2

x3

x4

x5

x6

...

xn

+1

+

w1

w2

w1

w3

w4

w5

w6

w6

wn

w0

YES

NO

> 0

≤ 0

f (x1 , x2 , .... , xn)=w0+∑ x iwi

Page 55: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

1? Yes/No

Page 56: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

0? 1? 2? 3? 4? 5? 6? 7? 8? 9?

MAX

0 - 9

Page 57: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Question 3: How do we optimize our perceptrons?

Page 58: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

How do we choose goodvalues for w0 … wn?

Page 59: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

One Approach

● Train the perceptron on valid data.

Page 60: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

One Approach

● Train the perceptron on valid data.● For each data point (image of a digit):

Page 61: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

One Approach

● Train the perceptron on valid data.● For each data point (image of a digit):

● Ask the perceptron what it thinks.

Page 62: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

One Approach

● Train the perceptron on valid data.● For each data point (image of a digit):

● Ask the perceptron what it thinks.● If correct, do nothing.

Page 63: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

One Approach

● Train the perceptron on valid data.● For each data point (image of a digit):

● Ask the perceptron what it thinks.● If correct, do nothing.

● Otherwise, nudge w0 … wn in the right direction.

Page 64: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

One Approach

● Train the perceptron on valid data.● For each data point (image of a digit):

● Ask the perceptron what it thinks.● If correct, do nothing.

● Otherwise, nudge w0 … wn in the right direction.

● Repeat until number of errors is “small enough.”

Page 65: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

One Approach

● Train the perceptron on valid data.● For each data point (image of a digit):

● Ask the perceptron what it thinks.● If correct, do nothing.

● Otherwise, nudge w0 … wn in the right direction.

● Repeat until number of errors is “small enough.”

● Question: What kind of mistakes can we make?

Page 66: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

False Positive

Page 67: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

x1

x2

x3

x4

x5

x6

...

xn

+1

+

w1

w2

w1

w3

w4

w5

w6

w6

wn

w0

YES > 0

False Positive

Page 68: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

False Positivex1

x2

x3

x4

x5

x6

...

xn

+1

+

w2 - α x

2

w1 - α x

1

w3 - α x

3

w4 - α x

4

w5 - α x

5

wn - α x

n

w0 - α x

0

YES > 0

w6 - α x

6

Page 69: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

False Negative

Page 70: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

x1

x2

x3

x4

x5

x6

...

xn

+1

+

w1

w2

w1

w3

w4

w5

w6

w6

wn

w0

NO ≤ 0

False Negative

Page 71: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

False Negativex1

x2

x3

x4

x5

x6

...

xn

+1

+

w2 + α x

2

w1 + α x

1

w3 + α x

3

w4 + α x

4

w5 + α x

5

wn + α x

n

w0 + α x

0

NO ≤ 0

w6 + α x

6

Page 72: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Visualizing w

7

7

7

7

7

7

1

1

1

1

1

1

11

1

1

7

7

7

w

Page 73: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Visualizing w

7

7

7

7

7

7

1

1

1

1

1

1

11

1

1

7

7

7

w

Page 74: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Visualizing w

7

7

7

7

7

7

1

1

1

1

1

11

1

1

7

7

7

w1

Page 75: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Visualizing w

7

7

7

7

7

7

1

1

1

1

1

1

11

1

1

7

7

7

w

Page 76: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Visualizing w

7

7

7

7

7

7

1

1

1

1

1

1

11

1

1

7

7

7

w

Page 77: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Visualizing w

7

7

7

7

7

7

1

1

1

1

1

1

11

1

1

7

7

7

w

Page 78: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Visualizing w

7

7

7

7

7

7

1

1

1

1

1

1

11

1

1

7

7

7

w

Page 79: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

A Nice Math Trick

● For false positives, set wi = wi – α xi.

● For false negatives, set wi = wi + α xi.

● For correct answers, set wi = wi.

● Let “YES” be 1 and “NO” be 0.

● Consider the difference between actual answer and perceptron guess:

● False positive: Actually NO, we say YES. Difference is -1.

● False negative: Actually YES, we say NO. Difference is +1.

● Correct answer: Both YES or both NO. Difference is 0.

● General update rule: wi = wi + α (real - guess) xi.

Page 80: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Perceptron Learning Algorithm

● Start with a random guess of each wi.

● Repeat until perceptron is sufficiently accurate:● Choose a training example (x0, x1, ..., xn).

● Let real be the real answer, guess be the perceptron's guess.

● For each i, set wi := wi + α (real - guess) xi

Page 81: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Visualization of w!

Page 82: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Combining Perceptrons

This is called a neural network.

This is called a neural network.

Page 83: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Perceptrons

● How “good” are perceptrons?

● Not very...they have lots of issues.● Better algorithms exist for classification

● Chose to cover them because they don't require much math.

Page 84: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Machine Learning

● Interesting in machine learning? Take CS109 and CS229.

Page 85: Machine Learning: Introduction to Supervised Learning€¦ · Machine Learning: Introduction to Supervised Learning. Announcements Please fill out course evaluations! Can pick up

Exam Statistics

● Median: 35

● First Quartile: 24

● Third Quartile: 42