KNN, ML Vocabulary, and K-Means
Lecture 2
Our first models, supervised and unsupervised
EECS 189/289, Fall 2025 @ UC Berkeley
Joseph E. Gonzalez and Narges Norouzi
EECS 189/289, Fall 2025 @ UC Berkeley
Joseph E. Gonzalez and Narges Norouzi
EECS 189/289, Fall 2026 @ UC Berkeley
Joseph E. Gonzalez and Narges Norouzi
Roadmap
7824889
Data Tools Overview
7824889
Look at the Data First
The Data
Labels
(Optional)
Features
7824889
Today's Question
A field researcher hands you two measurements from a penguin: the length of its bill and the length of its flipper.
Can you tell them what species it is?
And before you answer: how would you know whether your answer is any good?
7824889
Demo
Palmer Penguins�Review:
7824889
The Simplest Model
We want to predict a penguin's species from two numbers.
Similar penguins should have similar species.
To label a new penguin, find the most similar penguin we have seen, and copy its label.
7824889
K-Nearest Neighbors (KNN)
7824889
K-Nearest Neighbors, with k = 1
Given a new point, find the nearest point in the training data and copy its label.
from sklearn.neighbors import KNeighborsClassifier
knn = KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)
7824889
Training vs Inference
7824889
Training vs Inference
Training is the process of building the model from data.
Inference is the process of using the model to make a prediction.
7824889
Parametric vs Non-Parametric
7824889
Parametric vs. Non-Parametric Models
Voronoi
Diagram
https://en.wikipedia.org/wiki/Voronoi_diagram
Training Data
7824889
Demo
KNN with k = 1�How accurate is it?
7824889
Our k=1 model just scored 100% accuracy on the penguin data. What is the most likely explanation?
The Slido app must be installed on every computer you’re presenting from
Do not edit�How to change the design
7824889
100% Accuracy
Our dumbest possible model just classified every penguin correctly.
Something is wrong. What?
Accuracy on data the model has already seen tells us nothing about whether it has learned anything.
7824889
Generalization
7824889
Generalization
Generalization in machine learning is the ability of a model to perform well on new, unseen data sampled from the same distribution as its training data.
To evaluate generalization,�we need a method to evaluate�the model’s performance on �new data not used for �training.
Training Data
New, Unseen Data
Probably a better model?
7824889
Train/Validation/Test
7824889
How will we evaluate the model?
We are about to train a model using data.
How will we know if the model has “learned” from our data?
Why is memorizing the data (exam) not good?
The Exam Analogy:
What would happen if we gave everyone access to the exam and solutions (the data) to study (train) for the exam?
7824889
Evaluating generalization using the�Train-Test Split
The train-test split is the standard technique we use to evaluate generalization in machine learning:
Data
Test
Train
Train - Test
Split
You should only use the test dataset once �after developing and training the model.
Why?
If you use the test data to tune the model,�the test data no longer measures generalization.
7824889
Evaluating generalization using the�Train-Test Split
The train-test split is the standard technique we use to evaluate generalization in machine learning:
Data
Test
Train
Train - Test
Split
You should only use the test dataset once �after developing and training the model.
Why?
If you use the test data to tune the model�the test data no longer measures generalization.
But what if I want to peek at the test data�to tune for better generalization?
7824889
The Validation Split
The validation dataset is used to evaluate generalization performance during the model development process.
Data
Test
Train
Train - Test
Split
Train - Val.
Split
Train
Val.
Fit (train) the model using the training data.
Tune the model design using the validation data.
7824889
The Train-Validation-Test Split Analogy
You can think of the train, validation, and test splits as how you might study for an exam.
Test
Train
Val.
Practice question and answer pairs that you use to study for the exam. (Go to discussion!)
Practice exam evaluates if your studying process is working (or if you need to study more).
The exam is how we evaluate if you should pass the class.
7824889
You train a model, then try 30 different settings and pick the one with the best test accuracy. What have you actually measured?
The Slido app must be installed on every computer you’re presenting from
Do not edit�How to change the design
7824889
Demo
Train/test split�and the effect of k
7824889
Overfitting vs Underfitting
7824889
What k Controls
Let the k nearest neighbours vote, instead of just one.
7824889
Choosing k Without Eyeballing It
Fit the model for every k and score it on the validation set.
On our data the best k is 3, and the final test accuracy is 97%.
7824889
Hyperparameters
7824889
Hyperparameters
for hp1 in [0.1, 1, 10]:
for hp2 in [0.1, 1, 10]:
model = MyModel(hp1=hp1, hp2=hp2)
model.fit(X_train, y_train)
error[hp1, hp2] = error(y_val, model.predict(X_val))
hp1_best, hp2_best = arg_min(error)
| 0.1 | 1 | 10 |
0.1 | 0.8 | 0.7 | 0.7 |
1 | 0.4 | 0.2 | 0.3 |
10 | 0.5 | 0.6 | 0.6 |
7824889
Parameters vs Hyperparameters
A parameter is learned from the data during training.
A hyperparameter is chosen by us, before training.
7824889
Feature Engineering
7824889
Example of Feature Engineering: Standardization
Feature engineering is a process of preparing your data and its features for effective modeling.
KNN is built entirely on distance, so it inherits whatever the units happen to be.
Standardization rescales every feature to mean 0 and standard deviation 1, so features contribute on equal terms.
7824889
KNN for Regression
7824889
The Other Kind of Prediction
So far we predicted a category: which species.
That is classification.
What if we want to predict a number, like body mass in grams?
That is regression.
Does our algorithm still work?
7824889
KNN Regression
Find the k nearest neighbours, then average their values instead of taking a majority vote.
We see the same overfitting and underfitting issues.
7824889
Demo
KNN for regression�Predicting body mass from flipper length
7824889
K-Means Clustering
7824889
What If There Were No Labels?
Everything so far was supervised: every penguin arrived with its species attached.
Now suppose the researcher hands you the same measurements with no labels at all, and asks: are there natural groups in here?
This is unsupervised learning, and specifically clustering.
7824889
K-Means Clustering
7824889
K-Means Cluster (Lloyd’s) Algorithm
Why is this the mean of each cluster?
7824889
Updating the Cluster Centers
Sum over clusters
Sum over points in cluster k
7824889
Convergence of K-Means
Lloyd's algorithm always converges, but only to a local optimum.
7824889
Demo
K-means on the penguins�Animated Lloyd's algorithm
7824889
Choosing the Number of Clusters
The “Elbow”
7824889
Clusters Are Not Labels
K-means rediscovered the species without ever seeing a label.
But it found penguins that sit close together in bill and flipper measurements. In this dataset those groups happen to line up with species.
Change the features and the clusters change.
7824889
Interpreting the Clusters
We used k-means to compute a cluster assignment for each penguin.
7824889
K-means found three clusters that line up closely with the three penguin species. What does this tell us?
The Slido app must be installed on every computer you’re presenting from
Do not edit�How to change the design
7824889
KNN, ML Vocabulary, and K-Means
Lecture 2
Credit: Joseph E. Gonzalez and Narges Norouzi
Reference Book Chapters: Chapter 1 (up to the end of 1.2), Chapter 15.1 (K-Means)