k nearest neighbors
play

k-Nearest Neighbors Lecture 2 k-Nearest Neighbors September 16, - PowerPoint PPT Presentation

Wentworth Institute of Technology COMP4050 Machine Learning | Fall 2015 | Derbinsky k-Nearest Neighbors Lecture 2 k-Nearest Neighbors September 16, 2015 1 Wentworth Institute of Technology COMP4050 Machine Learning | Fall 2015 |


  1. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky k-Nearest Neighbors Lecture 2 k-Nearest Neighbors September 16, 2015 1

  2. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Outline 1. Learning via distance measurements 2. Model parameters – Bias vs. Variance 3. Extensions – Regression – Improving Efficiency k-Nearest Neighbors September 16, 2015 2

  3. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky A Motivating Example Movie ¡Title ¡ # ¡of ¡Kicks ¡ # ¡of ¡Kisses ¡ Type ¡of ¡Movie ¡ California ¡Man ¡ 3 ¡ 104 ¡ Romance ¡ He’s ¡Not ¡Really ¡into ¡Dudes ¡ 2 ¡ 100 ¡ Romance ¡ Beau>ful ¡Woman ¡ 1 ¡ 81 ¡ Romance ¡ Kevin ¡Longblade ¡ 101 ¡ 10 ¡ Ac>on ¡ Robo ¡Slayer ¡3000 ¡ 99 ¡ 5 ¡ Ac>on ¡ Amped ¡II ¡ 98 ¡ 2 ¡ Ac>on ¡ ? ¡ 18 ¡ 90 ¡ ? ¡ 150 ¡ # ¡of ¡Kisses ¡ 100 ¡ Romance ¡ 50 ¡ Ac>on ¡ Unknown ¡ 0 ¡ 0 ¡ 50 ¡ 100 ¡ 150 ¡ # ¡of ¡Kicks ¡ k-Nearest Neighbors September 16, 2015 3

  4. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky A Motivating Example Movie ¡Title ¡ # ¡of ¡Kicks ¡ # ¡of ¡Kisses ¡ Type ¡of ¡Movie ¡ L2 ¡Distance ¡ California ¡Man ¡ 3 ¡ 104 ¡ Romance ¡ 20.52 ¡ He’s ¡Not ¡Really ¡into ¡Dudes ¡ 2 ¡ 100 ¡ Romance ¡ 18.87 ¡ Beau>ful ¡Woman ¡ 1 ¡ 81 ¡ Romance ¡ 19.24 ¡ Kevin ¡Longblade ¡ 101 ¡ 10 ¡ Ac>on ¡ 115.28 ¡ Robo ¡Slayer ¡3000 ¡ 99 ¡ 5 ¡ Ac>on ¡ 117.41 ¡ Amped ¡II ¡ 98 ¡ 2 ¡ Ac>on ¡ 118.93 ¡ ? ¡ 18 ¡ 90 ¡ ? ¡ 0 ¡ 150 ¡ # ¡of ¡Kisses ¡ 100 ¡ Romance ¡ 50 ¡ Ac>on ¡ Unknown ¡ 0 ¡ 0 ¡ 50 ¡ 100 ¡ 150 ¡ # ¡of ¡Kicks ¡ k-Nearest Neighbors September 16, 2015 4

  5. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky kNN Training Testing • Store examples • Find the nearest k neighbors to target – Via distance function • Vote on result k-Nearest Neighbors September 16, 2015 5

  6. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky 2D Multiclass Classification Ground Truth 1-NN via Linear Scan k-Nearest Neighbors September 16, 2015 6

  7. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Model Parameters • k – number of neighbors to find • D( x 1 , x 2 ) – distance function • V({ x , y}) – voting function Related • Feature representation – Scaling – Curse of dimensionality • Efficiency – Storage/search k-Nearest Neighbors September 16, 2015 7

  8. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Choosing k • 1 = Nearest Neighbor • Pro tip: if binary, choose odd to avoid ties • Tradeoff: under/over-fitting – Small k: sensitive to noise – Large k: includes distal points k-Nearest Neighbors September 16, 2015 8

  9. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Bias vs. Variance Revisited General kNN Model ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡as ¡ ˆ y = f ( x ) f ( x ) k Bias = f ( x ) − 1 X f ( N i ( x )) k Err( x ) = Bias 2 + Variance + Irreducible Error i =1 Monotonically ¡increases ¡with ¡ k ¡ Err( x ) = E [( Y − ˆ f ( x )) 2 ] Bias = E [ ˆ Variance = σ 2 f ( x )] − f ( x ) Variance = E [( ˆ f ( x ) − E [ ˆ f ( x )]) 2 ] k Monotonically ¡decreases ¡with ¡ k ¡ Irreducible Error = σ 2 Example: ¡hUp://scoU.fortmann-­‑roe.com/docs/BiasVariance.html ¡ k-Nearest Neighbors September 16, 2015 9

  10. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Common Distance Functions • Manhattan (L1) • Euclidean (L2) • Cosine similarity A · B – Useful in high dimensions: cos ( θ ) = || A || || B || • Edit distance • Graph traversal – Decay • Modern: learn a useful distance measure! Individual instance weighting k-Nearest Neighbors September 16, 2015 10

  11. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Issues with Distance Functions • Categorical data – Indicator function is safe (i.e. Hamming Distance) • Pay attention to nominal features! • Curses! – Euclidean becomes less discriminating in high dimensions • Normalization – Consider a function over features • Annual salary • Height in meters X − Min X scaled = – Common to scale features to [0, 1] Max − Min k-Nearest Neighbors September 16, 2015 11

  12. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky V = Majority Vote y 0 = argmax X I ( v = y i ) v ( x i ,y i ) 2 D z k-Nearest Neighbors September 16, 2015 12

  13. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky V = Distance-Weighted Vote y 0 = argmax X w i × I ( v = y i ) v ( x i ,y i ) 2 D z 1 where w i = d ( x 0 , x i ) 2 Useful ¡if ¡the ¡nearest ¡neighbors ¡vary ¡widely ¡in ¡their ¡distance ¡and ¡the ¡closer ¡neighbors ¡ more ¡reliably ¡indicate ¡the ¡class ¡of ¡the ¡object ¡ k-Nearest Neighbors September 16, 2015 13

  14. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Efficiency Assume N training examples, d features… • What is the computational cost of training a new instance? O ( d ) ∼ O (1) • How much space is required to store the model? O ( N · d ) • What is the computational cost of predicting the result of a new test instance? O ( N · d ) k-Nearest Neighbors September 16, 2015 14

  15. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Some Theory (Cover & Hart, 1967) • Bayes error rate is the lowest possible error rate for a given class of classifier – Non-zero if the distributions of the instances overlap – More in later lectures • As the amount of data approaches infinity, kNN is guaranteed to yield an error rate no worse than twice the Bayes error rate • kNN is guaranteed to approach the Bayes error rate for some value of k (where k increases as a function of the number of data points) k-Nearest Neighbors September 16, 2015 15

  16. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Applying kNN to Regression • Rather than voting on a label, the voting function produces a value – Average – Weighted average (w.r.t. distance) k-Nearest Neighbors September 16, 2015 16

  17. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Example: House Price Index Age ¡ Loan ¡ House ¡Price ¡Index ¡ 25 ¡ $40,000 ¡ 135 ¡ 35 ¡ $60,000 ¡ 256 ¡ 45 ¡ $80,000 ¡ 231 ¡ 20 ¡ $20,000 ¡ 267 ¡ 35 ¡ $120,000 ¡ 139 ¡ 52 ¡ $18,000 ¡ 150 ¡ 23 ¡ $95,000 ¡ 127 ¡ 40 ¡ $62,000 ¡ 216 ¡ 60 ¡ $100,000 ¡ 139 ¡ 48 ¡ $220,000 ¡ 250 ¡ 33 ¡ $150,000 ¡ 264 ¡ 48 ¡ $142,000 ¡ ? ¡ hUp://www.saedsayad.com/k_nearest_neighbors_reg.htm ¡ k-Nearest Neighbors September 16, 2015 17

  18. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Improving Efficiency • Filtered Storage – Condensed NN • Intelligent Search – Space partitioning (k-d tree, R-tree) • Approximate NN – Locality Sensitive Hashing – Boundary Forests k-Nearest Neighbors September 16, 2015 18

  19. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky A 2D Classification Example k-Nearest Neighbors September 16, 2015 19

  20. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Interleaved Train/Query (1) Ground Truth Boundary Tree k-Nearest Neighbors September 16, 2015 20

  21. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Interleaved Train/Query (2) Ground Truth Boundary Tree k-Nearest Neighbors September 16, 2015 21

  22. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Interleaved Train/Query (3) Ground Truth Boundary Tree k-Nearest Neighbors September 16, 2015 22

  23. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Interleaved Train/Query (4) Ground Truth Boundary Tree k-Nearest Neighbors September 16, 2015 23

  24. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Interleaved Train/Query (5) Ground Truth Boundary Tree k-Nearest Neighbors September 16, 2015 24

  25. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Interleaved Train/Query (6) Ground Truth Boundary Tree k-Nearest Neighbors September 16, 2015 25

  26. Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky Interleaved Train/Query (7) Ground Truth Boundary Tree k-Nearest Neighbors September 16, 2015 26

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend