Supervised vs Unsupervised Learning: The Math and Code
Supervised learning trains a model using data that already has the correct answers attached, while unsupervised learning trains a model using data with no answers at all, letting it find structure on its own.
Supervised learning trains a model using data that already has the correct answers attached, while unsupervised learning trains a model using data with no answers at all, letting it find structure on its own.
That's the core distinction. Everything else — the math, the code, the examples — is just showing you what that difference actually looks like in practice.
Why It's Used
These are two different tools for two different kinds of problems.
If you have historical data where you know the outcome — like past houses with their actual sale prices, or past emails already marked as "spam" or "not spam" — you use supervised learning. You're teaching the model using an answer key.
But sometimes you don't have an answer key at all. Say you have a huge list of customers and you want to find natural groupings among them — you don't know in advance what those groups should be. There's no "correct" label to learn from. That's where unsupervised learning comes in: the model looks at the data and figures out structure (like clusters or patterns) purely from what it sees.
Intuition
Supervised learning is like studying for a test using flashcards. Each flashcard has a question on the front and the correct answer on the back. You look at the question, guess the answer, flip the card, see if you were right, and adjust. Do this enough times with enough flashcards, and you get good at answering even questions you haven't seen before — as long as they're similar in spirit to the flashcards you studied.
Unsupervised learning is more like being dropped into a room full of unlabeled boxes and asked to organize them however makes sense. Nobody tells you "this box goes in category A." You just start noticing: these boxes are all similar in size, these ones smell like books, these ones rattle when shook. You group them based on similarity alone — no answer key required, because there isn't one.
The key giveaway when you're facing a real problem: do you have labels (correct answers) in your data? If yes, it's supervised. If no, it's unsupervised.
The Math
Let's make this concrete with notation, because the difference actually shows up cleanly in how the problem is written mathematically.
Supervised Learning
You have a dataset of pairs:
Here:
- is the input (also called features) — for example, a house's size, number of bedrooms, and location.
- is the known correct output (the label) — for example, that house's actual sale price.
The goal is to find a function such that:
In words: find a rule that, when given the input, spits out something very close to the known correct answer. Once you have a good , you can feed it a new (a house you haven't seen before) and get a sensible predicted .
How "close" is to is measured by a loss function. A common one for predicting numbers is squared error:
This just says: for every example, find the difference between the real answer and the model's guess, square it (so negative and positive errors don't cancel out), and average it across all examples. Training a supervised model basically means adjusting to make as small as possible.
Unsupervised Learning
Here, you only have inputs — no at all:
There's no "correct answer" to compare against. So instead of minimizing the distance between a guess and a known label, unsupervised learning usually minimizes some measure of structure or similarity within the data itself.
A classic example is clustering, where you try to group points so that points in the same group are close together. One common measure to minimize is total within-cluster distance:
Don't worry, let's unpack every symbol:
- is the number of groups (clusters) you're trying to form.
- is the set of points that belong to cluster .
- is the "center" (average point) of cluster .
- is the squared distance between a point and its cluster's center.
In words: for every point, measure how far it is from the center of the group it's been assigned to, square that distance, and add it all up. The "learning" here is finding cluster centers and assignments that make this total as small as possible — points end up tightly grouped with others like them, with no labels ever involved.
A Tiny Worked Example
Suppose we have 4 simple 1-dimensional points: .
Supervised version: imagine each point also has a known label : — notice every time. A supervised model just needs to discover , and it'll predict perfectly on new points too.
Unsupervised version: we only have , no labels. Just by looking, you can tell and are close together, and and are close together — two natural clusters, centers at and . Nobody told us that grouping; we found it from the data's own structure.
Code
Supervised Learning — From Scratch
Let's fit a simple line to labeled data by hand, using the math above.
import numpy as np
# Labeled data: x = input, y = known correct answer
x = np.array([2, 3, 8, 9])
y = np.array([4, 6, 16, 18]) # y = 2x, the pattern we want the model to discover
# Start with a random guess for the slope (w) and intercept (b)
w, b = 0.0, 0.0
learning_rate = 0.01
# Train for 1000 rounds, nudging w and b closer to the truth each time
for step in range(1000):
y_pred = w * x + b # current guesses
error = y_pred - y # how wrong we are
# Gradient: direction to nudge w and b to reduce error (from calculus on the loss L)
w_grad = (2 / len(x)) * np.sum(error * x)
b_grad = (2 / len(x)) * np.sum(error)
w -= learning_rate * w_grad
b -= learning_rate * b_grad
print(f"Learned: y = {w:.2f} * x + {b:.2f}")
# Should land very close to y = 2x + 0Supervised Learning — Library Version
from sklearn.linear_model import LinearRegression
import numpy as np
x = np.array([2, 3, 8, 9]).reshape(-1, 1) # sklearn wants a 2D array of inputs
y = np.array([4, 6, 16, 18])
model = LinearRegression()
model.fit(x, y) # this is the "studying with flashcards" step
print(model.coef_, model.intercept_) # should print ~2.0 and ~0.0
print(model.predict([[5]])) # predicts on a NEW, unseen valueUnsupervised Learning — From Scratch
Now let's group unlabeled points using the clustering idea above (a simplified K-Means).
import numpy as np
x = np.array([2, 3, 8, 9]) # no labels at all here — just raw values
# Start with two random guesses for cluster centers
centers = np.array([1.0, 10.0])
for step in range(10):
# Assign each point to its nearest center
distances = np.abs(x[:, None] - centers[None, :])
assignments = np.argmin(distances, axis=1)
# Recompute each center as the average of its assigned points
for k in range(2):
if np.any(assignments == k):
centers[k] = x[assignments == k].mean()
print("Final cluster centers:", centers)
print("Point assignments:", assignments)Unsupervised Learning — Library Version
from sklearn.cluster import KMeans
import numpy as np
x = np.array([2, 3, 8, 9]).reshape(-1, 1)
model = KMeans(n_clusters=2, n_init=10, random_state=0)
model.fit(x)
print(model.cluster_centers_) # ~[2.5] and ~[8.5]
print(model.labels_) # which cluster each point landed inExample / Output
Running the supervised code, you'd see something like:
Learned: y = 2.00 * x + 0.00The model rediscovered the exact rule purely from examples — and it can now predict for a brand-new , like .
Running the unsupervised code, you'd see something like:
Final cluster centers: [2.5 8.5]
Point assignments: [0 0 1 1]No one told the model what a "correct" grouping looked like — it found, on its own, that and belong together, and and belong together.
Advantages and Disadvantages
| Advantages | Disadvantages | |
|---|---|---|
| Supervised | Clear performance metric (you can check against known answers); usually more accurate for its specific task | Needs labeled data, which can be expensive or slow to collect |
| Unsupervised | Works when no labels exist at all; can reveal hidden structure you didn't know to look for | Harder to evaluate ("is this grouping actually good?"); results can be less directly useful |
Common Mistakes / Misunderstandings
- Assuming you need labels for everything. Sometimes there simply aren't any, and that's fine — unsupervised learning exists precisely for that case.
- Treating unsupervised results as "ground truth." Clusters found by an algorithm are suggestions based on similarity, not objective facts. Two different runs can even produce slightly different groupings.
- Forgetting there's a middle ground. Some problems are semi-supervised (a little labeled data, lots of unlabeled), which this article doesn't cover but is worth knowing exists.
- Picking the wrong tool for the job. If you have labels and ignore them by using an unsupervised method, you're throwing away useful information for no reason.
Types of Supervised Learning
Supervised learning splits into two broad families based on what kind of answer you're trying to predict.
Regression
Use regression when the answer is a continuous number — something on a sliding scale, like a house price, tomorrow's temperature, or a stock's closing value. The model learns to output any real number, not just one from a fixed list. Common algorithms include Linear Regression, Polynomial Regression, Lasso, and Ridge.
Classification
Use classification when the answer is a category — one choice from a fixed set. Is this email spam or not? Does this X-ray show a tumour or not? Which of these three flowers is it? The model learns to assign examples to one of the possible classes. Common algorithms include Logistic Regression, Support Vector Machines, Decision Trees, Random Forests, and Naïve Bayes.
Where supervised learning shows up
- Image classification: sorting photos into categories (animals, scenes, products) — powers image search and content moderation.
- Medical diagnosis: analysing patient records or scans to flag conditions — the model learned from historical cases with confirmed diagnoses.
- Fraud detection: every card transaction is scored against patterns learned from past confirmed fraud.
- Natural language processing: sentiment analysis, machine translation, and summarisation all rely on models trained on massive labelled text corpora.
Types of Unsupervised Learning
Unsupervised learning also breaks into two main families, depending on what kind of structure you're trying to find.
Clustering
Clustering algorithms group similar data points together — without anyone telling them what the groups should be. They work by moving each point closer to the centre of its assigned group and away from others, iterating until the groupings stabilise. K-Means, DBSCAN, and Hierarchical Clustering are the most widely used.
Association Rule Learning
Association algorithms find co-occurrence patterns — rules like "people who buy X very often also buy Y." No labels needed, just transaction histories. This is the engine behind most recommendation systems. Key algorithms include Apriori, Eclat, and FP-Growth.
Where unsupervised learning shows up
- Anomaly detection: finding unusual behaviour in network traffic, financial transactions, or sensor readings — no labelled examples of fraud needed.
- Customer segmentation: grouping buyers by purchase behaviour so marketing can target each segment differently.
- Recommendation systems: identifying users with similar taste profiles and surfacing products, movies, or music they haven't discovered yet.
- Scientific discovery: uncovering hidden structure in genomics, astronomy, or climate data where no one knows in advance what patterns to look for.
Supervised vs Unsupervised: At a Glance
| Supervised | Unsupervised | |
|---|---|---|
| Has labels? | Yes — each example has a known correct answer | No — only raw inputs, no answer key |
| Goal | Learn a mapping from inputs to known outputs | Find hidden structure, patterns, or groupings |
| Sub-types | Regression (numbers) · Classification (categories) | Clustering · Association Rule Learning |
| Example algorithms | Linear Regression, Logistic Regression, Random Forest, SVM | K-Means, DBSCAN, Apriori, FP-Growth |
| Typical use cases | Price prediction, spam filtering, medical diagnosis, fraud detection | Customer segmentation, anomaly detection, recommendations, scientific discovery |
| How you evaluate it | Clear metric against known answers (MSE, accuracy) | Harder — no ground truth; use silhouette score or visual inspection |
Try This Yourself
Take the unsupervised code above and change the four points from [2, 3, 8, 9] to [2, 3, 4, 20, 21, 22], then set n_clusters=2. Watch where the boundary between groups lands — it'll show you exactly how the algorithm is just minimizing distance, nothing more mysterious than that.
Want to go deeper? You can learn all of this properly, step by step, on our website.
Summary
- Supervised learning uses labeled data (inputs and known correct answers) to learn a predictive rule.
- Unsupervised learning uses only inputs, and finds structure or groupings without any answer key.
- The math reflects this directly: supervised learning minimizes error against known labels; unsupervised learning minimizes internal structure like distance within clusters.
- Both are trained by starting with a rough guess and repeatedly nudging it to do better.
- Choosing between them comes down to one simple question: do you have labels or not?
Common questions
When should I use supervised vs unsupervised learning?
If you have data with known labels or correct answers that you want the model to predict on new data, use supervised learning. If you only have raw data and want to discover hidden groupings or patterns, use unsupervised learning.