cs349 nearest neighbor - nucs-349-fall21.github.io

24
(with ideas and a few images from Andrew Moore Check out his site at http://www.autonlab.org/) Machine Learning Topic 3: Instance-based learning (a.k.a. Nearest Neighbor learning) 1

Transcript of cs349 nearest neighbor - nucs-349-fall21.github.io

Page 1: cs349 nearest neighbor - nucs-349-fall21.github.io

(with ideas and a few images from Andrew MooreCheck out his site at http://www.autonlab.org/)

Machine Learning

Topic 3: Instance-based learning(a.k.a. Nearest Neighbor learning)

1

Page 2: cs349 nearest neighbor - nucs-349-fall21.github.io

Classification vs. Regression

• Classification:Learning a function to map from a n-tuple to a

discrete value from a finite set

• Regression:Learning a function to map from a n-tuple to a

continuous value

2Bryan Pardo, Machine Learning: EECS 349 Fall 2012

Page 3: cs349 nearest neighbor - nucs-349-fall21.github.io

Nearest Neighbor Classifier

• Example of memory-based (a.k.a instance-based, a.k.a case-based) learning

• The basic idea:1. Get some example set of cases with known outputs

e.g diagnoses of infectious diseases by experts2. When you see a new case, assign its output to be

the same as the most similar known case.Your symptoms most resemble Mr X.Mr X had the flu. Ergo you have the flu.

Bryan Pardo, Machine Learning: EECS 349 Fall 2012 3

Page 4: cs349 nearest neighbor - nucs-349-fall21.github.io

There is a set of possible examples

Each example is an k-tuple of attribute values

There is a target function that maps X onto some finite set Y

The DATA is a set of tuples <example, target function values>

Find a hypothesis h such that...

General Learning Task

},...{ 1 nxxX !!=

>=< kaax ,...,11!

YXf ®:

})(,,...)(,{ 11 ><><= mm xfxxfxD !!!!

)()(, xfxhx !!!»"

Bryan Pardo, Machine Learning: EECS 349 Fall 2012

Page 5: cs349 nearest neighbor - nucs-349-fall21.github.io

Eager vs. Lazy

Eager learning• Learn model ahead of

time from training data

• Explicitly learn h from training data

• E.g. decision tree, linear regression, svm, neural nets, etc.

Lazy learning• Delay the learning

process until a queryexample must be labeled

• h is implicity learned from training data

• E.g. Nearest neighbor, kNN, locally weightedregression, etc.

Bryan Pardo, Machine Learning: EECS 349 Fall 2012 5

Page 6: cs349 nearest neighbor - nucs-349-fall21.github.io

Single Nearest Neighbor

Given some set of training data…

…and query point , predict

1. Find the nearest member of the data set to the query

2. Assign the nearest neighbor’s output to the query

6

})(,,...)(,{ 11 ><><= mm xfxxfxD !!!!

qx! )( qxf

!

)),((minarg qDx

nn xxdx !!!

=

)()( nnq xfxh !!=

distance function

Our hypothesisBryan Pardo, Machine Learning: EECS 349 Fall 2012

Page 7: cs349 nearest neighbor - nucs-349-fall21.github.io

A Univariate Example

7

• Find closest point.

• Give query its value

)(xf

x

Dxqnn xxdx

Î

= )),(min(arg !!!

)()( nnq xfxf !!=

Bryan Pardo, Machine Learning: EECS 349 Fall 2012

1

2

3

Page 8: cs349 nearest neighbor - nucs-349-fall21.github.io

A Univariate Example

8

• Find closest point.

• Give query its value

)(xf

x

Dxqnn xxdx

Î

= )),(min(arg !!!

)()( nnq xfxf !!=

Bryan Pardo, Machine Learning: EECS 349 Fall 2012

1

2

3

Page 9: cs349 nearest neighbor - nucs-349-fall21.github.io

A Two-dimensional Example

• Voronoi diagram

Bryan Pardo, Machine Learning: EECS 349 Fall 2012 9

Page 10: cs349 nearest neighbor - nucs-349-fall21.github.io

What makes a memory based learner?

• A distance measureNearest neighbor: typically Euclidean

• Number of neighbors to considerNearest neighbor: One

• A weighting function (optional)Nearest neighbor: unused (equal weights)

• How to fit with the neighborsNearest neighbor: Same output as nearest

neighbor

10Bryan Pardo, Machine Learning: EECS 349 Fall 2012

Page 11: cs349 nearest neighbor - nucs-349-fall21.github.io

K-nearest neighbor

• A distance measureEuclidean

• Number of neighbors to considerK

• A weighting function (optional)Unused (i.e. equal weights)

• How to fit with the neighborsregression: average output among K nearest

neighbors.classification: most popular output among K

nearest neighbors11Bryan Pardo, Machine Learning: EECS 349 Fall 2012

Page 12: cs349 nearest neighbor - nucs-349-fall21.github.io

Choosing K

• Making K too small fits the output to the noise in the dataset (overfitting)

• Too large of K can make decision boundaries in classification indistinct (underfitting)

• Choose K empirically using cross-validation

12Bryan Pardo, Machine Learning: EECS 349 Fall 2012

Page 13: cs349 nearest neighbor - nucs-349-fall21.github.io

Ex. of KNN for classification

http://vision.stanford.edu/teaching/cs231n-demos/knn/

13Bryan Pardo, Machine Learning: EECS 349 Fall 2012

Page 14: cs349 nearest neighbor - nucs-349-fall21.github.io

Ex. Of kNN regression where K=9

14

Reasonable jobDid smooth noise

Screws up on the ends OK, but problem on the ends again.

Bryan Pardo, Machine Learning: EECS 349 Fall 2012

Page 15: cs349 nearest neighbor - nucs-349-fall21.github.io

Kernel Regression

• A distance measure: Scaled Euclidean

• Number of neighbors to consider: All of them

• A weighting function:

• How to fit with the neighbors:

15

÷÷ø

öççè

æ -= 2

2),(exp

W

qii K

xxdw

Nearby points to the query are weighted strongly, far points weakly. The Kw parameter is the Kernel Width.

åå ×

=

ii

iii

q w

xfwxh

)()( A weighted average

Bryan Pardo, Machine Learning: EECS 349 Fall 2012

Page 16: cs349 nearest neighbor - nucs-349-fall21.github.io

Kernel Regression

16

Kernel Weight = 1/32of X-axis width

Definitely better than KNN! Catch: Had to play with kernel width to get This result

Nice and smooth, butare the bumpsjustified, or is thisoverfitting?

Kernel Weight = 1/32of X-axis width

Kernel Weight = 1/16of X-axis width

A better fit than KNN?

Bryan Pardo, Machine Learning: EECS 349 Fall 2012

Page 17: cs349 nearest neighbor - nucs-349-fall21.github.io

Weighting dimensions

• Suppose data points are two-dimensional• Different dimensional weightings affect region shapes

17

222

211 )()(),( yxyxyxd -+-= 2

222

11 )33()(),( yxyxyxd -+-=

Bryan Pardo, Machine Learning: EECS 349 Fall 2012

Page 18: cs349 nearest neighbor - nucs-349-fall21.github.io

kNN and Kernel Regression

Pros• Robust to noise• Very effective when training data is sufficient• Customized to each query• Easily adapts when new training data is addedCons• How to weight different dimensions?• Irrelevant dimensions• Computationally expensive to label a new query• High space requirements (must retain each training

example)

Bryan Pardo, Machine Learning: EECS 349 Fall 2012 18

Page 19: cs349 nearest neighbor - nucs-349-fall21.github.io

Locally Weighted (Linear) Regression

• Linear regression: global, linear

• kNN: local, constant• LWR: local, linear

Bryan Pardo, Machine Learning: EECS 349 Fall 2012 19

Err = 12f (x)− h(x)( )2

x∈D∑

xnn = argminx∈D(d(x, xq ))

Err xq( ) = 12f (x)− h(x)( )2

x∈D∑ exp

−d(x, xq )2

KW2

$

%&&

'

())

( ) bxaxh T!!!!

+=

Page 20: cs349 nearest neighbor - nucs-349-fall21.github.io

Locally Weighted (Linear) Regression

Bryan Pardo, Machine Learning: EECS 349 Fall 2012 20

Page 21: cs349 nearest neighbor - nucs-349-fall21.github.io

Locally Weighted Polynomial Regression• Use a polynomial instead of a linear

function to fit the data locally– Quadratic, cubic, etc.

Bryan Pardo, Machine Learning: EECS 349 Fall 2012 21

Page 22: cs349 nearest neighbor - nucs-349-fall21.github.io

Memory-based Learning

Pros• Easily adapts to new training examples (no-retraining

required)• Can handle complex decision boundaries and functions

by considering the query instance when deciding how to generalize

Cons• Requires retaining all training examples in memory• Slow to evaluate a new query• Evaluation time grows with the dataset size

Bryan Pardo, Machine Learning: EECS 349 Fall 2012 22

Page 23: cs349 nearest neighbor - nucs-349-fall21.github.io

Summary

• Memory-based learning are “lazy”– Delay learning until receiving a query

• Local– Training data that is localized around the

query contribute more to the prediction value• Non-parametric• Robust to noise• Curse of dimensionality

– Irrelevant dimensions– How to scale dimensions

Bryan Pardo, Machine Learning: EECS 349 Fall 2012 23

Page 24: cs349 nearest neighbor - nucs-349-fall21.github.io

Summary

• Nearest neighbor – Output the nearest neighbor’s label

• kNN– Output the average of the k NN’s labels

• Kernel regression– Output weighted average of all training data’s

(or k NN’s) labels• Locally weighted (linear) regression

– Fit a linear function locally

Bryan Pardo, Machine Learning: EECS 349 Fall 2012 24