Spark MLlib
K-Means
machine learning
clustering
data science

Spark MLlib / K-Means intuition

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

K-Means in Spark MLlib is one of the fastest ways to cluster large unlabeled datasets, but its output quality depends on data preparation and parameter choices more than the algorithm call itself. Intuitively, K-Means keeps moving cluster centers until assignments stop changing significantly. To use it well, you need to understand scaling, initialization, and how to pick k.

Core Intuition of K-Means

K-Means tries to minimize within-cluster squared distance. Each iteration does two steps:

  1. assign each point to the nearest centroid.
  2. recompute each centroid as the mean of assigned points.

Repeat until convergence or max iteration limit.

This works best when clusters are roughly spherical and similarly scaled. If one feature dominates numeric magnitude, it can overwhelm distance calculations and distort assignments.

Spark MLlib Workflow

In modern Spark, you typically use DataFrame-based pyspark.ml APIs instead of legacy RDD mllib APIs.

python
1from pyspark.sql import SparkSession
2from pyspark.ml.feature import VectorAssembler, StandardScaler
3from pyspark.ml.clustering import KMeans
4
5spark = SparkSession.builder.appName("kmeans-demo").getOrCreate()
6
7data = spark.createDataFrame(
8    [
9        (1.0, 1.2),
10        (1.1, 0.9),
11        (8.0, 8.2),
12        (7.8, 7.9),
13        (0.9, 1.0),
14        (8.3, 8.1)
15    ],
16    ["x", "y"]
17)
18
19assembler = VectorAssembler(inputCols=["x", "y"], outputCol="raw_features")
20assembled = assembler.transform(data)
21
22scaler = StandardScaler(inputCol="raw_features", outputCol="features", withStd=True, withMean=True)
23scaled_model = scaler.fit(assembled)
24scaled = scaled_model.transform(assembled)
25
26kmeans = KMeans(
27    k=2,
28    seed=42,
29    featuresCol="features",
30    predictionCol="cluster",
31    maxIter=20
32)
33
34model = kmeans.fit(scaled)
35result = model.transform(scaled)
36result.select("x", "y", "cluster").show()
37print("Training cost:", model.summary.trainingCost)

This is runnable in a PySpark environment and demonstrates the full pipeline including scaling.

Choosing k with Practical Heuristics

K-Means requires you to pick number of clusters upfront. Common selection strategies:

  • elbow method using within-cluster sum of squares.
  • silhouette score for separation quality.
  • domain constraints such as known segment count.

Do not rely on a single metric blindly. Combine metric trend with business interpretation of clusters.

Example loop for elbow exploration:

python
1costs = []
2for k in range(2, 8):
3    km = KMeans(k=k, seed=42, featuresCol="features")
4    m = km.fit(scaled)
5    costs.append((k, m.summary.trainingCost))
6
7print(costs)

If the curve flattens after a value, that point is often a good starting candidate.

Distributed Behavior and Performance

Spark parallelizes distance calculations and centroid updates across partitions, which scales well for large datasets. Still, performance depends on:

  • partition sizing.
  • feature vector dimensionality.
  • serialization cost.
  • number of iterations.

Persist transformed features if reused across multiple k runs. This avoids recomputing expensive pipeline stages during tuning.

Interpreting Cluster Results

K-Means cluster IDs are labels, not rankings. Cluster 0 is not better than cluster 1. After training:

  • inspect centroid coordinates in original feature space.
  • profile each cluster with summary statistics.
  • validate cluster stability across random seeds.

If clusters change dramatically across seeds or time windows, the segmentation may not be operationally reliable.

When K-Means Is a Poor Fit

K-Means is weak when clusters are highly non-spherical, heavily imbalanced, or dominated by categorical features without proper encoding. In those cases, consider alternatives such as Gaussian mixtures, density-based clustering, or hierarchical methods. Picking the right algorithm often improves results more than hyperparameter tuning alone.

Common Pitfalls

  • Skipping feature scaling and letting large-magnitude columns dominate distances.
  • Assuming K-Means can model arbitrary non-spherical cluster shapes.
  • Picking k by guesswork without metric or domain validation.
  • Reading cluster IDs as ordered business tiers.
  • Running repeated model fits without caching transformed features.

Summary

  • K-Means iteratively assigns points and updates centroids to reduce squared distance.
  • In Spark, use DataFrame-based ML pipelines with scaling before clustering.
  • Choose k with elbow or silhouette analysis plus domain judgment.
  • Optimize distributed runs with caching and realistic partitioning.
  • Validate cluster stability and interpret centroids before production use.

Related reading
Course
Beginner
27 lessons
10 hours
System Design Fundamentals

Build a strong foundation in designing scalable, reliable distributed systems.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.