Segment customer records using K-Means and DBSCAN algorithms, evaluate clusters via Elbow and Silhouette scores, and apply PCA to reduce dimensions for 2D visualizations.
Domain / Environment
Retail Marketing / Conda VM
Difficulty
Intermediate (3/5)
Course Module
Unsupervised ML & Feature Eng
Deliverables
Clustering Plots & PCA 2D Scatter Image
1. System Architecture & Process Workflow
The diagram below displays the customer clustering pipeline. Raw features are scaled, reducing variance. These features are projected onto principal components using PCA, reducing dimensionality, and clustered using K-Means and DBSCAN.
2. Part 1: Step-by-Step Action Items & Key Execution Steps
STEP 1
Activate Python Virtual Environment
Point the terminal execution environment to the course conda sandbox environment.
$ conda activate ds_ai_ml
This points terminal execution to the active python virtual packages directory.
STEP 2
Create Project Folders inside Linux VM
Create a dedicated folder for the project files inside your guest VM home folder directory.
$ mkdir -p ~/Projects/customer_clustering && cd ~/Projects/customer_clustering
This sets up the working directory layout for the clustering code files.
STEP 3
Save Customer Purchasing Dataset
Create a CSV dataset containing customer transactional details, including spend totals and return rates.
$ nano customer_data.csv
This opens nano editor. Paste the CSV data template from Part 2, press **Ctrl + O** and **Enter** to save, and **Ctrl + X** to exit.
STEP 4
Create clusterer script file in VS Code
Launch VS Code and create a new script file inside the project workspace folder.
Launch VS Code via terminal "code ." -> Right-click in explorer tree -> click New File -> Type: customer_segmentation.py -> Press Enter
This registers an empty file `customer_segmentation.py` in the workspace editor window.
STEP 5
Load Python clustering comparison logic
Paste the K-means, DBSCAN, PCA, and metrics code blocks into the empty file.
Click customer_segmentation.py -> Paste python code from Part 2 below -> Save file via Ctrl + S
This populates the script file with clustering and PCA directives.
STEP 6
Execute unsupervised pipeline script
Run the validation script using the python engine to compare the models.
$ python customer_segmentation.py
This standardizes features, evaluates optimal cluster counts, applies PCA, runs DBSCAN comparisons, and saves output plots.
STEP 7
Open and verify generated figures
Open the figures using the default Linux desktop photo viewer to review the cluster shapes.
This command loads the image viewer application on the VM desktop to display the plots.
3. Operational Pipeline Architecture
The flowchart below outlines the unsupervised clustering pipeline. It details the steps from loading dataset records and scaling features to running PCA, evaluating inertia, and saving figures.
4. Part 2: Complete Deliverable Assets & Production Templates
To run the clustering, we need the raw customer dataset and the Python analysis script. Below is a line-by-line explanation of the code, followed by the combined template files.
Step-by-Step Code Construction
Lines 1 - 7
Import Unsupervised libraries
Include system OS, Pandas/NumPy, scaling, PCA, and clustering modules in the script.
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA
from sklearn.cluster import KMeans, DBSCAN
from sklearn.metrics import silhouette_score
import matplotlib.pyplot as plt
These imports load dataset scaling functions, PCA models, clustering algorithms, and graphing libraries.
Lines 8 - 14
Scale numeric features
Load customer records and scale columns to prevent feature weight bias.