Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
k-Nearest Neighbors
Lecture 2
September 16, 2015 k-Nearest Neighbors 1
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 |
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 1
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 2
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
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 ¡ ? ¡
September 16, 2015 k-Nearest Neighbors 3
0 ¡ 50 ¡ 100 ¡ 150 ¡ 0 ¡ 50 ¡ 100 ¡ 150 ¡ # ¡of ¡Kisses ¡ # ¡of ¡Kicks ¡ Romance ¡ Ac>on ¡ Unknown ¡
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
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 ¡
September 16, 2015 k-Nearest Neighbors 4
0 ¡ 50 ¡ 100 ¡ 150 ¡ 0 ¡ 50 ¡ 100 ¡ 150 ¡ # ¡of ¡Kisses ¡ # ¡of ¡Kicks ¡ Romance ¡ Ac>on ¡ Unknown ¡
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
– Via distance function
September 16, 2015 k-Nearest Neighbors 5
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 6
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 7
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 8
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 9
Model ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡ ¡as ¡
y = f(x)
ˆ f(x)
Err(x) = Bias2 + Variance + Irreducible Error
Err(x) = E[(Y − ˆ f(x))2] Bias = E[ ˆ f(x)] − f(x) Variance = E[( ˆ f(x) − E[ ˆ f(x)])2]
Irreducible Error = σ2
Bias = f(x) − 1 k
k
X
i=1
f(Ni(x))
Variance = σ2 k
Monotonically ¡increases ¡with ¡k ¡ Monotonically ¡decreases ¡with ¡k ¡ Example: ¡hUp://scoU.fortmann-‑roe.com/docs/BiasVariance.html ¡
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 10
cos(θ) = A · B ||A|| ||B||
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 11
Xscaled = X − Min Max − Min
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 12
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 13
Useful ¡if ¡the ¡nearest ¡neighbors ¡vary ¡widely ¡in ¡their ¡distance ¡and ¡the ¡closer ¡neighbors ¡ more ¡reliably ¡indicate ¡the ¡class ¡of ¡the ¡object ¡
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 14
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 15
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 16
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
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 ¡ ? ¡
September 16, 2015 k-Nearest Neighbors 17
hUp://www.saedsayad.com/k_nearest_neighbors_reg.htm ¡
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 18
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 19
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 20
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 21
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 22
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 23
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 24
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 25
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 26
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 27
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 28
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 29
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 30
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
– cand = children( v ) – if |children( v )| < k
– vmin = argminw < cand d( w, y ) – if vmin = v: break; – v = vmin
Result
– Connect( n, y )
ShouldAdd
September 16, 2015 k-Nearest Neighbors 31
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
– result[ i ] = ti.Test( y )
– ti.Train( y )
– ti.Train( Rand( r, i ) )
September 16, 2015 k-Nearest Neighbors 32
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
September 16, 2015 k-Nearest Neighbors 33
Wentworth Institute of Technology COMP4050 – Machine Learning | Fall 2015 | Derbinsky
– Easy, generally applicable – Need know nothing about the underlying process
– Training: lazy – Testing: only for small datasets
– Depends upon data/parameters (e.g. D, V, k, …) – Bounded above by twice the Bayes error under certain reasonable assumptions; the error of the general kNN method asymptotically approaches that of the Bayes error and can be used to approximate it
September 16, 2015 k-Nearest Neighbors 34