Search Knowledge

© 2026 LIBREUNI PROJECT

Machine Learning / Supervised Learning

Logistic Regression

Logistic Regression and Probability Estimation

Logistic Regression is a classifier that computes a weighted sum of inputs and maps the output to a probability between 00 and 11.

Probabilistic Formulation: The Sigmoid Function

The model feeds its linear output into the Sigmoid Function σ(t)\sigma(t):

p^=σ(θTx)=11+eθTx\hat{p} = \sigma(\theta^T \mathbf{x}) = \frac{1}{1 + e^{-\theta^T \mathbf{x}}}

The prediction rule for binary classes is:

y^={1if p^0.50if p^<0.5\hat{y} = \begin{cases} 1 & \text{if } \hat{p} \ge 0.5 \\ 0 & \text{if } \hat{p} < 0.5 \end{cases}

The prediction decision boundary is linear where θTx=0\theta^T \mathbf{x} = 0.

Code
import os
import numpy as np
import matplotlib.pyplot as plt
t = np.linspace(-6, 6, 200)
sig = 1 / (1 + np.exp(-t))
plt.figure(figsize=(5, 3.5))
plt.plot(t, sig, "b-", linewidth=2, label=r"$\sigma(t) = \frac{1}{1 + e^{-t}}$")
plt.axhline(y=0.5, color="gray", linestyle="--")
plt.axvline(x=0, color="gray", linestyle="--")
plt.title("Sigmoid Curve")
plt.grid(True, alpha=0.3)
plt.legend()
plt.tight_layout()
plt.savefig(os.environ["LIBREUNI_OUTPUT"], format="svg")
2026-07-12T18:15:57.407715 image/svg+xml Matplotlib v3.6.3, https://matplotlib.org/
The Sigmoid Function

Parameter Optimization: Log Loss

To optimize parameters θ\theta, we minimize the convex Log Loss (binary cross-entropy):

J(θ)=1mi=1m[y(i)log(p^(i))+(1y(i))log(1p^(i))]J(\theta) = -\frac{1}{m} \sum_{i=1}^{m} \left[ y^{(i)} \log(\hat{p}^{(i)}) + (1 - y^{(i)}) \log(1 - \hat{p}^{(i)}) \right]

It penalizes confident incorrect predictions with infinite cost (if y=1y=1 and p^0\hat{p} \to 0, then log(p^)-\log(\hat{p}) \to \infty).

Multiclass Classification Strategies

While Logistic Regression is inherently a binary classifier, it can be extended to handle multiple classes (e.g., classifying images of digits 0-9) using specific strategies:

  • One-vs-Rest (OVR) (also called One-vs-All): Trains NN separate binary classifiers for an NN-class problem. Each classifier is trained to distinguish one specific class from all the rest combined. During prediction, the classifier that outputs the highest probability wins. This is the default approach in most ML libraries for logistic regression.
  • One-vs-One (OVO): Trains a binary classifier for every possible pair of classes, resulting in N×(N1)2\frac{N \times (N-1)}{2} classifiers. For a new data point, all classifiers are run, and the class that wins the most duels is selected. OVO is particularly useful for algorithms that scale poorly with dataset size (like SVMs), because each classifier is only trained on the subset of data belonging to the two classes.

Alternatively, Softmax Regression (Multinomial Logistic Regression) generalizes Logistic Regression to natively support multiple classes by normalizing raw logits into a direct probability distribution across all classes without training multiple binary models.

Example: Logistic Regression Inference

The following example demonstrates calculating predictions using a trained Logistic Regression classifier:

python

Interactive Lab

Perform logistic regression inference. Calculate the predicted class and class probabilities for a sample point near the decision boundary.

Step 1
Inspect the idea
Step 2
Edit the program
Step 3
Run and compare

Exercise

Test your knowledge of the Logistic Regression model boundary behavior:

Why does the Log Loss cost function lack a closed-form analytical solution like the Normal Equation?

References & Further Reading

Previous Module Linear Regression