5 Kitty Training School
Linear Regression, Logistic Regression, SVMs, Decision Trees
🐾 Evergreen Chapter: Core mathematics and theory do not expire. Last verified: 2026-02-24
Prerequisites: Chapter 1-4 Technical Keywords: Supervised Learning, Linear Regression, Logistic Regression, Decision Trees, Random Forests, SVM, Feature Engineering, Overfitting, Regularization (L1/L2)
Imagine Pixel is sitting in the center of the room. The human holds up a square toy and says, “Block.” The human holds up a round toy and says, “Ball.”
After doing this a hundred times, the human holds up a brand-new, slightly squished round toy and waits. Pixel stares at it, processes the shape, and meows, “Ball!”
The human gives him a treat.
This is Supervised Learning. We give the AI a set of inputs (the toys) and their correct answers (the labels). The optimization engine tweaks the kitten’s brain until its guesses match the human’s labels. Once trained, the kitten can predict the labels of things it has never seen before.
Let’s look at the different mathematical “brains” we can give Pixel to solve these puzzles.
5.1 The Setup: Features (X) and Labels (y)
In Supervised Learning, every dataset is split into two parts: 1. X (Features): The inputs. The weight, color, and fuzziness of the toy. In math, this is a Matrix (from Chapter 1). 2. y (Labels/Targets): The correct answers. “Ball”, “Block”, or “3.5 treats”. In math, this is a Vector.
If y is a continuous number (like predicting exactly how many treats are in the jar), the task is called Regression. If y is a category (like predicting “Dog” vs. “Cat”), the task is called Classification.
5.2 Linear Regression: Drawing the Sunbeam Line
Pixel wants to predict exactly how long his afternoon nap will be (y, in minutes) based on how bright the sunbeam is (X, in lumens).
He plots all his past naps on a graph. Linear Regression is the algorithm that draws the best possible straight line through that cloud of dots.
Math time! The formula for a straight line is: \hat{y} = w \cdot x + b
- x: The input (brightness).
- w (Weights): The slope of the line. How much does one unit of brightness increase nap time?
- b (Bias): The y-intercept. Even if brightness is 0, Pixel still naps a little bit.
- \hat{y} (y-hat): Pixel’s prediction (which we hope matches the true y).
To find the perfect w and b, Pixel uses Gradient Descent (Chapter 3) to minimize the Mean Squared Error (MSE) between his line and the true data points.
Analogy
Linear Regression is stretching a piece of yarn through a scattering of toys on the floor so that the yarn is as close to all the toys as possible.

