cd /news/machine-learning/customer-purchase-prediction-with-lo… · home › topics › machine-learning › article
[ARTICLE · art-151490] src=dev.to ↗ pub= topic=machine-learning verified=true sentiment=↑ positive

Customer Purchase Prediction with Logistic Regression - A Beginner's End-to-End ML Project

A developer built an end-to-end logistic regression project that predicts customer purchase decisions from age, salary, experience, and gender, using an eight-row dataset and scikit-learn's train-test split, StandardScaler, and LogisticRegression pipeline. The model achieved 1.0 accuracy on the two-customer test set and predicted a new customer profile (age 36, salary 950, 6 years experience, female) as a buyer. The writeup emphasizes fitting the scaler only on training data and transforming the test and new-customer inputs with the same scaler.

by read3 min views1 publishedOct 16, 2026

When I finished my first machine learning lectures, I wanted a project that used everything I had learned: data , preprocessing, train-test split, scaling, training, and evaluation. So I built a small but complete project: predicting whether a customer will purchase a product, based on their age, salary, experience, and gender.

The dataset is tiny (8 customers), which makes it perfect for learning — you can follow every number by hand.

Age Salary Experience Gender Purchase
25 500 2 Male 0
32 800 5 Female 1
45 1200 15 Male 1
22 400 1 Female 0
38 950 8 Male 1
28 600 3 Female 0
50 1500 20 Male 1
30 700 4 Female 1

Purchase is the target: 1 means the customer bought, 0 means they didn't. The rest are the features.

Models work with numbers, so the Gender column needs encoding: Male = 0, Female = 1.

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

data = {
    'Age': [25, 32, 45, 22, 38, 28, 50, 30],
    'Salary': [500, 800, 1200, 400, 950, 600, 1500, 700],
    'Experience': [2, 5, 15, 1, 8, 3, 20, 4],
    'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Female', 'Male', 'Female'],
    'Purchase': [0, 1, 1, 0, 1, 0, 1, 1]
}
df = pd.DataFrame(data)
df['Gender'] = df['Gender'].map({'Male': 0, 'Female': 1})
X = df[['Age', 'Salary', 'Experience', 'Gender']]
y = df['Purchase']

X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.2, random_state=42
)

test_size=0.2 means 20% of the data goes to testing. random_state=42 makes the split reproducible.

Age ranges up to 50, Salary up to 1500. Without scaling, the bigger numbers dominate the model. StandardScaler fixes that — and the key rule is: fit only on the training data, then transform both sets.

scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.transform(X_test)  # never fit_transform on test data!

Since the target is yes/no, this is a classification problem, and logistic regression is the natural first model:

model = LogisticRegression()
model.fit(X_train, y_train)

y_pred = model.predict(X_test)
print("Actual:   ", list(y_test))   # [0, 1]
print("Predicted:", list(y_pred))   # [0, 1]
print("Accuracy: ", accuracy_score(y_test, y_pred))  # 1.0

The model predicted both test customers correctly: accuracy 1.0.

Now the fun part — a brand-new customer: age 36, salary 950, 6 years of experience, female. The new data must go through the same scaler (transform, not fit):

new_customer = [[36, 950, 6, 1]]
new_customer_scaled = scaler.transform(new_customer)
prediction = model.predict(new_customer_scaled)
print("Approved" if prediction[0] == 1 else "Not Approved")  # Approved

Result: Approved (Yes) — the model predicts this customer will purchase.

If you are learning ML, build this project yourself and change one thing at a time: try a different random_state, add a feature, or compare logistic regression with a decision tree. That is where the real learning happens.

── more in #machine-learning 4 stories · sorted by recency
── more on @scikit-learn 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
→ Live at https://your-agent.zahid.host ✓
Get free account → Pricing
from €0/mo · no card required
LIVE [news/customer-purchase-pr…] indexed:0 read:3min 2026-10-16 · —