{"slug": "customer-purchase-prediction-with-logistic-regression-a-beginner-s-end-to-end-ml", "title": "Customer Purchase Prediction with Logistic Regression - A Beginner's End-to-End ML Project", "summary": "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.", "body_md": "When I finished my first machine learning lectures, I wanted a project that used *everything* I had learned: data loading, 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.\n\nThe dataset is tiny (8 customers), which makes it perfect for learning — you can follow every number by hand.\n\n| Age | Salary | Experience | Gender | Purchase | \n|---|---|---|---|---|\n| 25 | 500 | 2 | Male | 0 | \n| 32 | 800 | 5 | Female | 1 | \n| 45 | 1200 | 15 | Male | 1 | \n| 22 | 400 | 1 | Female | 0 | \n| 38 | 950 | 8 | Male | 1 | \n| 28 | 600 | 3 | Female | 0 | \n| 50 | 1500 | 20 | Male | 1 | \n| 30 | 700 | 4 | Female | 1 | \n\n`Purchase` is the target: `1` means the customer bought, `0` means they didn't. The rest are the features.\n\nModels work with numbers, so the `Gender` column needs encoding: Male = 0, Female = 1.\n\n``` python\nimport pandas as pd\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.preprocessing import StandardScaler\nfrom sklearn.linear_model import LogisticRegression\nfrom sklearn.metrics import accuracy_score\n\ndata = {\n    'Age': [25, 32, 45, 22, 38, 28, 50, 30],\n    'Salary': [500, 800, 1200, 400, 950, 600, 1500, 700],\n    'Experience': [2, 5, 15, 1, 8, 3, 20, 4],\n    'Gender': ['Male', 'Female', 'Male', 'Female', 'Male', 'Female', 'Male', 'Female'],\n    'Purchase': [0, 1, 1, 0, 1, 0, 1, 1]\n}\ndf = pd.DataFrame(data)\ndf['Gender'] = df['Gender'].map({'Male': 0, 'Female': 1})\nX = df[['Age', 'Salary', 'Experience', 'Gender']]\ny = df['Purchase']\n\nX_train, X_test, y_train, y_test = train_test_split(\n    X, y, test_size=0.2, random_state=42\n)\n# Train shape: (6, 4) — Test shape: (2, 4)\n```\n\n`test_size=0.2` means 20% of the data goes to testing. `random_state=42` makes the split reproducible.\n\nAge 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.\n\n```\nscaler = StandardScaler()\nX_train = scaler.fit_transform(X_train)\nX_test = scaler.transform(X_test)  # never fit_transform on test data!\n```\n\nSince the target is yes/no, this is a **classification** problem, and logistic regression is the natural first model:\n\n```\nmodel = LogisticRegression()\nmodel.fit(X_train, y_train)\n\ny_pred = model.predict(X_test)\nprint(\"Actual:   \", list(y_test))   # [0, 1]\nprint(\"Predicted:\", list(y_pred))   # [0, 1]\nprint(\"Accuracy: \", accuracy_score(y_test, y_pred))  # 1.0\n```\n\nThe model predicted both test customers correctly: accuracy **1.0**.\n\nNow 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):\n\n```\nnew_customer = [[36, 950, 6, 1]]\nnew_customer_scaled = scaler.transform(new_customer)\nprediction = model.predict(new_customer_scaled)\nprint(\"Approved\" if prediction[0] == 1 else \"Not Approved\")  # Approved\n```\n\nResult: **Approved (Yes)** — the model predicts this customer will purchase.\n\nIf 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.", "url": "https://wpnews.pro/news/customer-purchase-prediction-with-logistic-regression-a-beginner-s-end-to-end-ml", "canonical_source": "https://dev.to/waleedshamhan2050sys/customer-purchase-prediction-with-logistic-regression-a-beginners-end-to-end-ml-project-2d24", "published_at": "2026-10-16 16:38:45+00:00", "updated_at": "2026-10-16 16:47:37.367493+00:00", "lang": "en", "topics": ["machine-learning", "artificial-intelligence"], "entities": ["scikit-learn", "LogisticRegression", "StandardScaler", "pandas"], "also_reported_by": [], "alternates": {"html": "https://wpnews.pro/news/customer-purchase-prediction-with-logistic-regression-a-beginner-s-end-to-end-ml", "markdown": "https://wpnews.pro/news/customer-purchase-prediction-with-logistic-regression-a-beginner-s-end-to-end-ml.md", "text": "https://wpnews.pro/news/customer-purchase-prediction-with-logistic-regression-a-beginner-s-end-to-end-ml.txt", "jsonld": "https://wpnews.pro/news/customer-purchase-prediction-with-logistic-regression-a-beginner-s-end-to-end-ml.jsonld"}}