Welcome to our new module on Supervised Classification Algorithms. In our previous module, we focused on unsupervised learning, where the goal was to discover hidden structures in data without a predefined outcome. We used clustering to group similar data points, a process where the concept of "distance" was fundamental for measuring dissimilarity.
Today, we transition to supervised learning, where we have a known outcome or "label" for our data, and our goal is to build a model that can predict this outcome for new, unseen data points. We will begin with an algorithm that you aptly described as "quick and dirty": K-Nearest Neighbors (KNN). As you'll see, KNN elegantly repurposes the concept of distance, not for discovery, but for classification. The learning outcome for this lesson is to classify a new data point using the K-Nearest Neighbors algorithm on a small training set. We'll focus entirely on the hand-calculations that underpin this intuitive method.
The K-Nearest Neighbors (KNN) Algorithm
The core idea behind KNN is simple and powerful: "A data point is likely to be similar to the points closest to it." In classification, this translates to assigning a new data point the same class label as the majority of its neighbors. It's called an "instance-based" or "lazy learning" algorithm because it doesn't build a formal model from the training data. Instead, it memorizes the entire training set and performs all the calculations at the time of prediction.
The process involves five key steps:
- Choose a value for k, the number of neighbors to consider.
- Select a distance metric, typically Euclidean distance.
- For a new, unclassified data point, calculate its distance to every single point in the training dataset.
- Identify the k points from the training set that have the smallest calculated distances to the new point. These are the "nearest neighbors."
- Conduct a majority vote: Look at the class labels of these k neighbors and assign the most common class to the new data point.
The Distance Metric
As in our clustering analysis, the most common way to measure the "closeness" between two points in a multi-dimensional space is with Euclidean distance. For two points, and , in a two-dimensional feature space, the formula is:
This is simply the length of the straight line connecting the two points, derived from the Pythagorean theorem.
The following image shows this exact calculation being performed in a spreadsheet. A new data point (row 21) with Height=161 and Weight=61 is being evaluated. The formula bar shows the Euclidean distance being calculated between this new point and the data point in row 6.
A crucial point to note is that features on different scales can disproportionately influence the distance metric. For example, if one feature ranges from 0-10 and another from 0-1000, the second feature will dominate the distance calculation. In a real-world application, you would first standardize the features (e.g., by converting them to z-scores). For our hand-calculations today, we will use features with comparable scales to keep the focus on the algorithmic steps.
Application 1: Recommending a Bra Style
Imagine your bra-fitting business has data from past clients. You want to use this data to make an initial recommendation for a new client, streamlining the consultation. You've collected data on two key fit characteristics, which you've distilled into a 1-10 index: Cup Index (representing volume and projection) and Band Index (representing torso shape and size). Based on successful fittings, you've classified clients as preferring either a 'Full-Support' or a 'Balconette' style.
The Goal: Predict the preferred style for a new client.
- k: We'll choose k = 3. Using an odd number helps avoid ties in a two-class problem.
- Features:
Band IndexandCup Index. - Classes: 'Full-Support' vs. 'Balconette'.
Training Data:
| Client ID | Band Index | Cup Index | Preferred Style |
|---|---|---|---|
| C1 | 4 | 7 | Balconette |
| C2 | 7 | 8 | Full-Support |
| C3 | 8 | 4 | Full-Support |
| C4 | 3 | 5 | Balconette |
| C5 | 6 | 9 | Full-Support |
New Data Point: A new client arrives whose measurements correspond to a Band Index = 5 and Cup Index = 6.
Step 1: Calculate Distances
We calculate the Euclidean distance from the new client (5, 6) to every client in our training set.
- Distance to C1 (4, 7):
- Distance to C2 (7, 8):
- Distance to C3 (8, 4):
- Distance to C4 (3, 5):
- Distance to C5 (6, 9):
Step 2: Identify Nearest Neighbors (k=3)
Let's rank the clients by distance, from smallest to largest:
- Client C1: Distance ≈ 1.41 (Style: Balconette)
- Client C4: Distance ≈ 2.24 (Style: Balconette)
- Client C2: Distance ≈ 2.83 (Style: Full-Support)
These are the 3-nearest neighbors.
Step 3: Majority Vote
Among these three neighbors, we have:
- 'Balconette': 2 votes (from C1 and C4)
- 'Full-Support': 1 vote (from C2)
The majority class is 'Balconette'.
Conclusion: The KNN algorithm predicts that the new client is most likely to prefer a 'Balconette' style. In a practical setting, this doesn't replace the consultation, but it gives you a data-driven starting point, allowing you to bring a balconette style bra as the first option to try.
Application 2: Predicting Relationship Dynamics
In Sarah's polyandrous relationship, she wants to anticipate the emotional texture of an upcoming week. She has been tracking two metrics: Planned Activities (number of scheduled events involving all three of them) and One-on-One Initiatives (number of times a partner proactively initiated individual quality time). Based on her journaling, she has labeled each past week's overall mood as either 'Harmonious' or 'Distanced'.
The Goal: Predict the likely mood for an upcoming week.
- k: We'll again use k = 3.
- Features:
Planned ActivitiesandOne-on-One Initiatives. - Classes: 'Harmonious' vs. 'Distanced'.
Training Data:
| Week | Planned Activities | One-on-One Initiatives | Mood |
|---|---|---|---|
| W1 | 2 | 5 | Harmonious |
| W2 | 5 | 2 | Distanced |
| W3 | 3 | 4 | Harmonious |
| W4 | 4 | 1 | Distanced |
| W5 | 1 | 3 | Harmonious |
New Data Point: For the upcoming week, they have 3 Planned Activities scheduled and anticipate around 2 One-on-One Initiatives.
Step 1: Calculate Distances
We calculate the distance from the new week (3, 2) to all previous weeks.
- Distance to W1 (2, 5):
- Distance to W2 (5, 2):
- Distance to W3 (3, 4):
- Distance to W4 (4, 1):
- Distance to W5 (1, 3):
Step 2: Identify Nearest Neighbors (k=3)
Ranking the weeks by distance:
- Week W4: Distance ≈ 1.41 (Mood: Distanced)
- Week W2: Distance = 2.00 (Mood: Distanced)
- Week W3: Distance = 2.00 (Mood: Harmonious)
Here we have a tie for the 2nd and 3rd nearest neighbors. Since we need 3 neighbors, we include both W2 and W3.
Step 3: Majority Vote
The neighbors are W4, W2, and W3. Their moods are:
- 'Distanced': 2 votes (from W4 and W2)
- 'Harmonious': 1 vote (from W3)
The majority class is 'Distanced'.
Conclusion: The algorithm predicts the upcoming week's mood is likely to be 'Distanced'. For Sarah, this prediction is a prompt for proactive relationship management. It's not a deterministic forecast, but a signal. Seeing this, she might decide to initiate a conversation about expectations for the week or schedule extra one-on-one time to foster connection and potentially steer the dynamic toward a more harmonious state.
Conclusion
In this lesson, we have demystified the K-Nearest Neighbors algorithm through manual calculation. You've seen how this simple, instance-based method leverages the fundamental concept of distance to classify new data points based on the "company they keep." Its power lies in its simplicity and interpretability—there is no complex model, only the data itself.
Key Takeaways:
- KNN is a supervised classification algorithm that predicts the class of a new point by a majority vote of its k nearest neighbors.
- The core calculation involves a distance metric, most commonly Euclidean distance, computed between the new point and all points in the training set.
- The choice of k is a critical parameter: a small k can be sensitive to noise, while a large k can oversmooth the decision boundary.
- KNN is a "lazy learner" because it does no upfront "training"; all the work happens at the time of prediction.
We have now seen our first classification algorithm. In the next lesson, we will explore a completely different approach: the Naive Bayes Classifier. Unlike KNN, Naive Bayes is a "probabilistic classifier" that builds an explicit model based on the data. It uses Bayes' theorem to calculate the probability of a data point belonging to each class and makes a "naive" but powerful assumption about the independence of features.