Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

61 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🎯 KMeans Visualizer

Interactive machine learning visualization application that demonstrates how the K-Means Clustering algorithm works step by step.

The project is designed as an educational tool for learning machine learning fundamentals, clustering algorithms, data visualization, and modern software engineering practices.


🎬 Application Demo

KMeans Visualizer Demo

☸️ Kubernetes Deployment Demo

Kubernetes Demo


✨ Current Features

πŸ“Š Dataset Generator

  • Generate random 2D datasets
  • Configure the number of points
  • Return datasets through FastAPI API
  • Prepare datasets for clustering experiments

🎯 K-Means Clustering Engine

  • Custom K-Means implementation using NumPy
  • Configurable number of clusters (k)
  • Configurable maximum number of iterations
  • Random centroid initialization
  • Point-to-centroid assignment
  • Centroid recalculation
  • Inertia (loss) calculation
  • Iteration history tracking
  • Final centroid calculation

🐳 Dockerized Development Environment

  • Backend Docker container
  • Frontend Docker container
  • Docker Compose orchestration
  • One-command local environment startup
  • Containerized FastAPI application
  • Containerized React application

☸️ Kubernetes Deployment

  • Backend Deployment
  • Frontend Deployment
  • Backend Service
  • Frontend Service
  • Kubernetes Ingress
  • Internal service discovery
  • Container orchestration with Kubernetes
  • Local Kubernetes deployment support

πŸ“ˆ Kubernetes Scaling & Monitoring

  • Backend Horizontal Pod Autoscaler (HPA)
  • CPU-based autoscaling configuration
  • Kubernetes ConfigMap configuration management
  • Resource requests and limits
  • Liveness probes
  • Readiness probes
  • Health monitoring through Kubernetes

⚑ Kubernetes Automation

  • One-command Kubernetes deployment
  • Automatic Docker image builds
  • Automatic Kubernetes resource deployment
  • Automated deployment rollout verification
  • Automated cluster cleanup
  • Automated port-forwarding setup

🚧 Planned Features

  • Enhanced centroid movement visualization
  • Inertia charts
  • Cluster assignment visualization improvements
  • Responsive UI
  • Dark mode
  • Linear Regression Visualizer

πŸ”„ Continuous Integration

  • Automated backend quality checks
  • Automated frontend linting
  • Automated frontend production build verification
  • Automated Docker image builds
  • Automated Kubernetes manifest validation
  • GitHub Actions CI workflow

πŸ› οΈ Tech Stack

Backend

  • Python 3.13
  • FastAPI
  • NumPy

Frontend

  • React
  • Vite
  • JavaScript
  • Axios
  • SVG Visualization

DevOps

  • Docker
  • Docker Compose
  • Kubernetes
  • Ingress
  • GitHub Actions

πŸ“ Project Structure

