Hey there, fellow ML explorer! 👋
Welcome to my Manual Logistic Regression project, where I decided to ditch scikit-learn, roll up my sleeves, and build logistic regression from scratch using nothing but good old NumPy. Why? Because sometimes, you’ve got to get your hands dirty to really understand what’s going on under the hood.
- Curiosity: I wanted to demystify what those black-box ML libraries actually do.
- Foundations Matter: Every good ML engineer should know the math, not just the API.
- Flexing Skills: Let’s be honest, implementing logistic regression by hand is a rite of passage.
- Sigmoid Function: The magical squishifier that compresses any number into a probability between 0 and 1.
sigmoid(z) = 1 / (1 + exp(-z)) - Binary Cross-Entropy Loss: The measure of how “off” my predictions are (and a gentle nudge toward better weights).
- Gradient Descent: The slow, steady path down the loss mountain to optimal weights.
- Weight Updates: How to actually adjust weights and biases to get my model to stop embarrassing itself.
Imagine you have some data points and you want to draw a boundary to separate “yes” from “no” (cats from dogs, spam from ham, etc.):
- Linear Combo: Multiply features by weights, add bias (
z = w*x + b). - Squish It: Pass
zthrough the sigmoid to get a probability. - Decision Time: If probability > 0.5, call it a 1; else, it's a 0.
No rocket science, but a dash of calculus (those pesky gradients!) goes a long way. 🚀
- Data Setup: Toy binary dataset with one feature (
X) and labels (y). - Sigmoid Function:
def sigmoid(z): return 1/(1+np.exp(-z))
- Training Loop:
- Compute predictions
- Calculate loss
- Compute gradients
- Update weights (
w) and bias (b) - Print loss every few iterations (so you can see the suffering decrease)
- Prediction Function:
def predict(X, w, b, threshold=0.5): probs = sigmoid(w * X + b) return (probs >= threshold).astype(int)
- Evaluation:
- Compare predictions to actual labels
- Calculate accuracy (and feel good about yourself)
- Visualization:
- Plot loss over iterations 📉
- Plot predicted probabilities
You only need to swap out the single weight w for a weight vector (array) and update the math to handle matrix operations.
Hint: Just let NumPy do the heavy lifting with np.dot(X, w)!
- No More Blind Trust: I get why the model learns (or doesn’t).
- Debugging Superpowers: If my model fails, I know where to look.
- Flexibility: I can tweak, tune, and experiment without being handcuffed to someone else’s code.
- Big Brain Energy: My understanding of optimization, loss functions, and model diagnostics went from “meh” to “I get this!”
ManualLogReg.ipynb– The Jupyter notebook where the magic happens
If you’re learning ML, I highly recommend building a few algorithms from scratch. It’s humbling, enlightening, and gives you some serious bragging rights.
So go ahead—fork this repo, run the notebook, and watch the math do its thing!
“The best way to learn is to build (and sometimes break) things yourself.” 😏
Happy squishing those probabilities!