Lesson 2 of 10 · 4 min
Logistic Regression
WHY Not Linear Regression for Classification?
Linear Regression outputs any number from −∞ to +∞. But probability must be between 0 and 1.
If we tried linear regression:
- Input: 10 hours study → output: 1.3 (impossible! probability can’t exceed 1)
- Input: 0 hours study → output: −0.2 (impossible! probability can’t be negative)
Solution: The sigmoid function squishes any number into 0–1 range.
The Sigmoid Function
σ(z) = 1 / (1 + e^⁻ᵣ)
| z value | σ(z) output | Meaning |
|---|---|---|
| Very negative (−10) | ≈0 | Very unlikely class 1 |
| 0 | 0.5 | Exactly on the boundary |
| Very positive (+10) | ≈1 | Very likely class 1 |
Analogy: Think of a dimmer switch. Linear regression is an on/off switch (0 or 1, harsh jump). Sigmoid is a dimmer — it smoothly transitions from 0 to 1 as the input increases.
Worked Numerical Example — Full Prediction Pipeline
Setup: Predict if student passes (1=Pass, 0=Fail) based on hours studied.
Learned from training: w₁ = 0.8, bias b = −3.0
Student A: studied 5 hours (x₁ = 5)
Step 1 — Weighted sum: z = w₁×x₁ + b = 0.8×5 + (−3.0) = 4.0 − 3.0 = 1.0
Step 2 — Sigmoid: σ(1.0) = 1 / (1 + e⁻¹·⁰) = 1 / (1 + 0.368) = 0.731
Step 3 — Decision (threshold = 0.5): 0.731 ≥ 0.5 → predict class 1 (Pass) with 73.1% confidence
Student B: studied 1 hour (x₁ = 1)
z = 0.8×1 + (−3.0) = −2.2
σ(−2.2) = 1 / (1 + e²·²) = 1 / (1 + 9.025) = 0.099
0.099 < 0.5 → predict class 0 (Fail) with only 9.9% confidence
Pattern: More hours studied → higher z → sigmoid closer to 1 → model more confident of Pass.
Decision Boundary
Default threshold = 0.5
- If σ(z) ≥ 0.5 → predict class 1 (positive)
- If σ(z) < 0.5 → predict class 0 (negative)
You can change the threshold depending on the problem (e.g., lower it to 0.3 for cancer detection to catch more cases).
Code — Binary Logistic Regression
from sklearn.linear_model import LogisticRegression
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
# Load breast cancer dataset: 569 samples, 30 features, 2 classes
data = load_breast_cancer()
X = data.data # 30 measurement features (radius, texture, area etc.)
y = data.target # 0 = malignant, 1 = benign
# Split: 80% train, 20% test
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)
# Scale features (Logistic Regression converges faster with scaled data)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train) # fit on train, transform train
X_test = scaler.transform(X_test) # transform test using SAME scaler
# Train model
model = LogisticRegression(random_state=42)
model.fit(X_train, y_train)
# Predict class labels (0 or 1)
y_pred = model.predict(X_test)
# Predict probabilities (probability of being class 1)
y_prob = model.predict_proba(X_test)[:, 1] # [:, 1] = column for class 1
print("First 5 predictions:", y_pred[:5])
print("First 5 probabilities:", y_prob[:5].round(3))
# Output:
# First 5 predictions: [0 1 1 1 0]
# First 5 probabilities: [0.048 0.975 0.982 0.961 0.043]Common Errors — Logistic Regression
| Error | Cause | Fix |
|---|---|---|
ConvergenceWarning | Not enough iterations | Add max_iter=1000 |
| Probabilities not shown | Using predict() instead of predict_proba() | Use model.predict_proba(X)[:, 1] |
| Fitting scaler on test data | Data leakage | fit_transform on train, transform only on test |
| Forgetting to scale | Slow or wrong convergence | Always StandardScaler before Logistic Regression |
