Skip to content
Pablo Rodriguez

K Means Programming

This exercise implements the K-means algorithm and applies it to image compression by reducing colors in an image to only the most common ones.

  • 1.1 Finding closest centroids (Exercise 1)
  • 1.2 Computing centroid means (Exercise 2)
  • Run implemented algorithm on toy 2D dataset
  • Visualize algorithm progress through iterations
  • Understanding centroid initialization process
  • Using kMeans_init_centroids function
  • 4.1 Dataset: Load and process bird image
  • 4.2 K-means on image pixels: Apply clustering to RGB values
  • 4.3 Compress image: Reconstruct using reduced color palette

Objective: Complete find_closest_centroids function

Task:

  • Input: Data matrix X, centroid locations
  • Output: Array idx with closest centroid index for each example
  • Formula: c⁽ⁱ⁾ := j that minimizes ||x⁽ⁱ⁾ - μⱼ||²

Implementation approach:

find-closest-centroids.py
for i in range(X.shape[0]):
distance = []
for j in range(centroids.shape[0]):
norm_ij = np.linalg.norm(X[i] - centroids[j])
distance.append(norm_ij)
idx[i] = np.argmin(distance)

Objective: Complete compute_centroids function

Task:

  • Input: Data matrix X, assignments idx, number of clusters K
  • Output: New centroids as means of assigned points
  • Formula: μₖ = (1/|Cₖ|) Σᵢ∈Cₖ x⁽ⁱ⁾

Implementation approach:

compute-centroids.py
for k in range(K):
points = X[idx == k] # Get points assigned to cluster k
centroids[k] = np.mean(points, axis=0) # Compute mean
  • Starts with random initial centroids
  • Iteratively assigns points and moves centroids
  • Visualizes progress through multiple iterations
  • Converges to final clustering solution
  • Algorithm should converge to stable cluster assignments
  • Final centroids become black X-marks in cluster centers
  • Connected X-marks show centroid movement history
  • Original encoding: 24-bit color (8 bits × 3 RGB channels)
  • Original size: 128 × 128 × 24 = 393,216 bits
  • Compressed encoding: 4 bits per pixel + 16 colors × 24 bits
  • Compressed size: 16 × 24 + 128 × 128 × 4 = 65,920 bits
  • Compression ratio: ~6:1 reduction
  1. Reshape image: Convert to m × 3 matrix (RGB values per pixel)
  2. Apply K-means: Find K=16 most representative colors
  3. Assign pixels: Map each pixel to closest representative color
  4. Reconstruct: Create new image using only 16 colors
  • Compressed image retains most characteristics of original
  • Some compression artifacts due to reduced color palette
  • Trade-off between image quality and file size
  • Implementation of core K-means components
  • Understanding of iterative optimization process
  • Experience with convergence behavior
  • Real-world use case in image compression
  • Trade-offs between quality and compression
  • Visualization of clustering results
  • NumPy operations for distance calculations
  • Array indexing and boolean masking
  • Image processing and visualization

The K-means programming exercise demonstrates both the algorithmic foundations and practical utility of clustering in computer vision and data compression applications.