0:00Intro: MOFUSA Tech Academy
Welcome to MOFUSA Tech Academy. Beyond learning, towards mastery.
0:09Will this customer leave?
Sana has a small budget for win-back coupons. Not enough for everyone. She needs to know which customers are about to leave, and how likely each one is to go, so the coupons go where they'll matter most. This isn't "how much". It's "yes or no": will they leave? What we want from a model is a probability, a number between zero and one. We could try the linear regression from Module Three. But watch: it predicts a probability of minus zero point zero three for a recent shopper, and one point two three for a customer who's been away seventy days.
Probabilities can't be negative, or bigger than one. We need a model that's just as simple, just as explainable, but always speaks in probabilities. It's called logistic regression, and despite the name, it's the classic starting point for classification. Season One, Module Four: classification. First stop: logistic regression.
1:10Why a straight line fails
Let's see the problem clearly. Here are ten Brightcart customers. The horizontal axis is how many days since their last order. Customers who stayed sit at zero. Customers who left sit at one. A straight line through these points escapes the zero-to-one range at both ends. And it has a second weakness: add one unusual customer, someone who hasn't ordered in two hundred days but still hasn't left, and the line flattens so much that it now predicts nobody will leave at all. One unusual customer has ruined it for everyone.
What we want is a curve that stays between zero and one, that's almost flat at the extremes, and that switches smoothly in the middle. An S-shape.
1:59The sigmoid function
That S-shape has a name: the sigmoid, or logistic function, written as sigma. Sigma of z equals one over one plus e to the minus z. Think of it as a squasher. Feed in any number at all, from minus a million to plus a million, and out comes a number between zero and one. Feed in zero and you get exactly a half. Feed in two and you get about zero point eight eight. Minus two gives zero point one two. Big positive numbers get squashed towards one, big negative ones towards zero. Notice the symmetry: zero point eight eight and zero point one two add up to one.
And one fact to keep for later: the sigmoid's slope is sigma times one minus sigma. It's steepest in the middle and nearly flat at the ends. That will matter a lot when we reach neural networks.
2:55The logistic regression model
Logistic regression is just two steps. First, compute a score exactly like linear regression: weights times features, plus a bias. We'll call that score z. Second, push z through the sigmoid to turn it into a probability. For Brightcart, the trained model is: the probability of leaving equals sigma of zero point one seven six six times the days since the last order, minus five point two two two. We'll see how it's trained in a moment. Let's try a customer who last ordered forty days ago. The score is zero point one seven six six times forty, minus five point two two two: one point eight four.
Through the sigmoid, that's zero point eight six. An eighty-six percent chance this customer is leaving. They're a strong candidate for one of Sana's coupons. Watch the curve across all ten customers: tiny probabilities for recent shoppers, rising steeply through the twenty-to-forty-day range, and near certainty after fifty days.
4:02Odds and log-odds
What does the weight zero point one seven six six actually mean? To answer that, we need odds, a word gamblers know well. The odds are the probability of something happening divided by the probability it doesn't. A probability of zero point eight means odds of four to one. A probability of a half means even odds, one to one. Here's the hidden structure of logistic regression. If you take the natural log of the odds, you get exactly the linear score z. So the model is linear after all: linear in the log-odds. That gives the weight a precise meaning.
Each extra day since the last order adds zero point one seven six six to the log-odds, which multiplies the odds of leaving by e to the zero point one seven six six, about one point one nine. Roughly nineteen percent higher odds of leaving for every day that passes, so the odds double about every four days. That's a sentence Sana can take straight to her team.
5:08Threshold and decision boundary
A probability is useful, but sometimes we need a decision: coupon or no coupon. The usual rule is a threshold: if the probability is above a half, predict "leaves". The probability crosses a half exactly where the score z is zero, which for Brightcart is at twenty-nine point six days. That's the decision boundary. Customers to the right are predicted to leave. The model isn't perfect. The customer at twenty-five days left even though the model thought they'd stay, and the one at thirty-three days stayed even though the model expected them to go.
Real customers are like that. And a half isn't sacred. If coupons are cheap and losing a customer is expensive, Sana might lower the threshold to zero point three to catch more leavers, accepting more false alarms. Choosing the threshold is a business decision, and next episode we'll learn the tools for making it well. With two features, the decision boundary becomes a straight line across the plane. With more features, it's a flat hyperplane. That's why logistic regression is called a linear classifier.
6:19Log loss (binary cross-entropy)
To train the model, we need a cost function. For probabilities of yes-or-no outcomes, the standard choice is binary cross-entropy, also called log loss. You may remember it from our cost-functions briefing. For a customer who really left, the loss is minus the log of the probability we gave to "left". Say ninety-nine percent, and the loss is tiny. Say fifty percent, and it's about zero point seven. Say one percent, and the loss shoots up to four point six. Confident mistakes are punished very hard. That's the point.
For a customer who stayed, it's the mirror image: minus the log of one minus p. The full formula just switches between the two halves, depending on the true answer. Our forty-day customer, at eighty-six percent: they did leave, so the loss is only zero point one five. Had they stayed, the loss would have been nearly two. Averaged over all ten customers, the trained model's cost is zero point two six six. Why not just use mean squared error? Because with the sigmoid inside, squared error makes a bumpy cost surface with flat stretches where gradient descent stalls.
Log loss gives a single smooth bowl. And as we saw in episode four, it's exactly the negative log-likelihood. It's what you get by asking which parameters make the observed outcomes most probable.
7:49Training: the gradient
Training uses gradient descent, just like before. And something beautiful happens when we take the derivative. The sigmoid's slope, sigma times one minus sigma, cancels perfectly with part of the log loss. The gradient for each weight comes out as simply p minus y, times x. Look at that next to linear regression. Prediction minus truth, times the input. The same shape. Error times input, again. Here's the real training run. At the start, with every weight at zero, the model says fifty percent for everyone, and the cost is about zero point six nine.
As it trains, the curve steepens and slides into place, and the cost falls to zero point two seven. Unlike linear regression, there's no formula that solves this in one step, so we always iterate. Libraries often use faster relatives of gradient descent, and they usually add L2 regularisation by default.
8:47Beyond two classes: softmax
What if there are more than two answers, like classifying a product photo as a mug, a lamp or a kettle? We compute one score per class, then use a generalisation of the sigmoid called softmax, which turns any list of scores into probabilities that add up to one. With just two classes, softmax and the sigmoid are the same thing. Keep softmax in mind. A language model choosing its next word is doing exactly this, over a vocabulary of tens of thousands of words.
9:22Try it yourself: two customers
Let's use the model. One customer last ordered twenty days ago, another fifty days ago. What are their chances of leaving? Pause and work out the scores first. For twenty days: zero point one seven six six times twenty is three point five three, minus five point two two two gives minus one point six nine. The sigmoid of that is about zero point one six: a sixteen percent chance of leaving. For fifty days: eight point eight three minus five point two two, three point six one, which squashes to about zero point nine seven.
Remember the coupon rule from episode four, which said a coupon is worth it above about seventeen percent, under those business assumptions? The twenty-day customer sits just below the line; the fifty-day customer is a clear yes. That's how a probability becomes an action.
10:24More than one feature
Real churn models use many features. Add the number of orders in the last ninety days, and the score becomes a weighted sum of both features plus a bias. The decision boundary is now a straight line across the plane, and the probability fades smoothly from red to green across it. Each weight still has the odds interpretation: every extra unit of that feature multiplies the odds of leaving by e to the weight. A negative weight on recent orders means that customers who've placed more orders in the last ninety days are less likely to leave, holding the days-since-last-order fixed.
As always: scale your features, and use a little L2 regularisation, which most libraries apply by default.
11:14When one class is rare
In real life, the interesting class is often rare: only one Brightcart customer in ten leaves in a given month. A model trained on that data learns, quite correctly, that leaving is unusual, so its probabilities tend to be low, and a fifty percent threshold flags very few customers. There are three common responses. The simplest is to choose a lower threshold based on the costs of each mistake, which we'll do properly next episode. Another is to give the rare class more weight during training, so each missed leaver counts more in the loss.
A third is to resample the training data, with more leavers or fewer stayers. Just remember that weighting and resampling distort the probabilities, so if you need real-world odds, recalibrate afterwards.
12:06Are the probabilities trustworthy?
When Sana's coupon rule depends on the probability itself, the probabilities need to be trustworthy. A calibration check groups customers by their predicted probability, say all customers given between thirty and forty percent, and compares that with the share who actually left. For a well-calibrated model, about thirty-five percent of that group should leave. Because logistic regression is trained with log loss, its probabilities are often reasonably well calibrated. But that's not guaranteed, especially after resampling, strong regularisation, or when customer behaviour shifts.
So check it on held-out data before betting money on it.
12:48Logistic regression and modern AI
Here's why this episode matters so much for the rest of the course. Look at the formula: a weighted sum, plus a bias, passed through a squashing function. In Season Two, that's exactly what a single artificial neuron does. Stack thousands of them in layers, and you have a neural network. And the very last step of a classifier, turning scores into probabilities with softmax and training with cross-entropy, is the same idea we learned today. Even a large language model ends each step this way, choosing the next word from a softmax over its vocabulary.
Logistic regression is the seed from which modern AI grew.
13:30Myth vs Fact
Myth: logistic regression is a regression method, because it says so in the name. Fact: it's a classification method. The name comes from what happens inside, a linear regression on the log-odds. And it's a workhorse in fields like credit scoring and medicine, because it's fast, gives probabilities, and every coefficient can be explained.
13:55Recap
Let's recap. Straight lines can't produce probabilities, so logistic regression computes a linear score and squashes it with the sigmoid. That score is the log-odds, so each weight multiplies the odds by e to the w. A threshold turns probabilities into decisions, and it gives a linear decision boundary. The model trains by minimising log loss, which punishes confident mistakes, and its gradient has the familiar shape, error times input. One sentence to remember: score it linearly, squash it with a sigmoid, and punish confident mistakes.
14:34Next: measuring classifiers
Mission complete. Every Brightcart customer now has a churn probability, and Sana's coupons go to the customers most likely to leave. But here's a question that fools a surprising number of people. Brightcart's full churn model is ninety-two point five percent accurate. Sounds great. What if I told you a model that does nothing at all scores ninety percent? Next episode: how to measure a classifier properly. See you on the map.