KMeansVisualizer/
.github/
β”œβ”€β”€ workflows/
β”‚    └── ci.yml
β”œβ”€β”€ backend/
β”‚   β”œβ”€β”€ schemas/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ dataset.py
β”‚   β”‚   └── kmeans.py
β”‚   β”‚
β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”œβ”€β”€ __init__.py
β”‚   β”‚   β”œβ”€β”€ dataset_service.py
β”‚   β”‚   └── kmeans_service.py
β”‚   β”‚
β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”œβ”€β”€ .dockerignore
β”‚   β”œβ”€β”€ main.py
β”‚   β”œβ”€β”€ requirements.txt
β”‚   └── pyproject.toml
β”‚
β”œβ”€β”€ frontend/
β”‚   β”œβ”€β”€ public/
β”‚   β”‚
β”‚   β”œβ”€β”€ src/
β”‚   β”‚   β”œβ”€β”€ assets/
β”‚   β”‚   β”œβ”€β”€ components/
β”‚   β”‚   β”‚   β”œβ”€β”€ ClusteringMetrics.jsx
β”‚   β”‚   β”‚   β”œβ”€β”€ DatasetGenerator.jsx
β”‚   β”‚   β”‚   β”œβ”€β”€ IterationControls.jsx
β”‚   β”‚   β”‚   β”œβ”€β”€ KMeansControls.jsx
β”‚   β”‚   β”‚   └── ScatterPlot.jsx
β”‚   β”‚   β”‚
β”‚   β”‚   β”œβ”€β”€ services/
β”‚   β”‚   β”‚   └── api.js
β”‚   β”‚   β”‚
β”‚   β”‚   β”œβ”€β”€ App.jsx
β”‚   β”‚   β”œβ”€β”€ App.css
β”‚   β”‚   β”œβ”€β”€ index.css
β”‚   β”‚   └── main.jsx
β”‚   β”‚
β”‚   β”œβ”€β”€ Dockerfile
β”‚   β”œβ”€β”€ .dockerignore
β”‚   β”œβ”€β”€ package.json
β”‚   β”œβ”€β”€ package-lock.json
β”‚   β”œβ”€β”€ vite.config.js
β”‚   └── eslint.config.js
β”‚
β”œβ”€β”€ k8s/
β”‚   β”œβ”€β”€ backend-deployment.yaml
β”‚   β”œβ”€β”€ backend-service.yaml
β”‚   β”œβ”€β”€ frontend-deployment.yaml
β”‚   β”œβ”€β”€ frontend-service.yaml
β”‚   β”œβ”€β”€ backend-hpa.yaml
β”‚   β”œβ”€β”€ configmap.yaml
β”‚   └── ingress.yaml
β”‚
β”œβ”€β”€ scripts/
β”‚   β”œβ”€β”€ deploy-k8s.ps1
β”‚   β”œβ”€β”€ destroy-k8s.ps1
β”‚   └── port-forward.ps1
β”‚
β”œβ”€β”€ docker-compose.yml
β”œβ”€β”€ README.md
└── .gitignore

πŸš€ Backend Setup

Clone Repository

git clone <repository-url>
cd KMeansVisualizer

Create Virtual Environment

cd backend
py -m venv venv

Activate Virtual Environment

.\venv\Scripts\Activate.ps1

Install Dependencies

pip install -r requirements.txt

Run Backend

python -m uvicorn main:app --reload

Backend will be available at:

http://localhost:8000

Swagger UI:

http://localhost:8000/docs

πŸ’» Frontend Setup

Install Dependencies

cd frontend
npm install

Run Frontend

npm run dev

Frontend will be available at:

http://localhost:5173

Verify Frontend Build

npm run build

🐳 Docker Setup

Build and Start All Services

docker compose up --build

Services will be available at:

Frontend: http://localhost:5173
Backend:  http://localhost:8000
Swagger:  http://localhost:8000/docs

Stop Services

docker compose down

☸️ Kubernetes Setup

Build Local Docker Images

docker build -t kmeans-backend:latest ./backend
docker build -t kmeans-frontend:latest ./frontend

Deploy to Kubernetes

kubectl apply -f k8s/

Verify Resources

kubectl get pods
kubectl get services
kubectl get ingress

Verify Horizontal Pod Autoscaler

kubectl get hpa
kubectl describe hpa kmeans-backend-hpa

Verify ConfigMap

kubectl get configmaps
kubectl describe configmap kmeans-config

Inspect Pod Health

kubectl describe pod <pod-name>

Access Backend

kubectl port-forward service/kmeans-backend-service 8000:8000

Backend will be available at:

http://localhost:8000

Access Frontend

kubectl port-forward service/kmeans-frontend-service 5173:5173

Frontend will be available at:

http://localhost:5173

Remove Kubernetes Resources

kubectl delete -f k8s/

⚑ Kubernetes Automation Scripts

Deploy Entire Environment

.\scripts\deploy-k8s.ps1

This script:

  • Removes previous Kubernetes resources
  • Builds backend Docker image
  • Builds frontend Docker image
  • Deploys all Kubernetes manifests
  • Waits for deployment rollout
  • Displays cluster status

Remove Entire Environment

.\scripts\destroy-k8s.ps1

This script:

  • Removes all Kubernetes resources
  • Displays remaining cluster resources

Start Port Forwarding

.\scripts\port-forward.ps1

This script:

  • Opens backend port forwarding
  • Opens frontend port forwarding
  • Provides local access to the application

πŸ”— Available Endpoints

Health Check

GET /health

Response:

{
  "status": "ok"
}

Generate Dataset

POST /datasets/generate

Request:

{
  "points": 100
}

Response:

{
  "points": [
    [12.5, 45.1],
    [22.8, 11.3]
  ]
}

Run K-Means

POST /kmeans/steps

