Lazy Learning
The k-nearest neighbour (k-NN) method is a lazy learner (instance-based learner): it stores the training tuples and does no work until a new tuple must be classified. Each tuple is a point in n-dimensional space.
Algorithm
- Choose k, the number of neighbours.
- For the unknown tuple, calculate the distance to every training tuple, usually the Euclidean distance.
- Select the k nearest tuples.
- Assign the class that occurs most often among them (majority vote). For numeric prediction, take the average of their values.
Distance-weighted k-NN gives closer neighbours a larger vote, for example weight 1 / d².
Practical Points
- Scale the attributes (min-max) so that a large range does not dominate the distance.
- Choose k by cross-validation: a very small k is sensitive to noise, a very large k blurs the classes. An odd k avoids ties in two-class problems.
- Classification is slow for large data because every distance is computed, so indexing structures (k-d trees) and editing (removing useless tuples) are used.
- Categorical attributes use distance 0 for equal values and 1 for different values.
Advantages and Limits
k-NN is simple, needs no training, adapts at once when new tuples are added and can model very irregular class boundaries. Its limits are the large storage and the slow classification, sensitivity to irrelevant attributes and, in very high dimensions, the curse of dimensionality, where all points become almost equally far apart so that "nearest" loses its meaning. Feature selection and dimensionality reduction therefore help k-NN.
Definition: Lazy learner. A classifier that stores the training data and delays all processing until a new tuple has to be classified.
Table 3.5: Eager and lazy learners
| Point | Eager (tree, Bayes) | Lazy (k-NN) |
|---|---|---|
| Model built | Before use | At classification time |
| Training time | Long | None |
| Classification time | Short | Long |
| Storage | Small model | All training data |
Example 3.5: k-NN with k = 3
Problem: The training points are A(1,1) Red, B(2,1) Red, C(4,4) Blue, D(5,4) Blue, E(1,2) Red and F(5,5) Blue. Classify the new point (3, 2.5) with k = 3.
Solution:
- Distances: B 1.803, C 1.803, E 2.062, A 2.5, D 2.5, F 3.2.
- The three nearest are B (Red), C (Blue) and E (Red).
- Votes: Red 2, Blue 1.
Result: The point (3, 2.5) is classified as Red.
from math import dist
train = [((1, 1), "Red"), ((2, 1), "Red"), ((4, 4), "Blue"),
((5, 4), "Blue"), ((1, 2), "Red"), ((5, 5), "Blue")]
q = (3, 2.5)
rank = sorted((dist(p, q), c) for p, c in train)
print("Distances:", [round(d, 3) for d, c in rank])
k = 3
votes = [c for d, c in rank[:k]]
print("Votes:", votes)
print("Class:", max(set(votes), key=votes.count))
Output
Distances: [1.803, 1.803, 2.062, 2.5, 2.5, 3.202]
Votes: ['Blue', 'Red', 'Red']
Class: Red
Common mistakes
- Using unscaled attributes, so that income dominates age.
- Choosing k equal to 1 on noisy data.
- Choosing an even k and getting tied votes.
- Expecting fast classification on a very large training set.
Key takeaways
- k-NN is lazy: no model is built in advance.
- Class is decided by the majority of the k nearest tuples.
- Scale attributes and choose k by cross-validation.
- Classification is slow on large data.
Practice questions
- Why is k-NN called a lazy learner?
- How is the class decided?
- Why must attributes be scaled?
Answers to the practice questions
- It does no learning until a tuple has to be classified.
- By majority vote of the k nearest neighbours.
- To stop attributes with large ranges from dominating the distance.
Sources
- Official BCA syllabus Data Warehousing and Mining, course code 25BCA406DS03, Unit III
