Linear vs Logistic Regression: A Clear Python Guide
Linear vs logistic regression is one of the first comparisons every machine learning learner meets, and both models are still used every day in finance, healthcare and marketing. Despite the similar names, they answer different questions: linear regression predicts a number, while logistic regression predicts the probability of a yes-or-no outcome. This guide explains both, shows how to train them in Python, and helps you choose the right one.
Linear vs logistic regression at a glance
| Linear regression | Logistic regression | |
|---|---|---|
| Target | Continuous number (price, CO₂ emissions, sales) | Binary class (churn / stay, fraud / genuine) |
| Output | A predicted value | A probability between 0 and 1, then a class |
| Shape | Straight line, plane or hyperplane | S-shaped sigmoid curve |
| Typical loss | Mean squared error | Log loss (cross-entropy) |
| scikit-learn class | LinearRegression |
LogisticRegression |
What is regression?
Regression is a supervised learning method that models the relationship between a continuous target and one or more explanatory features. A classic teaching example is predicting a car’s CO₂ emissions from its engine size, number of cylinders and fuel consumption. Once trained on past cars, the model can estimate emissions for a new car.
- Simple regression uses a single feature.
- Multiple regression uses two or more features.
- Either can be linear or non-linear.
Simple linear regression
Simple linear regression fits the best straight line through the data:
ŷ = θ₀ + θ₁x₁
Here θ₀ is the intercept and θ₁ the slope. “Best” usually means the line with the smallest mean squared error, the average of the squared gaps between predicted and actual values. This method is called ordinary least squares, and for a straight line it has an exact solution, so training is instant.

Multiple linear regression
Multiple linear regression adds more features: ŷ = θ₀ + θ₁x₁ + θ₂x₂ + … + θₙxₙ. It usually predicts better than a single feature and lets you compare how strongly each feature affects the target. Two cautions:
- Too many features can overfit, meaning the model memorises the training data and predicts poorly on new data.
- Correlated features (for example, engine size and number of cylinders) make individual coefficients unstable and harder to interpret.
Categorical features such as “manual or automatic” must be converted to numbers first, for example with one-hot encoding.
Polynomial and non-linear regression
Real data rarely follows a perfect straight line. When the trend curves, a straight line underfits. Two common fixes:
- Polynomial regression adds powers of a feature (x², x³…). It is still solved with linear regression, just on transformed features.
- Non-linear regression fits exponential, logarithmic or other curves, for example exponential growth in investments or the slowing growth of a learning curve.
Higher-degree polynomials can bend to fit every point, which is a fast route to overfitting. Keep the curve as simple as the data allows.
Logistic regression: predicting probabilities
Logistic regression takes the same linear score and passes it through a sigmoid function, which squeezes any number into the range 0 to 1. The result is a probability. Choose a threshold (often 0.5) and the model becomes a binary classifier.

Use logistic regression when:
- the target is binary (0 or 1);
- you need a probability, such as the chance a customer buys or a patient develops a condition;
- you want an interpretable model whose coefficients show which features matter most.
How logistic regression is trained
Training searches for the parameters θ that make the predicted probabilities match the real labels as closely as possible. The error is measured with log loss, which punishes confident wrong answers heavily. The loop is:
- Start with initial parameters.
- Predict a probability for every training example.
- Measure the log loss.
- Nudge the parameters in the direction that reduces the loss, using gradient descent.
- Repeat until the loss stops improving or a maximum number of iterations is reached.
Stochastic gradient descent updates the parameters using small random batches instead of the full dataset. It is faster on large data and can escape poor local solutions, at the cost of a noisier path.
Both models in Python
from sklearn.linear_model import LinearRegression, LogisticRegression
# Linear regression: predict a number
reg = LinearRegression().fit(X_train, y_price)
print(reg.coef_, reg.intercept_)
predicted_price = reg.predict(X_new)
# Logistic regression: predict a probability, then a class
clf = LogisticRegression(max_iter=1000).fit(X_train, y_churn)
churn_probability = clf.predict_proba(X_new)[:, 1]
churn_label = (churn_probability >= 0.5).astype(int)
For every option and solver, see the scikit-learn linear models documentation.
Linear vs logistic regression: which one should you use?
Ask what you are predicting. If the answer is “how much” or “how many”, start with linear regression. If it is “will it happen or not”, start with logistic regression. Both are fast, interpretable baselines; beat them before reaching for more complex models such as the ones in our guide to supervised learning algorithms.
Common mistakes with linear vs logistic regression
- Using linear regression for a yes/no target. It can predict values below 0 or above 1, which make no sense as probabilities. Use logistic regression instead.
- Keeping the default 0.5 threshold blindly. If missing a positive case is expensive, such as fraud or disease, lower the threshold and check precision and recall.
- Forgetting feature scaling. Regularised logistic regression converges faster and treats features fairly when they are on similar scales.
- Reading coefficients as causes. A large coefficient shows an association in your data, not cause and effect.
- Ignoring residual plots. For linear regression, a curved pattern in the residuals means you need polynomial features or a non-linear model.
The linear vs logistic regression decision is really about the target you predict; everything else follows from it. For the bigger picture, see our complete guide to machine learning with Python.
Practise with guided labs
Module 2 of IBM’s Machine Learning with Python course covers linear and logistic regression with hands-on Jupyter notebooks. See our course review or view it on Coursera.
FAQ
Is logistic regression a regression or a classification model?
Both, in a sense. Statistically it is a regression model for probabilities; in machine learning it is used as a classifier once you apply a threshold.
Can logistic regression handle more than two classes?
Yes. Multinomial (softmax) logistic regression and one-vs-rest strategies extend it to three or more classes, and scikit-learn supports both.
Why is my linear regression performing badly?
Common causes are a non-linear relationship, outliers, missing important features, or correlated features. Plot the data and the residuals before changing the model.