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.
What does it do?
It predicts a probability between 0 and 1.
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
One or more features, such as study hours.
A probability, then a class such as pass or fail.
Supervised: it learns from examples with known labels.
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.
The model follows three simple steps
It studies inputs (X) and known answers (0 or 1).
It combines the input values using weights and a bias.
The sigmoid function changes the score into a number from 0 to 1.
At a 0.5 threshold, 0.5 or above is class 1; below 0.5 is class 0.
2. Live Logistic Regression Playground
Change the examples and threshold. Watch the probability curve and predictions update.
Study hours and exam result
Green = passed (1), orange = did not pass (0).
Experiment controls
Start with sample data or add a new student.
3. The mathematics behind Logistic Regression
We will build the formula one small step at a time.
Step 1: Make a score
The model first makes a score. The score can be any number, including a negative number.
Step 2: Use the sigmoid function
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
The common starting threshold is 0.5, but a real project may choose a different value.
Step 4: Learn from mistakes
The training process adjusts weights to make the predicted probabilities closer to the known answers.
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.
z = wX + b
z = (1 × 6) − 5 = 1
p = 1 / (1 + e−1)
p ≈ 0.731 = 73.1%
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.
Common evaluation measures
Correct predictions divided by all predictions.
Of the items predicted positive, how many were truly positive?
Of all truly positive items, how many did the model find?
A balance between precision and recall.
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.
8. Knowledge check
Answer all 10 questions, then submit to see your score and explanations.
Mini challenge
Generate the overlapping dataset. Change the threshold from 0.5 to 0.7. Notice how the number of predicted passes changes.