Day21 :- Spam Detector
Spam Detector — Logistic Regression from Scratch
Here is how most beginner tutorials solve spam detection:
Four lines. It works. And it teaches you almost nothing.
What does fit() actually do? It runs an optimization loop — iteratively computing predictions, measuring error, computing gradients of that error with respect to every weight, and nudging each weight in the direction that reduces the error. The LogisticRegression object hides all of that behind a single method call. The cost is not just abstraction — it is the inability to answer the questions that matter when something goes wrong in production:
Why is my model's loss oscillating instead of decreasing?
Why does accuracy collapse to 50% after the first epoch?
Why does my gradient norm blow up on long emails but not short ones?
You cannot answer those questions by reading the sklearn source code at 2am during an incident. You answer them by having built it yourself once, from the math up.
The Failure Mode
Here is what happens when an engineer writes logistic regression from scratch for the first time without knowing the numerical landmines:
Failure 1 — log(0) produces NaN and poisons the entire gradient:
When sigmoid outputs 1.0 exactly (which happens for large positive z), np.log(1 - 1.0) computes np.log(0.0) → -inf. Multiply by a small label value and you get nan. That NaN propagates into every gradient, every weight update, every subsequent prediction. Your loss curve prints nan from epoch 3 onward. Weights go to nan. The model is dead and it will not tell you why.
Keep going with the full lesson
The rest of this lesson — plus its implementation guide, runnable scripts and source code — is included with the course or an All Access plan. Create a free account to start the free lessons in every course.
Already a member? Sign in