Logistic Regression in Python – Case Study
Logistic Regression is one of the most widely used machine learning algorithms for solving classification problems. It is simple, fast, and highly interpretable, which makes it a great starting point for beginners in data science and Python machine learning.
In this real-world case study, we will build a complete Logistic Regression project in Python using Scikit-Learn. The goal is to predict whether a customer will purchase a product based on their age and estimated salary.
This type of problem is widely used in marketing analytics, customer targeting, and business decision-making.
By the end of this tutorial, you will clearly understand how to:
- Define a real-world machine learning problem
- Load and explore a dataset
- Prepare and preprocess data
- Train a Logistic Regression model
- Make predictions and probabilities
- Evaluate model performance
- Extract business insights from predictions
Business Problem: Customer Purchase Prediction
A company wants to improve its marketing strategy for a new product.
Instead of showing advertisements to all customers, the company wants to target only users who are more likely to buy the product.
Available customer data:
- Age
- Estimated Salary
Target variable:
- Purchased (1 = Yes, 0 = No)
Objective:
Build a Logistic Regression model that predicts whether a customer will purchase the product.
This helps businesses:
- Reduce advertising cost
- Improve conversion rate
- Target the right audience
Understanding the Dataset
A sample dataset looks like this:
| Age | Estimated Salary | Purchased |
|---|---|---|
| 19 | 19000 | 0 |
| 35 | 20000 | 0 |
| 45 | 80000 | 1 |
| 50 | 90000 | 1 |
| 30 | 30000 | 0 |
Features:
- Age
- EstimatedSalary
Target:
- Purchased
Import Required Python Libraries
We start by importing the necessary libraries for machine learning:
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report
Load and Inspect the Dataset
data = pd.read_csv("customers.csv")
print(data.head())
Example Output:
Age EstimatedSalary Purchased
0 19 19000 0
1 35 20000 0
2 45 80000 1
3 50 90000 1
Exploratory Data Analysis (EDA)
Before training the model, we check data quality:
print(data.info())
print(data.describe())
Why this step is important:
- Detect missing values
- Understand data distribution
- Identify outliers
- Verify data types
Define Features and Target Variable
X = data[['Age', 'EstimatedSalary']]
y = data['Purchased']
- X = input features
- y = target output
Split Data into Training and Testing Sets
We divide data to evaluate model performance properly:
X_train, X_test, y_train, y_test = train_test_split(
X,
y,
test_size=0.25,
random_state=42
)
Why splitting is important:
- Prevents overfitting
- Ensures fair evaluation
- Simulates real-world predictions
Feature Scaling for Better Performance
Logistic Regression is sensitive to feature scale differences.
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
Example:
Before scaling:
- Age = 45
- Salary = 80000
After scaling:
- Age = 0.85
- Salary = 1.24
Train the Logistic Regression Model
model = LogisticRegression()
model.fit(X_train, y_train)
What happens here:
The model learns patterns between:
- Age → Purchase behavior
- Salary → Purchase behavior
Make Predictions
predictions = model.predict(X_test)
print(predictions)
Output:
[0 1 1 0 1]
- 0 → Not Purchased
- 1 → Purchased
Predict Purchase Probability
One advantage of Logistic Regression is probability output:
probabilities = model.predict_proba(X_test)
print(probabilities[:5])
Example:
[[0.82 0.18]
[0.21 0.79]
[0.10 0.90]]
Interpretation:
- Higher second value = higher chance of purchase
- Useful for marketing decision-making
Model Evaluation Metrics
1. Accuracy Score
accuracy = accuracy_score(y_test, predictions)
print("Accuracy:", accuracy)
2. Confusion Matrix
print(confusion_matrix(y_test, predictions))
Example:
[[60 5]
[ 6 29]]
Meaning:
- True Negatives: 60
- False Positives: 5
- False Negatives: 6
- True Positives: 29
3. Classification Report
print(classification_report(y_test, predictions))
Includes:
- Precision
- Recall
- F1-score
Visualization of Customer Data
import matplotlib.pyplot as plt
plt.scatter(
data['Age'],
data['EstimatedSalary'],
c=data['Purchased']
)
plt.xlabel("Age")
plt.ylabel("Estimated Salary")
plt.title("Customer Purchase Behavior")
plt.show()
Complete End-to-End Code
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, confusion_matrix
data = pd.read_csv("customers.csv")
X = data[['Age', 'EstimatedSalary']]
y = data['Purchased']
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=42
)
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)
model = LogisticRegression()
model.fit(X_train, y_train)
predictions = model.predict(X_test)
print("Accuracy:", accuracy_score(y_test, predictions))
print(confusion_matrix(y_test, predictions))
Business Insights from the Model
This model helps businesses to:
- Identify potential buyers before advertising
- Reduce unnecessary marketing costs
- Improve targeting strategies
- Increase sales conversion rates
- Understand customer behavior patterns
Why Logistic Regression Works Well
Logistic Regression is effective because:
- It is simple and fast
- It works well for binary classification
- It provides probability outputs
- It is easy to interpret
- It performs well on structured data
Real-World Applications
This same approach is used in:
- Customer churn prediction
- Credit risk analysis
- Fraud detection systems
- Medical diagnosis models
- Marketing campaign targeting
Common Challenges in Real Projects
Real datasets often include:
- Missing values
- Outliers
- Imbalanced classes
- Noisy data
- Irrelevant features
Proper preprocessing improves model accuracy significantly.
Conclusion
This case study demonstrated how Logistic Regression can be applied to a real-world customer purchase prediction problem using Python.
By following a complete machine learning pipeline—data loading, preprocessing, feature scaling, training, and evaluation—you can build a reliable classification model for business decision-making.
Logistic Regression remains one of the most important foundational algorithms in machine learning and is widely used in real-world data science applications such as marketing, finance, and healthcare.


0 Comments