Purchase Prediction

Purchase Prediction with Logistic Regression
Binary classification pipeline using logistic regression to predict customer purchases from demographic and behavioral features — practical, production-minded, and business-driven.
Project Overview
This project builds a logistic regression model to predict whether a customer will make a purchase (binary outcome) using features such as age, gender, marital status, income, occupation, and other behavioral attributes. The focus is on rigorous data preparation, informed feature selection, model training, evaluation, and visualization to drive business decisions.
Key Features
- Data Preprocessing: cleaning, handling missing values, encoding categorical variables, and scaling as appropriate for logistic regression.
- Feature Selection: identification of high-signal features (age, marital status, income, occupation) to reduce noise and improve model interpretability.
- Logistic Regression Model: training and evaluation of a logistic regression classifier with standard metrics (accuracy, precision, recall, F1, ROC-AUC).
- Evaluation & Validation: K-fold cross-validation and a hold-out test set to measure generalization performance and avoid overfitting.
- Visualization: exploratory visualizations and model performance plots (ROC curve, confusion matrix) for stakeholder reporting.
Technologies Used
- Python — orchestration and preprocessing
- scikit-learn — logistic regression, metrics, model selection
- pandas — data wrangling
- Matplotlib & Seaborn — EDA and model visualization
How to run
- Clone the repo and open the notebook/script in Jupyter or run the pipeline as a Python script.
- Install dependencies:
pip install -r requirements.txt(includes scikit-learn, pandas, matplotlib, seaborn). - Run preprocessing → feature selection → model training cells in order. Export model artifacts and visualizations as needed.
Project Overview
Customers who left within the last month - the column is called Churn Services that each customer has signed up for - phone, multiple lines, internet, online security, online backup, device protection, tech support, and streaming TV and movies Customer account information - how long they've been a customer, contract, payment method, paperless billing, monthly charges, and total charges Demographic info about customers - gender, age range, and if they have partners and dependents Inspiration To explore this type of models and learn more about the subject.
Code Snippet
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.preprocessing import StandardScaler
from sklearn.cluster import KMeans
from sklearn.decomposition import PCA
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import LogisticRegression
sns.set()
# Load the dataset
purch=pd.read_csv('../input/purchase/purchase data.csv')
purch.sample(6)
# Preprocessing
purch_analy=purch[['ID','Incidence']].groupby('ID').count().rename(columns={"Incidence":"Number of visit"})
purch_incidence=purch[purch["Incidence"]==1].groupby("ID").sum()
purch_analy["Number of Purchase"]=purch_incidence["Incidence"]
purch_analy["Average Purchase"]=(purch_analy["Number of Purchase"]/purch_analy["Number of visit"]).round(3)
purch_analy.head().sort_values(['Number of visit','Number of Purchase','Average Purchase'])
purch_seg=purch[['ID',"segment"]].groupby('ID').mean().round()
purch_analy['segment']=purch_seg['segment']
purch_analy
# Model
purc_quant=purch[purch["Incidence"]==1]
purc_quant=pd.get_dummies(purc_quant,columns=['Brand'],prefix='Brand',prefix_sep='_')
purc_quant['Price_Incidence']=(purc_quant['Brand_1']*purc_quant["Price_1"]+
purc_quant['Brand_2']*purc_quant["Price_2"]+
purc_quant['Brand_3']*purc_quant["Price_3"]+
purc_quant['Brand_4']*purc_quant["Price_4"]+
purc_quant['Brand_5']*purc_quant["Price_5"])
purc_quant['Promotion_Incidence']=(purc_quant['Brand_1']*purc_quant["Promotion_1"]+
purc_quant['Brand_2']*purc_quant["Promotion_2"]+
purc_quant['Brand_3']*purc_quant["Promotion_3"]+
purc_quant['Brand_4']*purc_quant["Promotion_4"]+
purc_quant['Brand_5']*purc_quant["Promotion_5"])
# Make predictions
x= purc_quant[['Price_Incidence','Promotion_Incidence']]
y=purc_quant["Quantity"]
model_quantity=LinearRegression()
model_quantity.fit(x,y)
model_quantity.coef_



















