The names make them sound like close cousins, and they are related, but they answer different kinds of questions. Mixing them up is a common beginner mistake and a common interview question.
The one-line difference
- Linear regression predicts a number: "How many minutes will this delivery take?"
- Logistic regression predicts a probability of a category: "What is the chance this applicant defaults?"
If your target column is a quantity, use linear. If it is yes/no (or one of a few classes), use logistic.
How linear regression works
It fits the best straight line (or flat surface, with several inputs) through the data:
predicted_time = b0 + b1 × distance_km + b2 × items + ..."Best" means the line with the smallest total squared error between predictions and actual values. Each coefficient tells you how much the prediction changes when that input rises by one, holding the others fixed.
from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = LinearRegression().fit(X_train, y_train)
model.coef_, model.intercept_
model.score(X_test, y_test) # R² on unseen dataYou evaluate it with error measures such as MAE (average size of the mistakes, in the target's own units), RMSE (punishes big mistakes more) and R² (share of the variation explained).
Why not use linear regression for yes/no?
Code yes as 1 and no as 0 and fit a straight line, and the line happily predicts values like 1.4 or −0.3, which make no sense as probabilities. It is also badly affected by points far from the boundary.
How logistic regression works
Logistic regression takes the same weighted sum of inputs and passes it through the sigmoid function, which squashes any number into the range 0 to 1:
probability = 1 / (1 + e^-(b0 + b1 × income + b2 × existing_loans + ...))The output is a probability. To make a decision, you choose a threshold: above it, predict "default"; below it, predict "repay".
from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(max_iter=1000).fit(X_train, y_train)
proba = clf.predict_proba(X_test)[:, 1] # chance of class 1
pred = (proba >= 0.5).astype(int)The threshold is a business decision
0.5 is only a default. A lender who loses much more on a default than it earns on a good loan may reject anyone above a lower probability. A sales team with limited callers may call only the users above a higher one. Changing the threshold trades precision against recall, which is why accuracy alone is a poor way to judge a classifier. Our article on the confusion matrix, precision and recall explains this trade-off.
Reading the coefficients
- Linear: a coefficient of 2.5 on
distance_kmmeans each extra km adds about 2.5 minutes to the prediction, other inputs fixed. - Logistic: coefficients act on the log-odds. A positive coefficient raises the probability, a negative one lowers it. Exponentiating a coefficient gives an odds ratio.
In both, compare coefficient sizes only after scaling the inputs to similar ranges.
Quick comparison
| Linear regression | Logistic regression | |
|---|---|---|
| Target | A number | A category (usually yes/no) |
| Output | Any real number | Probability from 0 to 1 |
| Fitting idea | Minimise squared error | Maximise likelihood (log-loss) |
| Evaluate with | MAE, RMSE, R² | Confusion matrix, precision, recall, ROC-AUC |
| Example | Delivery time, house price | Loan default, churn, spam |
Try both
The fastest way to build intuition is to play: drag points in the linear regression playground and watch the line move, then work through the logistic regression lesson and its practice set, where you tune the threshold for a sales team.
Practise what you just read
Reading helps; writing the code yourself is what sticks. Start here: