🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
💼 Servicesℹ️ About✉️ ContactView Pricing Plansfrom $10

Logistic Regression: Sigmoid, Decision Boundary and Multi-class

Module 7: Machine Learning FundamentalsLogistic Regression🟢 Free Lesson

Advertisement

Logistic Regression: Sigmoid, Decision Boundary and Multi-class

The Bridge from Regression to Classification

Logistic regression extends linear regression to classification problems. Despite its name, it's a classification algorithm that predicts probabilities using the sigmoid function.

From Linear to Logistic: The Transformation

Linear Regression → Logistic RegressionLinear RegressionFeature (x)Target (y)ŷ = 1.8 ≤Predicts: ŷ ≈ (-≡, +≡)f(z)Logistic RegressionFeature (x)P(y=1)0.5Predicts: P(y=1) ≈ [0, 1]

How this diagram works: This side-by-side comparison illustrates the critical transformation from linear regression to logistic regression. On the left, linear regression predicts unbounded values (like ŷ = 1.8) that go outside the valid probability range — making it unsuitable for classification. On the right, the sigmoid function f(z) squashes any input into the [0, 1] range, producing valid probabilities. The S-shaped curve creates a natural decision boundary at 0.5, where points above the threshold are classified as Class 1 and below as Class 0.


1. The Sigmoid Function

Mathematical Definition

Sigmoid (Logistic) Function:

Properties:

  • Output range: — always a valid probability
  • Symmetry:
  • Derivative:
  • At z = 0: (decision boundary)
  • Asymptotes: ,
Sigmoid Function: f(z) = 1/(1 + e⁻ᶻ)zf(z)0.5z=0 → ý0.5y = 1y = 0Class 1 (P > 0.5)Class 0 (P < 0.5)è-3)≈0.05è3)≈0.95Decision boundary

Derivative of Sigmoid

The derivative has an elegant form that enables efficient backpropagation:

Derivation:

Key Insight: The derivative is expressed in terms of the function itself — no recomputation needed!

Log-Odds Interpretation

From Probability to Log-Odds:

Interpretation:

  • = odds (ratio of success to failure)
  • = log-odds (logit function)
  • = change in log-odds for unit increase in
  • = odds ratio — multiplicative effect on odds
Probability Odds Log-Odds
0.010.0101≈4.60
0.10.111≈2.20
0.30.429≈0.85
0.51.00.0
0.72.3330.85
0.99.02.20
0.9999.04.60

2. Decision Boundary

Linear Decision Boundary

The decision boundary is the hypersurface where , which occurs when .

Binary Classification (2D Features):

This is always a line (hyperplane in higher dimensions).

Linear Decision BoundaryFeature x₁Feature x₂w₁x₁ + w₂x₂ + b = 0Class 1Class 0Decision Boundaryw (normal)

Non-Linear Decision Boundaries

By adding polynomial features, logistic regression can learn non-linear boundaries:

Polynomial Features:

This creates elliptical, parabolic, or other conic section boundaries.

Non-Linear Decision Boundaries via Polynomial FeaturesLinearCircularElliptical

Add polynomial features: x₁², x₂², x₁x₂ to learn curved boundaries


3. Cost Function: Binary Cross-Entropy

Why Not MSE for Classification?

MSE with Sigmoid Creates Non-Convex Loss:

  • Problem: Multiple local minima make optimization unreliable
  • Gradient: Near saturation ( or ), gradient → slow learning
MSE Loss Surface (Non-Convex) vs Cross-Entropy (Convex)MSE Loss (Non-Convex)Local minLocal minMultiple local minima → unreliableCross-Entropy Loss (Convex)Global minConvex → guaranteed global minimum

Binary Cross-Entropy Derivation

Single Sample Loss:

For : → penalizes low confidence predictions

For : → penalizes high confidence wrong predictions

Combined:

Cross-Entropy Loss ComponentsPredicted Probability (p̂)Loss-log(p̂) when y=1-log(1-p̂) when y=0p̂ = 0.5When y=1 and p̂→0: Loss→≡When y=0 and p̂→1: Loss→≡Penalizes confident wrong predictions

Average Cross-Entropy Loss

Full Cost Function:

Where

