MLFSMLFS

Part 1 · Chapter 02

Math You Can't Ignore (Sorry, Bestie)

Vectors, calculus, probability — the only three you actually need.

Okay, deep breaths. We need to talk about math.

I know, I know. You became a developer so you could build cool things, not relive your high school calculus nightmares. You just want to import antigravity and be done with it.

But here's the deal: machine learning isn't magic. It's math. You can't skip this chapter and expect to train a model that does anything other than set your CPU on fire. The good news? You don't need a PhD. You just need to understand three core concepts. We're going to treat this like ripping off a band-aid: quick, a little painful, but you'll feel so much better afterwards.

Vectors & Matrices: Spicy Python Lists

Forget everything you think you know about vectors from physics class. In machine learning, a vector is just a fancy list of numbers that represents... well, anything.

Analogy: The Fruit Stand

Imagine you're at a fruit stand. You want to buy 2 apples, 3 bananas, and 4 clementines. You can represent your shopping list as a vector:

The fruit stand has prices for each item: $1 for an apple, $2 for a banana, and $3 for a clementine. We can represent this as a prices vector:

Now, how do you calculate your total bill? You multiply the corresponding items and add them up:

Congratulations, you just did a dot product.

Live · drag the sliders
Apples
2 × $1 = $2
qty
price
Bananas
3 × $2 = $6
qty
price
Clementines
4 × $3 = $12
qty
price

The dot product is how we "multiply" two vectors to get a single number. It's a measure of their interaction. In Python with NumPy, it's dead simple:

dot_product.py

A matrix is just a stack of vectors. It's a spreadsheet. It's a list of lists. If you had shopping lists for three different people, you could stack them into a matrix:

That's it. Vectors and matrices are just containers for our data. They're spicy arrays that let us do math on a whole bunch of numbers at once.

Calculus: The Science of "How Fast Are We Screwing Up?"

Calculus is all about change. For us, we care about one thing: finding the slope of a curve at a single point. This slope is called the derivative.

Analogy: The Speedometer

Imagine you're driving. Your total trip is 60 miles and it takes you an hour. Your average speed is 60 mph. Boring.

The derivative is your speedometer. It tells you your speed at this exact instant. Right now, you're going 75 mph. A second later, you hit traffic, and you're going 15 mph. The derivative is the instantaneous rate of change.

Why do we care? Because in machine learning, our "curve" is the loss function (which we'll cover in the next chapter). The loss function tells us how wrong our model is. The derivative of the loss function tells us the slope of our error.

slide the parameter — feel the slope
w = 1.20 · slope = -1.52

At w = 5 the slope is zero — you've found the bottom of the valley.

The derivative tells us which way is "downhill" on our error curve. If the slope is negative, we need to increase our parameter to go down. If it's positive, we need to decrease it. If it's zero, we're at the bottom—we've found the minimum error! This process of following the derivative downhill is called Gradient Descent, and it's the engine of modern machine learning.

The joke goes: "The derivative of milk is cheese; the integral of milk is a cow". It's silly, but it captures the idea. The derivative breaks something down into its rate of change (milk → cheese), while the integral builds it up (cow → milk). We're in the cheese-making business.

Probability: A Guided Tour of Your Gambling Addiction

Probability is the language of uncertainty. And nowhere is uncertainty more expensive than in a casino.

Expected Value: The House Always Wins

Every casino game has a negative expected value for the player. This is the average amount you'd expect to win or lose per bet if you played forever.

Let's say you're playing a simple dice game. You bet $1. If you roll a 6, you win $5. If you roll anything else, you lose your $1.

  • Probability of winning (rolling a 6) = 1/6
  • Probability of losing (not rolling a 6) = 5/6

The expected value (EV) is calculated like this:

Huh. This is a fair game. A casino would never offer this. Let's make it more realistic. They pay you $4 if you win.

This means that on average, every time you play, you lose 17 cents. This is the "house edge." A machine learning model's performance is similar. Over thousands of predictions, we want its average error—its expected loss—to be as close to zero as possible.

Bayes' Theorem: Updating Your Beliefs

Bayes' Theorem is a formal way to update your beliefs in the face of new evidence. Let's use a classic example: food allergies.

Let's say the probability that any random person has a peanut allergy is low, maybe 1% (). This is our prior belief.

Now, your friend eats a cookie and their face swells up. This is new evidence. We want to calculate the probability they have an allergy given this new evidence: .

Bayes' Theorem gives us the formula:

This lets us update our initial 1% belief to something much, much higher. This is exactly how the Naive Bayes algorithm (Chapter 8) works: it starts with a prior belief about the classes and updates that belief as it sees new data.

These three pillars—linear algebra for structure, calculus for optimization, and probability for uncertainty—are the bedrock of everything we're about to build. You survived. Now let's use them.