Categorizing the world is the first step toward intelligence. Logistic Regression allows us to predict binary outcomes—Spam or Not, Success or Failure—with mathematical precision.
1The Classification Switch
While Linear Regression predicts continuous numbers (like prices), Logistic Regression is used for classification. It answers binary questions: 'Is this 0 or 1?' It does this by mapping any input to a value between 0 and 1, representing the probability of the positive class.
2The Sigmoid Function
The heart of Logistic Regression is the Sigmoid (or Logistic) function. It's an S-shaped curve that squashes the output of a linear equation. Large positive numbers approach 1, large negative numbers approach 0, and 0 maps exactly to 0.5—our standard Decision Boundary.
3Evaluating Classification
In classification, we don't just check the 'error'. we use a Confusion Matrix to see exactly how many times the model predicted correctly vs incorrectly. We measure Accuracy as the percentage of total correct predictions out of all samples.
4Step-by-Step Breakdown
Linear regression predicts continuous numbers. But what if we want to predict a category? E.g., Spam or Not Spam? That's where Logistic Regression comes in.
Logistic Regression outputs probabilities between 0 and 1. We achieve this using the Sigmoid Function, which squashes any number into that range.
Checkpoint: If our model outputs a probability of 0.35, and our threshold is 0.5, what is the predicted class?
- →Class 1 (True)
- →Class 0 (False)
In Scikit-Learn, we import LogisticRegression. We don't write the math manually—we just initialize the classifier.
Feature Scaling is highly recommended here. It helps the model converge (find the best weights) much faster.
Once trained, we can get binary labels with .predict() or raw probabilities with .predict_proba().
Checkpoint: Which Scikit-Learn method would you use to get the probability percentage instead of the label?
- →.predict()
- →.predict_proba()
You've successfully built your first classifier! You're ready to start predicting the future of binary events.
Implement the Real Sigmoid Function. Finish implementing the sigmoid function that powers logistic regression's probability output.
Level Up 🚀
Advanced cheat sheets, SEO tricks, and interview prep for this topic.
Browser Support
Fully supported.
Fully supported.
Fully supported.
Fully supported.
Accessibility (A11y)
1State the Predicted Probability, Not Just the Binary Label
A prediction shown only as a class label ('Spam') hides how confident the model actually was — surface the underlying probability in text ('87% likely spam') so users get a graded confidence signal instead of a false sense of certainty from a bare true/false output.
<p>Prediction: Spam (87% confidence)</p>SEO Implications
- 1
The Sigmoid Curve and Fitted Weights Exist Only in the Model Object
A trained LogisticRegression's coefficients live in memory or a serialized file, never as page content — this tutorial's SEO value is its own explanation of the sigmoid function and decision boundary, not any specific model's learned weights.
Best Practices
Adjust the Decision Threshold to Match Business Costs, Not Just Use 0.5
The default 0.5 threshold treats false positives and false negatives as equally costly, which is rarely true in practice. Use predict_proba() and choose a custom threshold (0.3, 0.7) that reflects the actual relative cost of each error type for your specific problem.
Check for Class Imbalance Before Trusting Default Accuracy
Logistic Regression trained on heavily imbalanced classes (99% negative, 1% positive) can achieve high accuracy by nearly always predicting the majority class. Use class_weight='balanced' or resampling techniques, and evaluate with precision/recall rather than accuracy alone.
Frequent Bugs
Interpreting the raw output of the linear equation (before the sigmoid) as a probability.
The linear part of Logistic Regression (w·X + b) can output any real number, positive or negative, and is not itself a probability — only after passing through the sigmoid function does it become a value between 0 and 1. Always call .predict_proba() to get actual probabilities rather than trying to interpret model.decision_function() output directly as one.
Real-World Examples
Tuning the Decision Threshold for Loan Default Prediction
A lending platform's Logistic Regression model outputs a default-risk probability for each applicant, but instead of using the default 0.5 cutoff, the risk team sets the approval threshold at 0.3 — deliberately erring toward rejecting more borderline applicants, because the cost of one bad loan outweighs the lost interest from several rejected good ones.
probs = model.predict_proba(X_test)[:, 1]
decisions = (probs >= 0.3).astype(int) # custom threshold, not default 0.5