AI Lec 5
AI Lec 5
AI Lec 5
Lecture 6
12/10/2013
3
Supervised Learning
Lecture 6
x1 x1(i)
x2(i)
1 2 3
X(15)
4
X(1) X(7) 6 5
X(16)
7
X(3) X(8)
9 10
X(25) X(12) 11
12
X(13) X(6) 13
14 8
X(9)
X(10)
15 16
X(4)
X(11)
X(14) Elliptical blobs (objects)
x2
12/10/2013
5
Nearest Neighbours
x1
X(j)=(x1(j), x2(j), ,xn(j))
n
D(i, j ) xk i xk j
2
k 1
x2
12/10/2013
6
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
12/10/2013
7
K-Nearest Neighbour Model
Given test point X
12/10/2013
8
K-Nearest Neighbour Model
Classification
x
x
12/10/2013
9
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 ?
12/10/2013
10
K-Nearest Neighbour Model
Example : 3-Nearest Neighbors
Customer Age Income No. credit Response
cards
John 35 35K 3 No
Tom 59 170K 1 No 15
12/10/2013
11
K-Nearest Neighbour Model
Example : 3-Nearest Neighbors
Customer Age Income No. credit Response
cards
John 35 35K 3 No
Tom 59 170K 1 No 15
12/10/2013
12
K-Nearest Neighbour Model
Example : 3-Nearest Neighbors
Customer Age Income No. credit Response
cards
John 35 35K 3 No
Tom 59 170K 1 No 15
12/10/2013
13
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
Hannah 63 200K 1 No
Tom 59 170K 1 No
David 37 50K 2 ?
12/10/2013
14
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
k
Return f (xq ) argmax d (xq, f (xi ))
i=1
12/10/2013
15
Nearest Neighbour Rule
12/10/2013 16
k-Nearest Neighbor Examples
(discrete-valued target function)
k=1
k=5
12/10/2013
17
KNN Flavors
12/10/2013
18
Distance-Weighted Nearest Neighbor
Algorithm
Distance-weighted function (For Discrete-valued function)
k
f argmax w d (x , f (x ))
i q i
i=1
where 1
wi
d(xq , xi )2
12/10/2013
19
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
12/10/2013
20
K-Nearest Neighbour Model
Picking K
Use N fold cross validation Pick K to minimize the cross validation error
Use the K that gives lowest average error over the N training examples
12/10/2013
21
Nearest Neighbour Complexity
Expensive for high dimensional data (d>20?)
12/10/2013
22
Advantages/Disadvantages
Advantages:
Training is very fast
Learning complex target functions
Dont lose information
Disadvantages:
Slow at query
Easily fooled by irrelevant attributes
12/10/2013
23
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
12/10/2013
24
Condensing
Aim is to reduce the number of training samples
For example, Retain only the samples that are needed to
define the decision boundary
12/10/2013 27
12/10/2013 28
KNN in Collaborative Filtering (CF)
Item1 Item2 Item3 Item4 Item5 Item6
User1 4 2 1 5
User2 5 5 5 1
User3 4 4 4 1
User4 3 3 5
12/10/2013 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
12/10/2013 30
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
12/10/2013 31
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
12/10/2013 33
How does KNN work here?
Find similar users.
Similarity measures.
Vector
Pearson correlation
12/10/2013
35
12/10/2013
36
Questions?
12/10/2013
37