Lesson 27
Logistic Regression
Turn a weighted sum of inputs into a probability for a class, then pick the class when that probability crosses a threshold.
Intermediate12 min
What You Will Learn
- Idea
- Example
- When to use it
Idea
Logistic regression still learns a weight per input. It passes the weighted sum through a sigmoid so the output sits between 0 and 1. That number is the estimated probability of the positive class. A threshold, usually 0.5, turns it into a label.
Example
import numpy as np
from sklearn.linear_model import LogisticRegression
X = np.array([[1], [2], [3], [7], [8], [9]], dtype=float)
y = np.array([0, 0, 0, 1, 1, 1])
prob = LogisticRegression().fit(X, y).predict_proba([[5]])[0, 1]
print(f"P(class 1 | 5): {prob:.2f}")Output:
P(class 1 | 5): 0.50When to use it
- Use it when you need a probability and the class boundary is roughly linear.
- Scale the inputs so one large column does not dominate the weights.
- For many classes, the same idea becomes one weight vector per class.
Loading playground…