Rosetta Stone
| Kitten World | Math | Python (scikit-learn) |
|---|---|---|
| “The yarn line” | \hat{y} = Xw + b | model = LinearRegression() |
| “Learn from the toys” | Optimize w, b | model.fit(X_train, y_train) |
| “Guess the next nap” | Predict \hat{y} | y_pred = model.predict(X_new) |
5.3 Logistic Regression: The “Yes or No” S-Curve
Despite the name, Logistic Regression is used for Classification, not regression!
Pixel wants to predict: “If I scratch the sofa, will the human yell at me?” The answer isn’t a continuous number. It’s binary: 1 (Yes) or 0 (No).
If we use a straight line (Linear Regression) to predict 1s and 0s, the line will eventually shoot off into infinity, predicting answers like “The human will yell at you 4,000%.” That makes no sense. Probabilities must live between 0 and 1.
So, we take our straight line (wx + b) and shove it through a mathematical trash compactor called the Sigmoid Function (\sigma). This function squishes any number into an S-shaped curve between 0 and 1.
\sigma(z) = \frac{1}{1 + e^{-z}} \hat{y} = \sigma(w \cdot x + b)
Now, the output is a pure probability. If \hat{y} = 0.85, there is an 85% chance of being yelled at.
5.4 Decision Trees & Random Forests: Kitty Logic Flowcharts
Not all data can be separated by a straight line. Sometimes, Pixel makes decisions using a flowchart.
- Did the human move their hand?
- No: Go to sleep.
- Yes: Is it holding food?
- Yes: Meow loudly.
- No: Is it holding the nail clippers?
- Yes: Hide under the bed.
- No: Pounce on the hand.
This is a Decision Tree. At every step, the algorithm splits the data based on the feature that reduces uncertainty (Entropy, from Chapter 2!) the most.
But a single Decision Tree has a flaw: it takes things too literally. If you rely on one tree, it will memorize every weird, highly specific rule of your house.
To fix this, we use a Random Forest (Breiman, 2001). We create 100 different Decision Trees, give each of them a slightly randomized subset of the data, and let them vote. If 90 trees vote “Pounce” and 10 trees vote “Hide”, Pixel pounces.
Analogy
One kitten might be paranoid and think every hand movement is the nail clippers. A council of 100 kittens voting on what to do is much wiser and more robust. This concept is called Ensembling.
5.5 Support Vector Machines (SVM): The Widest Scratching Post
Imagine you have a bunch of red laser dots (Dogs) and green laser dots (Cats) on the floor. You want to draw a line to separate them.
You could draw a line right up against the cats. You could draw one right up against the dogs. But a Support Vector Machine (SVM) says: “Draw the line exactly in the middle, making the gap (the margin) between the dogs and cats as impossibly wide as you can.”
The points sitting right on the edge of this margin are the Support Vectors. They are the only points that actually matter; if you move them, the line moves.
What if the dots are mixed in a circle and you can’t draw a straight line? SVM uses the Kernel Trick. It mathematically throws all the dots into the air (adds a dimension, relating back to Chapter 1), turning a flat 2D floor into a 3D bowl. Suddenly, you can slide a flat sheet of cardboard right between the flying dogs and the cats!
5.6 Feature Engineering: Crafting Smarter Toys
Sometimes the raw data (X) isn’t enough. You are trying to predict if Pixel will wake up. You feed the model two features: sound_loudness and sound_pitch. The model struggles.
But as a clever human, you realize that high-pitch + high-loudness = a vacuum cleaner. So you multiply the two columns together to create a brand new column: vacuum_danger_index.
Suddenly, the model achieves 100% accuracy.
This is Feature Engineering. It is the art of combining, transforming, and massaging your data using human intuition before you ever feed it to the model. Deep Learning (Phase III) will eventually do this for us, but in Classical ML, Feature Engineering is where you make or break your model.
5.7 Overfitting: The Danger of Memorization
This is the most important concept in all of Machine Learning.
Imagine Pixel learns that every time the human wears blue socks, he gets a treat. The human happened to wear blue socks on three treat days in a row. Pixel writes this rule in stone. The next day, the human wears red socks and gives him a treat. Pixel is completely paralyzed. His brain throws an error.
Pixel has Overfit. He didn’t learn the general, underlying pattern (Human = Treats). Instead, he memorized the ultra-specific, noisy details of his training room (Blue Socks = Treats).
When a model overfits, its Loss on the Training Data drops to almost zero. But when you test it on new, unseen data (the Test Set), its error explodes. It cannot generalize.
Catastrophic Failure: Underfitting vs. Overfitting
- Underfitting: The model is too simple. It just guesses “Treats” every time. It hasn’t learned anything. (High Train Error, High Test Error).
- Overfitting: The model is a conspiracy theorist. It memorized every speck of dust in the training room. (Low Train Error, Massive Test Error).
- The Goldilocks Zone: The model learns the general shape of the data but ignores the noise.
5.8 Regularization: Putting a Weight Vest on the Kitten
How do we stop Pixel from overfitting? We make it mathematically painful for him to create overly complicated rules. We add a penalty to the Loss Function called Regularization.
L_{total} = \text{MSE} + \lambda (\text{Complexity Penalty})
We force the optimizer to not only minimize the error but also keep the weights (w) as small and simple as possible.
- L1 Regularization (Lasso): The penalty is the absolute value of the weights (|w|). L1 is ruthless. When a feature isn’t needed by the fitted model (like the color of the socks), L1 can shrink its weight to exactly 0, leaving that feature unused in the prediction.
- L2 Regularization (Ridge): The penalty is the squared value of the weights (w^2). L2 shrinks all the weights smoothly, so no single feature has too much power. It puts a heavy weight vest on the model so it can’t make wild, overly complex jumps.
5.9 Removing the Training Wheels
The Real Engineering
We used kittens voting and drawing yarn lines to explain Supervised Learning. In reality, Random Forests are ensembles of decision trees splitting on Gini impurity or entropy. Logistic regression isn’t a magical S-curve machine; it’s a linear combination passed through a sigmoid to output pseudo-probabilities. Regularization (L1/L2) mathematically adds the parameter norms to the loss, violently forcing the optimizer to shrink unused weights to zero. Overfitting means your model is memorizing the noise, completely destroying its out-of-sample generalization.
5.10 Paws and Reflect: Chapter Quiz
Work through these conceptually before checking the answers.
You are building an AI to predict the temperature of Pixel’s favorite sunbeam in Celsius. Should you use Linear Regression or Logistic Regression?
A Decision Tree reaches 100% accuracy on its training data, but only 50% accuracy on new data. What just happened?
What is the main advantage of a Random Forest over a single Decision Tree?
You add L1 Regularization to your model. After training, you notice that out of your 100 input features, 85 of them now have a weight of exactly 0.0. What did L1 do?
Reveal answers
- Linear Regression. Temperature is a continuous number. Logistic Regression is only for probabilities/classification.
- The tree Overfit! It grew so deep that it memorized the training data perfectly, losing its ability to generalize.
- Ensembling. The “wisdom of the crowd” prevents the model from overfitting to the noise that a single tree might memorize.
- L1 performed sparse feature selection. A zero coefficient means this fitted model does not use those 85 features to make predictions—though it does not prove they are useless or inherently redundant (for instance, among strongly correlated features, L1 often keeps one and zeros out the others).
5.11 Chapter 5 Summary
You’ve learned the classic algorithms of the ML world:
- Linear Regression: Fits a straight line to predict continuous numbers.
- Logistic Regression: Uses the Sigmoid S-curve to predict probabilities for classification.
- Decision Trees & Random Forests: Uses flowcharts and the “wisdom of the crowd” to handle non-linear logic.
- SVM: Finds the absolute widest gap between categories, using the Kernel Trick for complex spaces.
- Feature Engineering: The human art of combining data to make it smarter.
- Overfitting: The cardinal sin of ML—memorizing the training data instead of learning the general rules.
- Regularization (L1/L2): The mathematical weight vest that forces models to stay simple and robust.
Pixel is now a very smart kitten. He can classify toys and predict nap times. But how do we prove he is smart? We can’t just trust him. We need to grade him.
It is time for the ultimate exam. 🐈📊
AI Safety Thread: Chapter 5
The Clever Hans Effect: In the early 1900s, a horse named Clever Hans was famous for being able to do math. People would ask him “What is 2+2?” and he would tap his hoof 4 times. It was later discovered the horse couldn’t do math at all; he was just watching his trainer’s face. When the trainer tensed up expecting the 4th tap, the horse stopped tapping.
When your AI model Overfits, it is often acting like Clever Hans. It isn’t learning what a “Dog” is. It is learning that “photos of dogs usually have green grass in the background.” If you show it a photo of a dog on the beach, it will confidently predict “Not a Dog.” Always investigate what features your model is actually using to make its decisions.