n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E...

16
Classication with nearest neighbors SUPERVISED LEARNING IN R: CLASSIFICATION Brett Lantz Instructor

Transcript of n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E...

Page 1: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

Classi�cation withnearest neighbors

S U P E R V I S E D L E A R N I N G I N R : C L A S S I F I C AT I O N

Brett LantzInstructor

Page 2: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

SUPERVISED LEARNING IN R: CLASSIFICATION

Classi�cation tasks for driverless cars

Page 3: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

SUPERVISED LEARNING IN R: CLASSIFICATION

Understanding Nearest Neighbors

Page 4: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

SUPERVISED LEARNING IN R: CLASSIFICATION

Measuring similarity with distance

Page 5: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

SUPERVISED LEARNING IN R: CLASSIFICATION

Applying nearest neighbors in Rlibrary(class)

pred <- knn(training_data, testing_data, training_labels)

Page 6: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

Let's practice!S U P E R V I S E D L E A R N I N G I N R : C L A S S I F I C AT I O N

Page 7: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

What about the 'k' inkNN?

S U P E R V I S E D L E A R N I N G I N R : C L A S S I F I C AT I O N

Brett LantzInstructor

Page 8: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

SUPERVISED LEARNING IN R: CLASSIFICATION

Choosing 'k' neighbors

Page 9: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

SUPERVISED LEARNING IN R: CLASSIFICATION

Bigger 'k' is not always better

Page 10: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

SUPERVISED LEARNING IN R: CLASSIFICATION

Choosing 'k'

Page 11: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

Let's practice!S U P E R V I S E D L E A R N I N G I N R : C L A S S I F I C AT I O N

Page 12: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

Data preparation forkNN

S U P E R V I S E D L E A R N I N G I N R : C L A S S I F I C AT I O N

Brett LantzInstructor

Page 13: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

SUPERVISED LEARNING IN R: CLASSIFICATION

kNN assumes numeric data

Page 14: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

SUPERVISED LEARNING IN R: CLASSIFICATION

kNN bene�ts from normalized data

Page 15: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

SUPERVISED LEARNING IN R: CLASSIFICATION

Normalizing data in R# define a min-max normalize() function normalize <- function(x) { return((x - min(x)) / (max(x) - min(x)))}

# normalized version of r1 summary(normalize(signs$r1))

Min. 1st Qu. Median Mean 3rd Qu. Max. 0.0000 0.1935 0.3528 0.4046 0.6129 1.0000

# un-normalized version of r1summary(signs$r1)

Min. 1st Qu. Median Mean 3rd Qu. Max. 3.0 51.0 90.5 103.3 155.0 251.0

Page 16: n ea rest n ei g h b o rs C l a ssi ca ti o n w i th...Unders ta ndi ng Nea res t Nei g hbors SU P E R V I SE D LE A R NI NG I N R : CLA SSI F I CATI O N M ea s uri ng s i m i l a

Let's practice!S U P E R V I S E D L E A R N I N G I N R : C L A S S I F I C AT I O N