edSlash Labs  /  Machine Learning Lab  /  Supervised Learning  /  Logistic Regression
Algorithm 02 · Supervised learning · Classification

Logistic Regression

A model that estimates the chance that something belongs to a class. Despite its name, Logistic Regression is mainly used for classification, such as predicting pass or fail.

Beginner friendly

What does it do?

It predicts a probability between 0 and 1.

p = 1 / (1 + e−z)The sigmoid function turns a score into a probability

Imagine using study hours to estimate the chance that a student passes an exam. The model might predict 0.82, which means an estimated 82% chance of passing.

Quick facts

Input

One or more features, such as study hours.

Output

A probability, then a class such as pass or fail.

Learning style

Supervised: it learns from examples with known labels.

Decision boundary

A rule that separates the predicted classes.

1. How does it work?

Think of each dot as one student from an earlier exam.

From examples to a class

Green dots passed. Orange dots did not pass. The curve estimates the chance of passing as study hours increase.

Probability of passing0 hours10 hours00.51Study hours
PassedDid not passPredicted probability
The curve is S-shaped. It stays between 0 and 1, so it can be read as a probability.

The model follows three simple steps

Look at labelled examples

It studies inputs (X) and known answers (0 or 1).

Calculate a score

It combines the input values using weights and a bias.

Turn the score into a probability

The sigmoid function changes the score into a number from 0 to 1.

Choose a class

At a 0.5 threshold, 0.5 or above is class 1; below 0.5 is class 0.

Remember: Probability is the model's estimate. The threshold turns that estimate into a class label.

2. Live Logistic Regression Playground

Change the examples and threshold. Watch the probability curve and predictions update.

Interactive experiment

Study hours and exam result

Green = passed (1), orange = did not pass (0).

Passed (1)Did not pass (0)Probability curveDecision threshold

Experiment controls

Start with sample data or add a new student.

P(pass) = 1 / (1 + e−z)Sigmoid probability
Accuracy—Training examples
Threshold0.50Decision cut-off
Students0Data points
Predicted pass—At chosen threshold
Choose a dataset, then predict a student's result.
Generate a dataset to begin.

3. The mathematics behind Logistic Regression

We will build the formula one small step at a time.

Step 1: Make a score

z = wX + bw = weight, X = input, b = bias

The model first makes a score. The score can be any number, including a negative number.

Step 2: Use the sigmoid function

σ(z) = 1 / (1 + e−z)Output is always between 0 and 1

A large positive score gives a probability close to 1. A large negative score gives a probability close to 0.

Step 3: Choose a threshold

p ≥ 0.5 → class 1p < 0.5 → class 0

The common starting threshold is 0.5, but a real project may choose a different value.

Step 4: Learn from mistakes

Loss = −[y log(p) + (1−y) log(1−p)]Binary cross-entropy for one example

The training process adjusts weights to make the predicted probabilities closer to the known answers.

Important: Logistic Regression learns weights that help separate classes. The sigmoid function itself does not do the learning; it converts the model's score into a probability.

4. Numerical worked example

Let's make one prediction using simple numbers.

Example: Estimate whether a student passes

Suppose a trained model has weight w = 1 and bias b = −5. The student studies for 6 hours.

Step 1: Calculate the score
z = wX + b
z = (1 × 6) − 5 = 1
Step 2: Apply sigmoid
p = 1 / (1 + e−1)
p ≈ 0.731 = 73.1%
Step 3: Make a class decision
At a 0.5 threshold, 0.731 is above 0.5. The model predicts class 1: Pass.

5. How do we evaluate the model?

Accuracy is useful, but it is not the whole story.

Confusion matrix

It compares predicted classes with actual classes.

Predicted 0
Predicted 1
Actual 0
True negative
False positive
Actual 1
False negative
True positive

Common evaluation measures

Accuracy

Correct predictions divided by all predictions.

Precision

Of the items predicted positive, how many were truly positive?

Recall

Of all truly positive items, how many did the model find?

F1-score

A balance between precision and recall.

If classes are uneven—for example, 95% of transactions are normal—accuracy can look high even when the model misses many rare cases. Check precision, recall, and the confusion matrix too.

6. Python implementation

Train a Logistic Regression classifier using scikit-learn.

Example code

Study hours are the input; pass/fail is the label.

import numpy as np
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, confusion_matrix
from sklearn.metrics import classification_report

# X must be 2D: one row per student
X = np.array([[1], [2], [3], [4], [5], [6],
              [7], [8], [9], [10]])
y = np.array([0, 0, 0, 0, 1, 1, 1, 1, 1, 1])

# Split examples into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.3, random_state=42, stratify=y
)

# Create and train the classifier
model = LogisticRegression()
model.fit(X_train, y_train)

# Predict classes and probabilities
predictions = model.predict(X_test)
probabilities = model.predict_proba(X_test)[:, 1]

print("Accuracy:", accuracy_score(y_test, predictions))
print("Confusion matrix:\\n", confusion_matrix(y_test, predictions))
print(classification_report(y_test, predictions))

# Predict for a student who studied 6 hours
new_student = np.array([[6]])
print("Predicted class:", model.predict(new_student)[0])
print("Pass probability:", model.predict_proba(new_student)[0, 1])

7. Real-life applications

Places where predicting a category or event can be useful.

Email spam detection

Estimate whether an email is spam or not spam.

Loan default risk

Estimate whether a borrower may miss a payment. Real lending decisions need careful checks and fairness controls.

Medical screening

Estimate risk categories to support trained professionals, not replace diagnosis.

Customer churn

Estimate whether a customer may leave a service.

Use Logistic Regression when the target is a class, especially a yes/no outcome. It is a strong simple baseline, but it may not capture complicated patterns.

8. Knowledge check

Answer all 10 questions, then submit to see your score and explanations.

0 of 10 answered

Mini challenge

Generate the overlapping dataset. Change the threshold from 0.5 to 0.7. Notice how the number of predicted passes changes.

Can you explain why a higher threshold makes the model more careful before predicting class 1?