Logistic Regression: Model, Training, and Worked Examples
Logistic regression is a widely used method for predicting a binary outcome from features by modeling the log-odds as a linear function of inputs. It outputs a probability in using the sigmoid function so that the probability increases/decreases smoothly with the predictors.
A standard form for a single observation is:
- features
- model score (logit)
- predicted probability with
The key modeling assumption is that the log-odds are linear in :
Training is typically done via maximum likelihood estimation by minimizing the log loss (equivalently, maximizing likelihood).
When logistic regression works well: when the relationship between features and the log-odds is approximately linear, and when classification performance is improved by regularization rather than by complex nonlinearities.
Important: Logistic regression is not restricted to “straight lines” in feature space; if you add polynomial or transformed features, the decision boundary can become nonlinear in the original input space.
Footnotes
-
Logistic regression - Wikipedia - Defines logit (log-odds), sigmoid mapping to probability, and maximum likelihood/log loss training. ↩
-
Logistic regression - Wikipedia: model and decision boundary - Discusses linear decision boundary/hyperplane and interpretation via log-odds. ↩ ↩2
Logistic Regression (Intuition + Math) — Sigmoid, Odds, and Decision Boundary
1) Model description with a concrete probability example
Consider a customer churn model:
- means “churn”
- = number of support tickets (scaled), = tenure in months (scaled)
Let weights be and . For a customer:
- ,
- score:
- probability:
So logistic regression predicts a 41.3% churn probability. If you classify with threshold , this customer would be predicted as “not churn.”
A useful interpretation comes from the log-odds form:
Each coefficient represents how the log-odds change when feature increases by 1 unit (holding others fixed).
Odds ratio viewpoint (interpretation): if increases by 1, log-odds increase by , so odds multiply by .
For example, if , then increasing support tickets by 1 increases odds of churn by a factor of (about a 123% increase in odds).
Footnotes
-
Logistic regression - Wikipedia - Defines logit (log-odds), sigmoid mapping to probability, and maximum likelihood/log loss training. ↩ ↩2
2) Decision boundary and classification threshold
The predicted class often uses a threshold (commonly ):
- predict if
- otherwise predict
Because , the set of points where corresponds to:
since . That means the default decision boundary is a hyperplane in feature space.
If you use a different threshold , then solve:
So the boundary shifts accordingly.
Example (thresholding): In medical screening, you may prefer high recall (fewer false negatives). You might set a lower threshold so more patients are flagged positive.
Training logistic regression with log loss (high level)
- 1Step 1
Use for each training example .
- 2Step 2
For each with , use .
- 3Step 3
Compute total loss (or average loss).
- 4Step 4
Minimize using an optimizer (e.g., gradient-based methods).
- 5Step 5
Add a penalty such as (ridge-style) to improve generalization.
Footnotes
-
Logistic regression - Wikipedia: regularization - Describes regularized logistic regression objectives (e.g., L2/L1 as common choices). ↩
-
- 6Step 6
Convert probabilities to labels using .
3) Worked examples (from intuition to computation)
Example A: Spam vs. not spam (text features)
Let:
- be a vector of engineered features like word frequencies or TF-IDF weights.
- means “spam.”
For a given email, the model computes:
If , the model assigns high probability of spam.
Practical note: logistic regression is commonly used with high-dimensional sparse text features because it remains linear in parameters and can be efficiently regularized.
Example B: Credit default risk (calibration + threshold tradeoff)
Let:
- means “default”
- includes income, debt ratio, credit history summary variables.
If an account has , the model believes there is a 12% chance of default given those features.
A risk team might:
- deny credit if exceeds a cutoff driven by business costs
- or set different cutoffs to balance false positives and false negatives.
This illustrates why logistic regression outputs probabilities rather than only hard classes—useful for decision-making.
Footnotes
-
Logistic regression - Wikipedia: model and decision boundary - Discusses linear decision boundary/hyperplane and interpretation via log-odds. ↩
How coefficient signs affect probability (toy illustration)
Suppose with . Then increasing changes according to the sign of .
4) Regularization and variants (why / matter)
In real datasets, features may be correlated and models may overfit. Logistic regression is often trained with regularization:
- L2 regularization shrinks weights smoothly
- L1 regularization encourages sparsity (feature selection)
A typical regularized objective is:
Libraries (e.g., scikit-learn) implement logistic regression with different solvers and penalties; in practice, practitioners tune (inverse regularization strength) and choose .
Footnotes
-
Logistic regression - Wikipedia: regularization - Describes regularized logistic regression objectives (e.g., L2/L1 as common choices). ↩
-
scikit-learn LogisticRegression documentation - Documents solver choices, penalty options, and as inverse regularization strength. ↩
Use “log-odds linearity” as your diagnostic
If plots of predictors vs. log-odds show strong curvature, consider feature transforms, interactions, or a more flexible model.
Probability calibration vs. classification accuracy
High accuracy doesn’t guarantee well-calibrated probabilities. If you rely on values (not just ranking), consider calibration checks (e.g., reliability plots).
5) Assumptions, strengths, and limitations
Assumptions (modeling level)
- Independence of observations (common ML assumption).
- Linear relationship between predictors and log-odds (after feature engineering).
- Sufficient representational power via feature transformations.
Strengths
- Interpretable coefficients (odds ratios).
- Works well for sparse/high-dimensional inputs with regularization.
- Produces probabilities directly.
Limitations
- If the true relationship between and log-odds is highly nonlinear and not captured by feature engineering, performance can suffer.
- Sensitivity to feature scaling when using gradient-based solvers (standard practice: standardize features).
Footnotes
-
Logistic regression - Wikipedia: model and decision boundary - Discusses linear decision boundary/hyperplane and interpretation via log-odds. ↩ ↩2 ↩3
-
scikit-learn LogisticRegression documentation - Documents solver choices, penalty options, and as inverse regularization strength. ↩
Common exam-style questions (with examples)
How logistic regression is typically applied (end-to-end)
Feature engineering
1) Prepare featuresScale/transform inputs; consider interactions/polynomials if needed."
Train with regularized log loss
2) Fit parametersOptimize by minimizing negative log-likelihood (with penalty)."
Select threshold & tune
3) ValidateTune hyperparameters; evaluate with relevant metrics."
Predict probabilities
4) DeployUse for ranking and threshold-based decisions."
Logistic Regression Quick Deck
Knowledge Check
In logistic regression, the model uses with . What quantity is linear in ?
Explore Related Topics
Bayesian Classifiers: Definition, Naive Bayes, and Why the Correct Choice Is (i)
Machine Learning Foundations and Lifecycle
Machine learning is an AI subfield that builds models to learn patterns from data, covering its paradigms, lifecycle, mathematics, and common algorithms.
- Supervised, unsupervised, and reinforcement learning describe the three main paradigms.
- Standard dataset partitioning allocates 70 % for training, 15 % for validation, and 15 % for testing.
- The ML lifecycle progresses through problem definition, data collection/preprocessing, feature engineering, model training, evaluation/tuning, and deployment/monitoring, with data quality and overfitting as key concerns.
- Understanding linear algebra, calculus (gradient descent), and probability/statistics is essential for model development.
- Typical algorithms include linear regression, decision trees, k‑means clustering, and neural networks.
Complexity Analysis: Best Case, Worst Case, and Average Case
The material introduces best‑case, worst‑case, and average‑case complexity as three distinct functions describing an algorithm’s running time on inputs of size , explains how they are formally defined, and shows why worst‑case analysis is usually preferred.
- Best case: , the minimum time over all inputs of size .
- Worst case: , giving a guaranteed upper bound.
- Average case: , requiring an explicit input probability model.
- Linear search illustrates the three cases: best, worst, and average (expected comparisons).
- Worst‑case analysis is favored because it needs no probabilistic assumptions and ensures reliability for all inputs, especially in real‑time or safety‑critical systems.