Supervised LearningCore· 40 min read

Logistic Regression: Predict a Category

When the answer is a yes/no or a class, logistic regression predicts which one — and how sure it is.

What you will learn

  • Know when to use classification
  • Train LogisticRegression with scikit-learn
  • Read prediction probabilities

When the answer is a category

Many questions have a category as the answer: spam or not spam? pass or fail? cat or dog? Predicting a category is called classification.

Despite its confusing name, logistic regression is a classification tool. Instead of a number, it outputs a probability between 0 and 1, then picks the more likely class.

A worked example: will a student pass?

We will predict pass (1) or fail (0) from hours studied. This is the same data idea as before, but now the answer is a category.

Classifying pass vs fail from study hours
from sklearn.linear_model import LogisticRegression

X = [[1],[2],[3],[4],[5],[6],[7],[8]]   # hours studied
y = [ 0,  0,  0,  0,  1,  1,  1,  1]     # 0 = fail, 1 = pass

model = LogisticRegression()
model.fit(X, y)

print('Studied 2h ->', model.predict([[2]])[0])
print('Studied 7h ->', model.predict([[7]])[0])

Note: Output: Studied 2h -> 0 Studied 7h -> 1 The model predicts fail (0) for 2 hours and pass (1) for 7 hours. It learned roughly “study more than about 4 hours and you pass”, just like the pattern you could see by eye.

How sure is it?

A big advantage of logistic regression is that it tells you its confidence with predict_proba.

Asking the model how confident it is for a 5-hour student
probs = model.predict_proba([[5]])[0]
print('Fail chance:', round(probs[0], 2))
print('Pass chance:', round(probs[1], 2))

Note: Output: Fail chance: 0.36 Pass chance: 0.64 For 5 hours the model is 64% sure the student passes, so it predicts “pass”. Near the boundary it is less certain — handy when you want to flag “unsure” cases.

Use this when…ModelAnswer looks like
The answer is a numberLinear regression180, 45.5, 1200
The answer is a categoryLogistic regressionspam / not spam, pass / fail

Tip: Logistic regression handles more than two classes too (e.g. cat / dog / bird). scikit-learn does this automatically — the same .fit() and .predict() still work.

Q. What does logistic regression predict?

Answer: Despite the name, logistic regression is for classification: it predicts a category and can give the probability for each class.

✍️ Practice

  1. Print the pass/fail prediction and the probabilities for a student who studied 4 hours.
  2. Explain why logistic regression suits “spam vs not spam” but linear regression does not.

🏠 Homework

  1. Build a logistic regression that predicts “buy (1)” or “not buy (0)” from a product’s price. Invent 6 rows and predict two new prices.
Want to learn this with a mentor?

CodingClave runs guided, project-based training (28-day, 45-day & 6-month batches).

Explore Training →