Introduction: Labeled Data for Learning
Imagine you need to teach a robot to recognize fruits. You show it 1,000 photos: "This one is an apple." "This one is an orange." "This one is a banana." After seeing hundreds of apples, the robot starts to see patterns: round shape, red or green color, particular texture. Then you show it an apple it's never seen before. It recognizes: "That's an apple!"
This is supervised learning. Each training example says "here's the input, here's the expected answer." The model learns to predict the answer for new inputs.
Because there's a "supervisor" (you or the labeled data). You tell the model: "Look, here's the right answer." The model learns. It's like having a teacher who corrects you immediately.

The Fundamental Concept: Labeled Data
Each example for supervised learning contains two parts:
Features: the input variables that describe the problem. To predict the price of an apartment: area in sq ft, number of rooms, floor, neighborhood, year built.
Label: the answer you're trying to predict. For the apartment: its actual sale price ($350,000, $500,000, etc.).
A training dataset looks like a table:
| Area (sq ft) | Rooms | Floor | Neighborhood | Price (USD) |
|---|---|---|---|---|
| 485 | 2 | 3 | Manhattan | 380,000 |
| 880 | 4 | 1 | Brooklyn | 420,000 |
| 270 | 1 | 5 | Queens | 210,000 |
The model sees these patterns and learns: "Large area + central = high price. Small area + outer borough = low price."
If you mislabel your data ("This spam is a legitimate email!"), the model learns garbage. We say "garbage in, garbage out": bad data in = bad model out.
Classification: Predicting a Category
Classification is answering a yes/no question: "Cat or not cat?", "Spam or not spam?", "Sick or not sick?" The answer is a category from a finite set.
Binary Classification: Two Choices
Only two possible categories. Examples:
- Email: spam or not spam
- Bank transaction: fraudulent or legitimate
- Medical imaging: malignant or benign tumor
- Customer review: positive or negative
The model typically produces a score between 0 and 1, interpreted as "probability of being spam." If the score exceeds 0.5, we say "spam." Otherwise, "not spam."

Multiclass Classification: More Than Two Choices
When you have 3, 4, or 10 possible categories.
- Handwritten digit recognition (the MNIST dataset, 70,000 hand-written digits): classes 0 through 9, so 10 categories
- Face recognition: person A, person B, person C...
- Document classification: sports, politics, economics, culture, science

Evaluating a Classifier: The Real Metrics
The most intuitive metric is accuracy: it's simply the percentage of correct answers. But it's often misleading. Imagine you need to detect cancer. If you say "no cancer" for every patient and 99% are indeed healthy, you get 99% accuracy, but you miss every sick patient, making the model completely useless.
That's why we use two complementary metrics instead.
Precision measures the reliability of your positive predictions: among all the cases you predict as positive, how many actually are? For example, if you label 100 messages as "spam" and 80 of them are actually spam, your precision is 80%.
Recall, on the other hand, measures your ability to detect all positive cases: among all the true positives in the data, how many do you manage to find? For example, if there are 1,000 actual spams in your inbox and you detect 800 of them, your recall is 80%.
Precision and recall are often in opposition. To increase precision (fewer false alarms), you say "spam" less often... but you miss more actual spams (recall drops). The context determines the right balance: in fraud detection, you want high recall (don't miss a fraud). In movie recommendations, you want high precision (don't recommend a bad movie).

Regression: Predicting a Numerical Value
Regression is predicting a real number, not a category. Examples:
- House price: $450,000 (not "expensive" or "cheap," but an exact number)
- Tomorrow's temperature: 59.5 degrees Fahrenheit
- Revenue: $5.2 million
- A person's age from a photo: 32 years old

Measuring Regression Quality
You can't use accuracy (these aren't categories). You use:
Mean Squared Error (MSE): the average of the squared gaps.
Symbol by symbol: is the number of examples; numbers them, 1 through ; is the true value of example ; ("y-hat") is what the model predicted for that same example; the big means "add this up across all examples"; and turns that sum into an average. In one sentence: for each example, take the gap between prediction and truth, square it, and average all those squares.
Squaring penalizes large errors heavily. If you predict $500,000 instead of $400,000 (an error of $100,000), that counts as (100,000)^2 = 10 billion in the error. The model doesn't forgive big mistakes.
Root Mean Squared Error (RMSE): the square root of MSE. It brings the result back into the original unit (dollars, not squared dollars) which makes it directly readable.
Mean Absolute Error (MAE): the average of the gaps taken without their sign and without squaring. Far more forgiving of extreme outlying values.
R-squared: measures the proportion of variance in the data that your model explains. An R-squared of 1 means the model perfectly predicts every value. An R-squared of 0 means the model is no better than just predicting the mean. In practice, an R-squared of 0.85 is often excellent for real-world noisy data.

