Professional Documents
Culture Documents
Lecture 6
10/11/13
1
Supervised Learning
Lecture 6
10/11/13
3
Nearest Neighbours
x1
X(j)=(x1(j), x2(j), ,xn(j))
n
D(i, j ) = ( xk ( i ) xk ( j ) )
2
k =1
x2
10/11/13
4
Nearest Neighbour
Algorithm
Given training data (X(1),D(1)), (X(2),D(2)), ,
(X(N),D(N))
Euclidean Distance
n
D(i, j ) = ( xk ( i ) xk ( j ) )
2
k =1
10/11/13
5
K-Nearest Neighbour Model
Given test point X
10/11/13
6
K-Nearest Neighbour Model
Classification
x
x
10/11/13
7
K-Nearest Neighbour Model
Example : Classify whether a customer will respond to a
survey question using a 3-Nearest Neighbor classifier
Hannah 63 200K 1 No
Tom 59 170K 1 No
David 37 50K 2 ?
10/11/13
8
K-Nearest Neighbour Model
Example : 3-Nearest Neighbors
15.16
15
152.23
122
15.74
10/11/13
9
K-Nearest Neighbour Model
Example : 3-Nearest Neighbors
15.16
15
152.23
122
15.74
10/11/13
10
K-Nearest Neighbour Model
Example : 3-Nearest Neighbors
15.16
15
152.23
122
15.74
Yes
10/11/13
11
K-Nearest Neighbour Model
Example: For the example we saw earlier, pick the best K
from the set {1, 2, 3} to build a K-NN classifier
10/11/13
12
k-Nearest Neighbor Pseudo
code
Training Algorithm:
Store all training examples <x, f(x)>
Find best value for K
Classification (Testing) Algorithm:
Given a query instance xq to be classified,
Let x1, xk denote the k instances from the list of
training examples
Return k
10/11/13
13
Nearest Neighbour Rule
10/11/13 14
k-Nearest Neighbor
Examples
(discrete-valued target function)
10/11/13
15
KNN Flavors
10/11/13
16
Distance-Weighted Nearest
Neighbor Algorithm
Distance-weighted function (For Discrete-valued function)
k
where
i=1
1
i
d(xq, xi )2
10/11/13
17
How many neighbors, K?
K
Fixed constant
Determines number of elements to be
included in each neighborhood.
Neighborhood determines classification
Different k values can and will produce
different classifications
10/11/13
18
K-Nearest Neighbour Model
Picking K
Find its K nearest neighbours
Make a classification based on these K neighbours
Calculate classification error
Output average error over all examples
10/11/13
19
Nearest Neighbour
Complexity
Expensive for high dimensional data
(d>20?)
O(Nd) complexity for both storage
and query time
N is the number of training examples,
d is the dimension of each sample
10/11/13
20
Advantages/Disadvantages
Advantages:
Training is very fast
Learning complex target functions
Dont lose information
Disadvantages:
Slow at query
Easily fooled by irrelevant attributes
10/11/13
21
Nearest Neighbour Issues
Expensive
To determine the nearest neighbour of a query point q,
must compute the distance to all N training examples
Pre-sort training examples into fast data structures (kd-trees)
Remove redundant data (condensing)
Storage Requirements
Must store all training data D_tr
Remove redundant data (condensing)
Pre-sorting often increases the storage requirements
High Dimensional Data
Curse of Dimensionality
Required amount of training data increases exponentially
with dimension
Computational cost also increases dramatically
10/11/13
22
Condensing
10/11/13 25
10/11/13 26
KNN in Collaborative Filtering (CF)
User1 4 2 1 5
User2 5 5 5 1
User3 4 4 4 1
User4 3 3 5
10/11/13 27
KNN in CF
Item1 Item2 Item3 Item4 Item5 Item6
User1 4 1 5
x
User2 5 5 5 1
User3 4 4 4 1
User4 3 3 5
10/11/13 28
KNN in CF
Item1 Item2 Item3 Item4 Item5 Item6
User1 4 1 5
x
User2 5 5 5 1
User3 4 4 4 1
User4 3 3 5
10/11/13 29
KNN in CF
Item1 Item2 Item3 Item4 Item5 Item6
User1 4 1 5
x
User2 5 5 5 1
User3 4 4 4 1
User4 3 3 5
User1 4 1 5
x
User2 5 5 5 1
User3 4 4 4 1
User4 3 3 5
Pearson correlation
10/11/13
32
Pearson Correlation
10/11/13
33
10/11/13
34
Questions?
10/11/13
35