Request:

{
  "points": [
    [1, 2],
    [2, 1],
    [8, 9],
    [9, 8]
  ],
  "k": 2,
  "max_iterations": 10
}

Response:

{
  "iterations": [...],
  "final_centroids": [...]
}

πŸ“š API Documentation

Swagger UI:

http://localhost:8000/docs

πŸ“Έ Current Application Preview

Frontend

  • Dataset generation UI
  • K-Means execution UI
  • Scatter plot visualization
  • Cluster color visualization
  • Centroid visualization
  • Iteration slider
  • Previous / Next controls
  • Play / Pause animation
  • Reset animation
  • Clustering metrics dashboard

Backend

  • Root API endpoint
  • Health check endpoint
  • Dataset generation API
  • K-Means clustering API
  • Iteration tracking
  • Inertia calculation
  • Swagger documentation

Infrastructure

  • Dockerized backend
  • Dockerized frontend
  • Docker Compose environment
  • Kubernetes deployments
  • Kubernetes services
  • Kubernetes ingress
  • Horizontal Pod Autoscaler (HPA)
  • ConfigMap-based configuration
  • Health monitoring probes
  • Resource limits and requests
  • Automated Kubernetes deployment scripts
  • Automated Kubernetes cleanup scripts
  • Automated port-forwarding scripts

πŸ”„ GitHub Actions CI

The project includes an automated CI workflow executed on every push and pull request.

Automated Checks

  • Backend Ruff linting
  • Backend Black formatting verification
  • Frontend ESLint verification
  • Frontend production build verification
  • Backend Docker image build
  • Frontend Docker image build
  • Kubernetes manifest validation

Workflow File

.github/workflows/ci.yml

βœ… Code Quality

Backend Code Formatting

cd backend
py -m black .

Run Backend Linter

cd backend
py -m ruff check .

Auto-Fix Backend Linter Issues

cd backend
py -m ruff check . --fix

Frontend Lint

cd frontend
npm run lint

Validate Docker Compose

docker compose config

Build Backend Docker Image

docker build -t kmeans-backend ./backend

Build Frontend Docker Image

docker build -t kmeans-frontend ./frontend

Validate Kubernetes Resources

kubectl apply --dry-run=client -f k8s/

View Kubernetes Resources

kubectl get pods
kubectl get services
kubectl get ingress

View Horizontal Pod Autoscaler

kubectl get hpa

Describe HPA

kubectl describe hpa kmeans-backend-hpa

View ConfigMaps

kubectl get configmaps

Inspect Pod Health

kubectl describe pod <pod-name>

πŸ”§ Development Tools

  • Black - Python formatter
  • Ruff - Python linter
  • React - Frontend UI
  • Vite - Frontend tooling
  • Axios - API communication
  • FastAPI - Backend API
  • Swagger UI - API documentation
  • Docker - Containerization
  • Docker Compose - Local orchestration
  • Kubernetes - Container orchestration
  • Ingress - Traffic routing
  • Horizontal Pod Autoscaler (HPA) - Automatic scaling
  • ConfigMap - Configuration management
  • Liveness Probe - Container health checks
  • Readiness Probe - Traffic readiness checks
  • PowerShell Scripts - Kubernetes automation
  • GitHub Actions - Continuous Integration

πŸŽ“ Learning Goals

This project demonstrates:

  • Machine Learning Fundamentals
  • K-Means Clustering Algorithm
  • Numerical Computing with NumPy
  • FastAPI Backend Development
  • API Design and Validation
  • React State Management
  • API Integration with Axios
  • Interactive Algorithm Visualization
  • Frontend-Backend Communication
  • Data Visualization
  • Docker Containerization
  • Docker Compose Orchestration
  • Kubernetes Deployments
  • Kubernetes Services
  • Kubernetes Ingress
  • Kubernetes Horizontal Pod Autoscaling
  • Kubernetes ConfigMaps
  • Kubernetes Health Checks
  • Resource Management in Kubernetes
  • Application Monitoring Concepts
  • Infrastructure Automation
  • Kubernetes Operational Workflows
  • Deployment Automation
  • CI/CD Automation

About

Interactive K-Means Clustering Visualizer built with React and Python. Explore clustering step-by-step, centroid updates, convergence, and inertia metrics in real time.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Contributors

Languages