Linear & Logistic Regression Explained with Examples
Linear and logistic regression are the "hello world" algorithms of machine learning. This beginner's guide explains both simply — with a clear diagram, real examples, Python code, and when to use which.
Every machine learning journey has a "hello world" moment — and it usually starts with these two algorithms: linear regression and logistic regression. They're simple, they're everywhere, and they teach you how ML actually works.
These were the first two algorithms I genuinely understood — and realizing that "logistic regression" is really about yes/no, not numbers, was a small lightbulb moment. Let's get you there faster than I did.
This is Lesson 9 of the GyaanPost Learn AI roadmap. Both are supervised learning — so a quick look at Lesson 8 helps if you need it.
See the difference at a glance
Linear regression — predicting a number
Linear regression draws the best straight line through your data to predict a continuous number. Give it the size of a house, and it predicts the price. Give it study hours, it predicts the exam score.
You might remember the line equation from school:
y = mx + b — where y is what you predict, x is your input, m is the slope, and b is where the line starts.
The algorithm's whole job is to find the m and b that make the line fit the data as closely as possible. Then, for a new x, it reads off the predicted y.
Linear regression in Python
from sklearn.linear_model import LinearRegression
# House size (sq ft) -> price ($1000s)
X = [[1000], [1500], [1200], [1800]]
y = [200, 300, 240, 360]
model = LinearRegression()
model.fit(X, y) # find the best-fit line
print(model.predict([[1600]])) # predict price for a 1600 sq ft house
Logistic regression — predicting yes or no
Here's the twist that confuses everyone: despite the name "regression," logistic regression is used for classification — predicting a category, usually yes/no.
Instead of a straight line, it fits an S-shaped curve that outputs a probability between 0 and 1. If the probability is above 0.5, it predicts "yes"; below, "no."
- Is this email spam or not? (yes/no)
- Will this customer churn or stay?
- Is this transaction fraud or legitimate?
Logistic regression in Python
from sklearn.linear_model import LogisticRegression
# Hours studied -> passed exam? (0 = no, 1 = yes)
X = [[1], [2], [3], [6], [7], [8]]
y = [0, 0, 0, 1, 1, 1]
model = LogisticRegression()
model.fit(X, y)
print(model.predict([[5]])) # will a 5-hour student pass?
Why is it called "regression" then?
Fair question. The name is historical — it comes from the math it's built on (a "logistic function"). Just remember the practical rule: linear regression → numbers; logistic regression → categories.
Linear vs logistic: side by side
| Linear Regression | Logistic Regression | |
|---|---|---|
| Predicts | A number (continuous) | A category (usually yes/no) |
| Output | Any value (e.g., 240,000) | A probability 0–1 → class |
| Shape | Straight line | S-shaped curve |
| Task type | Regression | Classification |
| Example | Predict house price | Predict spam / not spam |
When to use which
- Predicting a number (price, temperature, sales)? → Linear regression.
- Predicting a category (yes/no, spam/not, pass/fail)? → Logistic regression.
Ask yourself one question: "Am I predicting a number or a class?" That answer picks the algorithm.
Common mistakes
- Thinking logistic regression predicts numbers. It predicts categories — the name is misleading.
- Using linear regression for yes/no problems. It doesn't handle categories well; use logistic.
- Expecting a perfect fit. Real data is messy — the line/curve captures the trend, not every point.
Key takeaways
- Linear regression predicts a number using a best-fit straight line (y = mx + b).
- Logistic regression predicts a category (yes/no) using an S-shaped curve that outputs a probability.
- Both are supervised learning and just a few lines of code in scikit-learn.
- Ask "number or class?" to choose between them.
- These two are the foundation — most other algorithms build on the same ideas.
👉 Next lesson: Classification Algorithms: KNN, Decision Tree, Random Forest & SVM →
📬 Want each new AI lesson in your inbox? Subscribe to the GyaanPost newsletter and follow the roadmap from beginner to advanced.
Frequently Asked Questions
What is the difference between linear and logistic regression?
Linear regression predicts a continuous number, like a house price, using a best-fit straight line. Logistic regression predicts a category, usually yes or no, using an S-shaped curve that outputs a probability between 0 and 1.
Is logistic regression used for classification or regression?
Despite its name, logistic regression is used for classification. It predicts categories such as spam or not spam by outputting a probability and then choosing a class based on whether that probability is above or below 0.5.
What does the equation y = mx + b mean in linear regression?
It is the equation of a straight line. Here y is the value you predict, x is your input, m is the slope, and b is where the line starts. The algorithm finds the m and b that best fit your data.
When should I use linear regression vs logistic regression?
Use linear regression when you are predicting a number, such as price, temperature, or sales. Use logistic regression when you are predicting a category, such as yes or no, spam or not spam, or pass or fail.
Why is logistic regression called regression if it does classification?
The name is historical and comes from the logistic function it is built on. In practice, just remember the rule: linear regression predicts numbers, and logistic regression predicts categories.