A production-ready FastAPI web application for customer segmentation using Machine Learning (KMeans Clustering). Built with MVC architecture following industry best practices.
- Features
- Project Structure
- Installation
- Usage
- API Documentation
- Architecture
- Customer Segments
- Technologies
- Machine Learning: KMeans clustering for customer segmentation
- FastAPI Backend: High-performance async API
- Modern UI: Beautiful, responsive HTML interface
- Real-time Predictions: Instant customer segment classification
- MVC Architecture: Clean separation of concerns
- Business Intelligence: Marketing strategy recommendations
- Input Validation: Pydantic schemas for data validation
- Auto Documentation: Swagger UI and ReDoc
- Best Practices: Following industry standards
practice/
βββ app/ # Main application package
β βββ __init__.py # Package initialization
β βββ core/ # Core configuration
β β βββ __init__.py
β β βββ config.py # Application settings
β βββ models/ # ML Models (Model layer)
β β βββ __init__.py
β β βββ ml_model.py # KMeans model handler
β βββ schemas/ # Data validation schemas
β β βββ __init__.py
β β βββ customer.py # Pydantic models
β βββ services/ # Business logic (Service layer)
β β βββ __init__.py
β β βββ prediction_service.py
β βββ controllers/ # Request handlers (Controller layer)
β β βββ __init__.py
β β βββ api_controller.py # REST API endpoints
β β βββ view_controller.py # HTML views
β βββ templates/ # Jinja2 templates (View layer)
β β βββ index.html # Home page
β β βββ about.html # About page
β βββ static/ # Static assets
β β βββ css/
β β β βββ style.css # Styles
β β βββ js/
β β βββ app.js # Frontend JavaScript
β βββ utils/ # Utility functions
β βββ __init__.py
β βββ helpers.py
βββ data/ # Data directory
β βββ raw/ # Raw data
β β βββ mall_customers.csv
β βββ processed/ # Processed data
β βββ mall_customers_processed.csv
βββ models_artifacts/ # Saved ML models
β βββ kmeans_model.pkl # Trained KMeans model
β βββ scaler.pkl # Feature scaler
βββ notebooks/ # Jupyter notebooks
β βββ 01_eda_preprocessing.ipynb
β βββ 02_modeling_evaluation.ipynb
βββ main.py # Application entry point
βββ train_model.py # Model training script
βββ requirements.txt # Python dependencies
βββ README.md # This file
- Python 3.10 or higher
- pip package manager
cd d:\AIML\practice# Create virtual environment
python -m venv venv
# Activate virtual environment
# On Windows:
venv\Scripts\activate
# On macOS/Linux:
source venv/bin/activatepip install -r requirements.txtFirst, you need to train the KMeans model and save the artifacts:
Option A: Using Jupyter Notebooks (Recommended for first-time users)
# Launch Jupyter
jupyter notebook
# Open and run these notebooks in order:
# 1. notebooks/01_eda_preprocessing.ipynb
# 2. notebooks/02_modeling_evaluation.ipynbOption B: Using Training Script
# This requires processed data from the notebooks
python train_model.pyThe training script will:
- Load processed customer data
- Train KMeans clustering model
- Save model artifacts to
models_artifacts/ - Generate customer segment labels
# Start the FastAPI server
python main.py
# Or using uvicorn directly:
uvicorn main:app --reload --host 0.0.0.0 --port 8000- Web Interface: http://localhost:8000
- API Documentation (Swagger): http://localhost:8000/docs
- API Documentation (ReDoc): http://localhost:8000/redoc
- Health Check: http://localhost:8000/api/v1/health
POST /api/v1/predict
Content-Type: application/json
{
"annual_income": 70.0,
"spending_score": 75
}Response:
{
"cluster_id": 1,
"cluster_name": "VIP / Whale",
"annual_income": 70.0,
"spending_score": 75,
"description": "High income, high spending customers",
"marketing_strategy": "Premium products, exclusive offers..."
}GET /api/v1/clustersGET /api/v1/clusters/infoGET /api/v1/model/infoGET /api/v1/healthimport requests
# Predict customer segment
response = requests.post(
"http://localhost:8000/api/v1/predict",
json={
"annual_income": 90.0,
"spending_score": 85
}
)
result = response.json()
print(f"Segment: {result['cluster_name']}")curl -X POST "http://localhost:8000/api/v1/predict" \
-H "Content-Type: application/json" \
-d '{"annual_income": 90.0, "spending_score": 85}'This application follows the MVC (Model-View-Controller) architectural pattern:
- Handles ML model loading, predictions, and persistence
- Manages KMeans clustering model and StandardScaler
- Provides model metadata and cluster centroids
- Jinja2 templates for HTML rendering
- CSS for styling
- JavaScript for interactivity and API calls
- API Controller: REST API endpoints
- View Controller: HTML page rendering
- Request/response handling
- Input validation
- Business logic and rules
- Prediction processing
- Cluster descriptions and marketing strategies
- Statistics calculation
- Application settings
- Environment variables
- Path configurations
- Pydantic models for validation
- Request/response schemas
- Type safety
The model identifies 5 distinct customer segments:
| Segment | Income | Spending | Description | Marketing Strategy |
|---|---|---|---|---|
| Average Customer | Moderate | Moderate | Balanced shoppers | Standard promotions, loyalty programs |
| VIP / Whale | High | High | Premium customers | Exclusive offers, VIP experiences |
| Young Trendsetter | Low-Moderate | High | Fashion-conscious | Trendy products, social media marketing |
| High Earner Saver | High | Low | Conservative spenders | Quality products, investment opportunities |
| Budget Conscious | Low | Low | Price-sensitive | Discounts, clearance sales |
- FastAPI - Modern, fast web framework
- Uvicorn - ASGI server
- Pydantic - Data validation
- scikit-learn - Machine Learning
- pandas - Data manipulation
- NumPy - Numerical computing
- HTML5 - Semantic markup
- CSS3 - Modern styling with gradients and animations
- JavaScript - Async API calls and DOM manipulation
- Jinja2 - Template engine
- KMeans Clustering - Customer segmentation algorithm
- StandardScaler - Feature normalization
- matplotlib - Visualization (notebooks)
- seaborn - Statistical visualization (notebooks)
Application settings can be configured in app/core/config.py:
class Settings(BaseSettings):
APP_NAME: str = "Customer Segmentation API"
APP_VERSION: str = "1.0.0"
DEBUG: bool = True
API_PREFIX: str = "/api/v1"
# ... more settings- Algorithm: KMeans with k-means++ initialization
- Number of Clusters: 5 (optimized using Elbow Method)
- Features: Annual Income, Spending Score
- Preprocessing: StandardScaler normalization
- Validation: Silhouette Score
- Test via Web Interface: Navigate to http://localhost:8000
- Test via API Docs: Go to http://localhost:8000/docs
- Test via Command Line:
# Health check
curl http://localhost:8000/api/v1/health
# Prediction
curl -X POST http://localhost:8000/api/v1/predict \
-H "Content-Type: application/json" \
-d '{"annual_income": 90, "spending_score": 85}'This project is created for educational purposes.