intermediatemachine-learning

Logistic Regression

A linear model for yes/no questions. It gives you probabilities, not just guesses.

Key Points

What it is

Logistic regression is a classification algorithm, despite the confusing name. It predicts the probability that something belongs to a category (like 'spam' or 'not spam'). It works by taking a linear combination of inputs (like linear regression) and then squashing that number through a special S-shaped curve called the sigmoid function. The output is always between 0 and 1, perfect for probabilities. You then decide a threshold (usually 0.5) to turn that probability into a yes/no prediction.

Why it exists

Linear regression would give you numbers like -5 or 10 for probabilities, which makes no sense. You need a model that stays between 0 and 1. Logistic regression was invented to do exactly that. It is the simplest and most interpretable classification model. It exists because many real problems are binary yes/no: will this customer buy? Is this tumor malignant? Will this email be spam? And you often want a probability, not just a label.

What it measures

The coefficients measure the effect of each input on the log-odds of the positive class. If the coefficient is positive, higher values of that feature increase the probability of being in class 1. The magnitude tells you the strength. The model's performance is measured with accuracy, precision, recall, ROC curve, and log-loss (cross-entropy). The predicted probabilities should be well-calibrated – a predicted 0.7 should mean 70% chance in reality.

Intuition

Imagine you want to predict if a student will pass an exam based on hours studied. You collect data: many points where hours studied is on the x-axis and pass/fail (0 or 1) on the y-axis. Linear regression would draw a straight line through those 0s and 1s, giving values below 0 and above 1 – nonsense. Logistic regression fits an S-curve that stays between 0 and 1. At 0 hours, it might say 10% chance of passing. At 20 hours, 95% chance. The curve is smooth and interpretable.

Formulas

Sigmoid function

p=11+e−(β0+β1x1+… )p = \frac{1}{1 + e^{-(\beta_0 + \beta_1 x_1 + \dots)}}
  • p — predicted probability that class = 1
  • β₀ + β₁x₁ + ... — same linear combination as linear regression
  • e^{-z} — exponential of negative z. When z is large positive, p is near 1. When z is large negative, p near 0.

Log-odds (logit)

log⁡(p1−p)=β0+β1x1+…\log\left(\frac{p}{1-p}\right) = \beta_0 + \beta_1 x_1 + \dots
  • p/(1-p) — odds of being in class 1 (e.g., 3:1 odds means p=0.75)
  • log(...) — natural log – makes the linear relationship work

When to use

When not to use

Common mistakes

In practice

In scikit-learn: `from sklearn.linear_model import LogisticRegression`. Use `model = LogisticRegression()`. Fit with `model.fit(X_train, y_train)`. Get probabilities with `model.predict_proba(X_test)` – that gives [prob_class0, prob_class1]. Get hard predictions with `model.predict(X_test)`. Use `penalty='l1'` or `'l2'` for regularization. Always scale features if using regularization. For imbalanced data, use `class_weight='balanced'`.

Was this concept helpful?

Spot an error or gap? Suggest an edit →

Related concepts

linear-regressionconfusion-matrixroc-auclog-loss
Chirp at me!