MACHINE LEARNING • LESSON 8

Logistic Regression

Logistic Regression is a Machine Learning algorithm used to predict the probability of a class and make classification decisions.

THE SIMPLEST DEFINITION

Logistic Regression predicts the probability of a class.

The model looks at the input features, calculates a probability, and then uses that probability to decide which class the input belongs to.

01

What Is Logistic Regression?

Logistic Regression is mainly used for classification problems.

Instead of predicting a continuous number such as a house price, it helps answer questions like:

Is this email spam? Yes / No
Is this transaction fraudulent? Fraud / Not Fraud
Will the customer leave? Leave / Stay
Is the patient at risk? Risk / No Risk
Logistic Regression is a classification algorithm, even though its name contains the word "Regression."
02

A Simple Example

Imagine that we want to predict whether a student will Pass or Fail an exam.

We can use the student's study hours as a feature.

Study Hours Result
1 hour Fail
2 hours Fail
5 hours Pass
7 hours Pass

The model learns the relationship between study hours and the known results.

INPUT Study Hours
MODEL Logistic Regression
OUTPUT Pass / Fail
03

Why Does It Predict a Probability?

Logistic Regression does not immediately say only "Pass" or "Fail."

It first produces a probability for the class we are interested in.

STUDY HOURS 2 hours Pass probability: 20%
STUDY HOURS 5 hours Pass probability: 72%
STUDY HOURS 7 hours Pass probability: 91%

The probability is between 0 and 1, or equivalently between 0% and 100%.

Logistic Regression gives us a probability first. The classification decision comes after that.
04

From Probability to Class

We need a rule to convert the predicted probability into a class.

A common default threshold is 0.5.

PROBABILITY Below 0.5 Class 0
0.5
PROBABILITY 0.5 or higher Class 1

For our Pass/Fail example, we can define:

Probability = 0.30 Below 0.5 → Fail
Probability = 0.80 0.5 or higher → Pass
The 0.5 threshold is common, but it is not a universal rule. The threshold can be changed depending on the problem.
05

Understanding the Classification Decision

Think of Logistic Regression as a two-step process.

STEP 1 Calculate Probability

Example: 0.82

STEP 2 Apply Threshold

0.82 ≥ 0.50

RESULT Class 1

Example: Pass

This is the key idea behind using Logistic Regression for binary classification.

06

The Logistic Function

Logistic Regression uses a mathematical function called the logistic function, also called the sigmoid function.

Its job is to convert the model's calculated score into a value between 0 and 1.

SIGMOID FUNCTION σ(z) = 1 / (1 + e−z)
VERY LOW SCORE Probability near 0
MIDDLE SCORE Probability near 0.5
VERY HIGH SCORE Probability near 1

You do not need to memorize the formula yet. The important idea is what the function does:

It takes a model score and turns it into a probability between 0 and 1.
07

Logistic Regression With Multiple Features

A real model usually uses more than one feature.

For example, to predict whether a customer will leave an e-commerce service, we might use:

FEATURE 1 Number of Purchases
FEATURE 2 Days Since Last Purchase
FEATURE 3 Customer Activity
FEATURES Customer Data
LOGISTIC REGRESSION Calculates Probability
PROBABILITY 0.78
CLASS Leave
08

Logistic Regression With Python

Scikit-learn provides Logistic Regression through LogisticRegression.

from sklearn.linear_model import LogisticRegression

X = [
    [1],
    [2],
    [3],
    [5],
    [6],
    [7]
]

y = [
    0,
    0,
    0,
    1,
    1,
    1
]

model = LogisticRegression()

model.fit(X, y)

prediction = model.predict([[6]])

print(prediction)

Here:

X Contains the input feature.
y Contains the class labels.
fit() Trains the Logistic Regression model.
predict() Predicts the class for new data.

In this example, the classes are represented as:

CLASS 0 Fail
CLASS 1 Pass
09

Prediction vs Probability

Logistic Regression can provide both the predicted class and the probability of the classes.

prediction = model.predict([[6]])

probability = model.predict_proba([[6]])

print(prediction)
print(probability)

The difference is important.

predict() Gives the class

Example: Class 1

predict_proba() Gives probabilities

Example: 0.12 / 0.88

predict() answers "Which class?" while predict_proba() answers "How probable is each class?"
REMEMBER THIS

Logistic Regression turns input features into a probability and then uses a threshold to make a classification decision.

For binary classification, the process can be understood as:

Features Logistic Regression Probability Class
QUICK CHECK

Check Your Understanding

Is Logistic Regression used for classification? Yes. It is commonly used for classification problems.
What does it produce first? A probability between 0 and 1.
What is a threshold? A rule used to convert a probability into a class.
What does predict() return? The predicted class.
NEXT TOPIC

Build a Classifier With Python

Next, we will build a complete classification model with Python and scikit-learn, step by step.