Gradient (same form as linear regression!):

Cross-Entropy Loss: Convex OptimizationOptimumGradient Descent Pathw₁w₂

4. Maximum Likelihood Estimation

Likelihood Function

Bernoulli Likelihood:

Each observation follows

Joint Likelihood (i.i.d. samples):

Log-Likelihood (easier to optimize):

Negative Log-L likelihood = Cross-Entropy Loss:

Gradient Derivation

Chain Rule Application:

Where:

Result:

The terms cancel beautifully!


5. Multi-class Classification

One-vs-Rest (OvR)

Train binary classifiers, one per class:

One-vs-Rest Strategy: K Binary ClassifiersClassifier 1Class 1 vs Rest1RR1R213RP(y=1|x) vs P(y≈ 1|x)Classifier 2Class 2 vs RestR2RR2RR3RP(y=2|x) vs P(y≈ 2|x)Classifier 3Class 3 vs RestRR3R23RR3P(y=3|x) vs P(y≈ 3|x)

Final: ŷ = argmax[P(y=1|x), P(y=2|x), P(y=3|x)]

Softmax (Multinomial Logistic Regression)

For direct multi-class modeling, use the softmax function:

Softmax Function:

Properties:

  • (valid probability distribution)
  • Reduces to sigmoid when
  • Monotonic: higher logit → higher probability
  • Differentiable everywhere
Softmax: Converting Logits to ProbabilitiesLogits (z)z₁ = 2.0z₂ = 1.0z₃ = 0.1exp()eᶻe²·⁰ = 7.39e¹·⁰ = 2.72e⁰·¹ = 1.11P(y=k|x)P(y=1) = 0.66P(y=2) = 0.24P(y=3) = 0.10Distributionk=1k=2k=3

ŷ = argmax(P) = Class 1 (highest probability)

Cross-Entropy for Multi-class

Categorical Cross-Entropy Loss:

Where is 1 if sample belongs to class , else 0 (one-hot encoded).

Gradient for class k:


6. Regularization

L1 Regularization (Lasso)

L1 Penalty:

Effect: Drives some coefficients to exactly zero → feature selection

** Interpretation:** Diamond constraint region → corner solutions

L2 Regularization (Ridge)

L2 Penalty:

Effect: Shrinks all coefficients toward zero, none exactly zero

Geometric Interpretation: Circular constraint region → smooth solutions

Elastic Net

Combined Penalty:

Advantage: Handles correlated features better than L1 alone

Regularization: Constraint RegionsL1 (Lasso)SolutionCorner solutions → sparsityL2 (Ridge)SolutionSmooth shrinkage, no zerosElastic NetSolutionBest of both worlds

7. Complete Python Implementation

Binary Classification with Evaluation

Feature Importance and Odds Ratios

Decision Boundary Visualization

ROC Curve and AUC


8. Threshold Optimization

Default threshold = 0.5 is not always optimal!

Cost-Sensitive Threshold Selection:

Where = cost of false negative (missed disease), = cost of false alarm.


Key Takeaways

  1. Sigmoid function maps any real number to — enables probability interpretation
  2. Cross-entropy loss is convex → guaranteed global minimum with gradient descent
  3. Decision boundary is always linear in feature space (use polynomial features for non-linear)
  4. Coefficient interpretation: = odds ratio for one-unit increase in
  5. Multi-class: Use softmax for direct multiclass, or OvR for binary decomposition
  6. Regularization: L1 for feature selection, L2 for smooth shrinkage, Elastic Net for both
  7. Threshold tuning is critical — default 0.5 may not be optimal for imbalanced data

Practice Exercises

Exercise 1: Binary Classification Pipeline

Exercise 2: Multi-class Problem

Exercise 3: Cost-Sensitive Learning

  • Train a model with class_weight='balanced'
  • Compare confusion matrices with unweighted model
  • In which scenarios is the weighted model better?

Exercise 4: Polynomial Features

Discussion Questions

  1. When would you prioritize recall over precision (and vice versa)?
  2. Why might AUC be preferred over accuracy for imbalanced datasets?
  3. How does regularization affect logistic regression coefficients and interpretability?
  4. Under what assumptions does logistic regression fail?

Need Expert Data Science Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement