Practice Project 8

Unsupervised Learning & Feature Engineering Project

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.

Customer Data Total_Spend Orders_Count Return_Rate Scale 1. Prep & PCA StandardScaler PCA(n_comp=2) 2A. K-Means - Elbow Method (Inertia) - Silhouette Coefficients - Center centroids 2B. DBSCAN / Density - Epsilon & MinSamples - Density-based grouping - Outlier detection 3. Visual plots elbow_curve.png pca_clusters.png Segments Profile: - High spend / return
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.

$ xdg-open elbow_curve.png && xdg-open pca_clusters.png
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.

1. Scale data Standardize metrics to prevent skewness StandardScaler 2. PCA 2D Project columns onto 2 components PCA(n_components) 3. Elbow Loop Calculate inertia over multiple K settings KMeans(k).inertia_ 4. Density group Run DBSCAN models to catch anomalies DBSCAN(eps, min) 5. Save plots Save clustering maps to export PNG files plt.savefig()
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.

df = pd.read_csv("customer_data.csv") features = df[["Total_Spend", "Orders_Count", "Return_Rate"]] scaler = StandardScaler() X_scaled = scaler.fit_transform(features)
This normalizes features, scaling customer spend metrics to have a mean of 0 and variance of 1.
Lines 15 - 28

Determine optimal K (Elbow Loop)

Iterate over possible cluster counts, logging model inertia scores to find the optimal number of clusters.

inertia_scores = [] k_range = range(1, 8) for k in k_range: kmeans = KMeans(n_clusters=k, random_state=42) kmeans.fit(X_scaled) inertia_scores.append(kmeans.inertia_) # Plot Elbow Curve plt.figure() plt.plot(k_range, inertia_scores, 'bx-') plt.savefig("elbow_curve.png")
This loop fits K-Means across a range of K values, calculating within-cluster sum of squares to identify the elbow point.
Lines 29 - 42

Run PCA and Visualize Clusters

Apply PCA to reduce dimensionality to 2 components, then fit K-Means and plot a scatter graph.

pca = PCA(n_components=2) X_pca = pca.fit_transform(X_scaled) kmeans_final = KMeans(n_clusters=3, random_state=42) cluster_labels = kmeans_final.fit_transform(X_scaled) plt.scatter(X_pca[:, 0], X_pca[:, 1], c=kmeans_final.labels_, cmap='viridis') plt.savefig("pca_clusters.png")
This projects customer profiles onto 2 PCA coordinates, clusters them into 3 groups, and plots the results.

Production templates

1. Sample Dataset (Save as ~/Projects/customer_clustering/customer_data.csv):

Customer_ID,Total_Spend,Orders_Count,Return_Rate 1,150.00,5,0.02 2,1200.00,25,0.05 3,45.50,2,0.00 4,950.00,22,0.04 5,15.50,1,0.00 6,1100.00,24,0.06 7,135.00,4,0.01 8,850.00,18,0.03 9,25.00,1,0.00 10,1300.00,28,0.05 11,180.00,6,0.02 12,900.00,20,0.04 13,50.00,2,0.00 14,1050.00,23,0.05 15,160.00,5,0.01

2. Python script (Save as ~/Projects/customer_clustering/customer_segmentation.py):

# customer_segmentation.py - Unsupervised customer clustering 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 def main(): print("=== Loading Customer Purchasing Dataset ===") df = pd.read_csv("customer_data.csv") features = df[["Total_Spend", "Orders_Count", "Return_Rate"]] # 1. Feature Standardization Scaling scaler = StandardScaler() X_scaled = scaler.fit_transform(features) print("Features normalized to standard scaling scale.") # 2. Optimal K determination via Elbow curve metrics print("\n[Running Elbow Method Inertia Sweeps]") inertia_scores = [] k_range = range(1, 8) for k in k_range: kmeans = KMeans(n_clusters=k, random_state=42, n_init=10) kmeans.fit(X_scaled) inertia_scores.append(kmeans.inertia_) print(f" - KMeans K={k} | Within-cluster Sum of Squares: {kmeans.inertia_:.4f}") # Save Elbow Plot plt.style.use('dark_background') plt.figure(figsize=(8, 4)) plt.plot(k_range, inertia_scores, 'bx-', color="#38bdf8", linewidth=2) plt.title("K-Means Optimal Clustering Elbow Curve", color="#f8fafc") plt.xlabel("Cluster count (K)", color="#94a3b8") plt.ylabel("Inertia", color="#94a3b8") plt.grid(True, linestyle="--", color="#334155", alpha=0.5) elbow_filename = "elbow_curve.png" plt.savefig(elbow_filename, facecolor="#0f172a", edgecolor="none") print(f"Elbow plot saved: {elbow_filename}") # Calculate silhouette score for K=3 kmeans_3 = KMeans(n_clusters=3, random_state=42, n_init=10) labels_3 = kmeans_3.fit_predict(X_scaled) sil_score = silhouette_score(X_scaled, labels_3) print(f"K=3 Silhouette Score: {sil_score:.4f}") # 3. PCA Dimensionality Reduction to 2D print("\n[PCA Dimensionality Reduction]") pca = PCA(n_components=2) X_pca = pca.fit_transform(X_scaled) print(f"Explained variance ratio: Component 1={pca.explained_variance_ratio_[0]:.4f}, Component 2={pca.explained_variance_ratio_[1]:.4f}") # 4. Compare K-Means vs DBSCAN print("\n[Running DBSCAN Density Clustering]") dbscan = DBSCAN(eps=0.5, min_samples=2) db_labels = dbscan.fit_predict(X_scaled) print(f" - DBSCAN found: {len(set(db_labels)) - (1 if -1 in db_labels else 0)} clusters.") print(f" - Noise outliers detected: {list(db_labels).count(-1)} records.") # Save 2D PCA Cluster visualization plot plt.figure(figsize=(8, 5)) scatter = plt.scatter(X_pca[:, 0], X_pca[:, 1], c=labels_3, cmap='viridis', s=100, alpha=0.8) plt.title("Customer Segments Visualized via PCA 2D Space", color="#f8fafc") plt.xlabel("Principal Component 1", color="#94a3b8") plt.ylabel("Principal Component 2", color="#94a3b8") plt.colorbar(scatter, label="Cluster Labels") plt.grid(True, linestyle="--", color="#334155", alpha=0.5) cluster_filename = "pca_clusters.png" plt.savefig(cluster_filename, facecolor="#0f172a", edgecolor="none") print(f"PCA cluster scatter plot saved: {cluster_filename}") print("\n=== Unsupervised Learning Project Successfully Complete! ===") if __name__ == "__main__": main()
5. Deliverables Summary

Verify that the following configurations and outputs exist inside your project workspace.

Created Files / Templates

  • ~/Projects/customer_clustering/customer_data.csv - Customer feature database.
  • ~/Projects/customer_clustering/customer_segmentation.py - Unsupervised pipeline script file.
  • ~/Projects/customer_clustering/elbow_curve.png - Saved optimal cluster check graph.
  • ~/Projects/customer_clustering/pca_clusters.png - 2D PCA cluster visualization plot.

Verification Artifacts / Execution Proof

  • K-Means inertia scores printed for K values 1 through 7.
  • Silhouette metric score calculated for K=3 (around 0.65+).
  • DBSCAN execution log identifying core clusters and noise outliers.
6. Closing Explanation: Why We Did This & What It Accomplishes

Architectural Intent & Operational Impact

Why We Did This

What This Accomplishes