Despite its name, logistic regression is a classification algorithm. It borrows the linear model but passes its output through a function that turns it into a probability.
From a Line to a Probability
A linear score z = w₁x₁ + ... + b can take any real value, which is not a probability. The sigmoid (logistic) function σ(z) = 1 / (1 + e⁻ᶻ) squashes any z into the range between 0 and 1. It gives σ(0) = 0.5, and values near 1 for large positive z and near 0 for large negative z. The model estimates P(y = 1 | x) = σ(w·x + b). A decision rule then predicts class 1 when this probability is at least 0.5, which is the same as when z ≥ 0. The set of points where z = 0 is a straight line (or plane), the decision boundary, so logistic regression is a linear classifier.
Training by Log Loss
The weights are not found by least squares but by maximum likelihood, which means minimising the log loss (cross-entropy): for one instance the loss is −ln(p) if the true class is 1 and −ln(1 − p) if it is 0, where p is the predicted probability of class 1. A confident wrong prediction is punished very heavily. The loss is minimised by gradient descent, repeatedly moving the weights a small step against the gradient.
Extensions
For more than two classes, logistic regression is extended by one-versus-rest (one model per class) or by the softmax function, which gives a probability for every class. Regularisation adds a penalty on large weights to reduce overfitting.
Definition: Sigmoid function. The function σ(z) = 1 / (1 + e⁻ᶻ), which maps any real number to a value between 0 and 1 and is used by logistic regression to give a probability.
Table 3.2: Linear regression and logistic regression
| Aspect | Linear regression | Logistic regression |
|---|---|---|
| Task | Regression | Classification |
| Output | A real number | A probability from 0 to 1 |
| Loss | Squared error | Log loss (cross-entropy) |
| Fitting | Least squares | Maximum likelihood, gradient descent |
| Boundary | Not applicable | Linear decision boundary |
Example 3.2: Probability of passing
Problem: A model has z = 1.5 × hours − 4.5. Find the probability of passing for 2, 3 and 4 hours of study, and the log loss for the 4-hour student if the student actually passed.
Solution:
- For 2 hours, z = −1.5 and σ(−1.5) = 1 / (1 + e^1.5) = 0.182.
- For 3 hours, z = 0 and σ(0) = 0.5, so this is the decision boundary.
- For 4 hours, z = 1.5 and σ(1.5) = 1 / (1 + e^−1.5) = 0.818.
- The student with 4 hours passed (y = 1), so the loss is −ln(0.818) = 0.201.
Result: Probabilities 0.182, 0.5 and 0.818; the boundary is at 3 hours and the log loss is 0.201.
import math
from sklearn.linear_model import LogisticRegression
def sigmoid(z):
return 1 / (1 + math.exp(-z))
for h in (2, 3, 4):
z = 1.5 * h - 4.5
print("hours", h, "z", z, "p", round(sigmoid(z), 3))
print("log loss:", round(-math.log(sigmoid(1.5)), 3))
X = [[1], [2], [3], [4], [5], [6]]
y = [0, 0, 0, 1, 1, 1]
m = LogisticRegression().fit(X, y)
print("predict 2 and 5 hours:", m.predict([[2], [5]]).tolist())
print("p at 3.5 hours:", round(m.predict_proba([[3.5]])[0][1], 2))
Output
hours 2 z -1.5 p 0.182
hours 3 z 0.0 p 0.5
hours 4 z 1.5 p 0.818
log loss: 0.201
predict 2 and 5 hours: [0, 1]
p at 3.5 hours: 0.5
Common mistakes
- Treating logistic regression as a regression method because of its name.
- Reading the output as a certain class instead of a probability.
- Using least squares instead of log loss to train it.
- Assuming a linear boundary can separate any data.
Key takeaways
- P(y = 1) = σ(z) with σ(z) = 1 / (1 + e⁻ᶻ) and σ(0) = 0.5.
- Class 1 is predicted when z ≥ 0.
- Trained by minimising log loss with gradient descent.
- Its decision boundary is linear.
Practice questions
- What does the sigmoid function do?
- What is the decision boundary of logistic regression?
- Which loss does logistic regression minimise?
Answers to the practice questions
- It maps any real number to a value between 0 and 1.
- The points where z = 0, a line or plane.
- Log loss (cross-entropy).
Sources
- Official BCA syllabus Introduction to Machine Learning, course code 26BCA404DSE21, Unit III