The Algorithms: Which Tool for Which Problem
Linear Regression: The Basic That Works
The simplest one. Looks for a line (or a plane in higher dimensions) that minimizes the error.
With one input variable: . Here is the observed feature (the area), the value to predict (the price), the slope ("how much the price rises when area rises by one unit") and the intercept ("the starting price, at zero area"). and are the learned parameters: training searches for them, and once found they are frozen. and change with every apartment.
Example: . Each square foot adds $5,000 to a base price of $50,000.
Strengths: simple, fast, interpretable. "Each sq ft = $5,000."
Limitations: only captures linear relationships. If the price depends on (not just area), it misses it.

Logistic Regression: The Misleading Name
Despite its name, it's a classification algorithm, not regression. It uses the sigmoid function to transform a linear output into a probability between 0 and 1.
The symbols: is the raw score coming out of the linear part of the model (any number, positive or negative); is Euler's number, the constant 2.718... used as the base of the exponential; ("sigma") is the traditional name of this function. In one sentence: the sigmoid squashes any number into the range between 0 and 1, so it can be read as a probability.
The sigmoid has a characteristic S-shape: when z is very large (a very confident decision), the output approaches 1. When z is very small (very negative), it approaches 0. And for values of z close to zero, the transition is smooth and continuous. This property makes it useful: it transforms any real score into a value interpretable as a probability, without ever leaving the [0, 1] interval.
If , we call it "class 1." Otherwise, "class 0."
Strengths: simple, provides probabilities, interpretable.
Limitations: the decision boundary is linear. For complex problems, it's not enough.

SVM (Support Vector Machine): Maximum Separation
SVMs (Support Vector Machines) are algorithms that look for the optimal separation boundary between two classes. Imagine two groups of points (spam vs not spam) on a plane. SVMs look for the line that separates them with the maximum margin: the distance between the line and the closest points.
It's like wanting the widest possible highway between two groups of enemy houses.

With the kernel trick, SVMs also handle non-linear problems: they project the data into a higher-dimensional space where a straight boundary becomes possible again. The image below shows the idea.
Strengths: excellent in high dimensions, robust with little data.
Limitations: slow on very large datasets, less interpretable.

Decision Trees: The Human Algorithm
Divides the space hierarchically with binary questions.
If Area > 650 sq ft
If Neighborhood = Central
-> High price
Else
-> Medium price
Else
-> Low price
It's exactly how a human decides.
Strengths: very readable, no need to normalize data, handles mixed numeric/categorical features.
Limitations: a strong tendency to overfit (learning the training data by heart instead of extracting a rule from it) and real instability: changing a single example can produce a completely different tree.

Random Forests: Many Trees, One Decision
Trains 100 or 1,000 different decision trees (each on slightly different data). For a prediction, asks all trees to vote.
100 trees vote. 60 say "spam," 40 say "not spam." Result: spam.
Strengths: very robust, little overfitting, almost no hyperparameters to tune.
Limitations: less readable than a single tree, slower to train.
XGBoost, LightGBM, CatBoost outperform random forests. Each new tree corrects the errors of the previous one. It's often the winner of Kaggle competitions on tabular data.

KNN (K-Nearest Neighbors): The Lazy Algorithm
The K-Nearest Neighbors algorithm is disarmingly simple: when you need to classify a new example, look at the K closest training examples. Majority vote.
A new customer arrives. Look at their 5 nearest neighbors in the training data. 4 bought, 1 didn't. Prediction: will buy.
Strengths: no training at all, very simple.
Limitations: slow at prediction time, since it must compare the new case against every stored example; and it suffers from the curse of dimensionality, when examples are described by hundreds of variables, every point ends up roughly the same distance from every other, and "nearest neighbor" stops meaning anything.

Choosing Your Algorithm: The Practical Guide
| Situation | Algorithm | Reason |
|---|---|---|
| Little data, high dimension | SVM | Generalizes well |
| Tabular data, max performance | XGBoost/LightGBM | Wins competitions |
| I want to understand | Logistic regression, tree | Interpretable |
| I'm starting out | Linear/logistic regression | Fast and basic |
| Images, text, audio | Deep neural networks | The only family that handles raw data of this kind |
| Mixed data, basic | Random Forest | Robust and easy |
ALWAYS start with a stupidly simple reference model: linear regression for regression, logistic for classification. Five minutes of work, and it gives you the score to beat. Only if that score is not enough do you bring out the heavy machinery.
Why This Matters
Supervised learning is the foundation of the large majority of ML (Machine Learning) applications running in production. This is how you predict prices, detect fraud, and diagnose diseases. It's the best understood paradigm, the most debuggable, and the most reliable. Mastering it means having the foundations of applied AI.
1. Predicting the exact price of an apartment (say 420,000 euros) is a problem ofβ¦
2. A cancer screening model answers Β« no cancer Β» for every patient and scores 99% accuracy, because 99% of patients are healthy. What should you conclude?
3. In bank fraud detection, which metric do you mostly want high, and why?
4. Logistic regression is used toβ¦
5. What happens if some of the labels in the training set are wrong?