Join the PiyushAI AI & Data Science Community | Newsletter
📬 PiyushAI  ·  AI & Data Science Learning Community

Stay Ahead in AI, Data Science, Exams & Your Learning Journey

Join 20,000+ learners exploring AI & Data Science — GATE, Bank IT & PSU exam aspirants, IIT Madras BS Degree students, school teachers exploring the CBSE CT & AI curriculum, working professionals, and anyone starting their AI literacy journey. Tell us a little about yourself and get personalised updates, resources, and mentorship alerts — straight from Piyush Wairale.

🎯
Exam & Career Updates First
GATE, Bank IT Officer, PSU & Government job alerts — plus IIT Madras BS Degree guidance.
📚
Free Learning Resources
Study notes, PYQ analysis, practice questions & guides for exams, data science & AI.
🚀
AI Literacy & CBSE CT-AI
AI tools & concepts for everyone, CBSE CT & AI curriculum support for schools & teachers, plus early course access.
✍️ Join the Community — Fill the Form

Takes less than 60 seconds  •  No spam, only what helps you learn & grow

👨‍🎓 20,000+ Students
▶️ 44,000+ YouTube Subscribers
🎓 IIT Madras Alumnus Mentor
GATE DA 2027 · MACHINE LEARNING

K-Means Clustering & PCA for GATE DA: Worked Iterations and Variance Explained

The unsupervised half of the ML syllabus, done by hand — every assign/update step of a k-means run traced to convergence, and a complete PCA with eigenvalues, components and variance-explained ratios.

2
Iterations to converge (worked)
3
GATE-style solved problems
2–3
Marks asked most years
Feb 2027
GATE DA exam (IIT Madras)

By Piyush Wairale · GATE DA Educator & Course Instructor, IIT Madras BS Programme · Updated August 2026

Key Takeaways

K-means minimises within-cluster sum of squares (WCSS) by alternating two steps: assign each point to its nearest centroid, then update each centroid to its cluster’s mean. It always converges — but only to a local optimum.

• The elbow method picks k where the WCSS-vs-k curve stops dropping steeply; k-means++ fixes bad initialisations.

PCA finds orthogonal directions of maximum variance: eigenvectors of the covariance matrix, sorted by eigenvalue. Variance explained by PCᵢ = λᵢ/Σλ.

• PCA is SVD applied to centered data — same directions, and GATE asks the variance-explained NAT almost every year.

The K-Means Objective: What Is Actually Being Minimised

Given n points and a chosen k, k-means seeks cluster assignments and centroids μ₁…μₖ minimising the within-cluster sum of squares:

WCSS = Σₖ Σx ∈ Cₖ ‖x − μₖ‖²

Both alternating steps can only lower (or keep) this objective — assignment picks the nearest centroid by definition, and the mean is the point minimising summed squared distance to a set. Since there are finitely many partitions, the algorithm must converge. Clustering and dimensionality reduction are both named in the official GATE DA syllabus; the ML pillar guide places them in the full roadmap.

▶ Watch: my complete Machine Learning playlist for GATE DA

Clustering and PCA numericals solved on camera — all playlists →

A Full K-Means Run, Worked to Convergence

Data (1D for clean arithmetic): 2, 3, 4, 10, 11, 12, with k = 2 and initial centroids μ₁ = 3, μ₂ = 4.

Iteration 1

Assign: 2 → C₁ (|2−3| = 1 < |2−4| = 2); 3 → C₁ (0 < 1); 4 → C₂ (0); 10, 11, 12 → C₂ (all nearer 4 than 3). Clusters: C₁ = {2, 3}, C₂ = {4, 10, 11, 12}.

Update: μ₁ = (2+3)/2 = 2.5; μ₂ = (4+10+11+12)/4 = 9.25.

Iteration 2

Assign: 2, 3 stay in C₁. Point 4: |4−2.5| = 1.5 < |4−9.25| = 5.25 → 4 switches to C₁. 10, 11, 12 stay in C₂. Clusters: C₁ = {2, 3, 4}, C₂ = {10, 11, 12}.

Update: μ₁ = (2+3+4)/3 = 3; μ₂ = (10+11+12)/3 = 11.

Iteration 3 — convergence check

Assign: every point keeps its cluster (all of 2, 3, 4 are nearer 3; all of 10, 11, 12 nearer 11). No assignment changed → converged. Final WCSS = (1+0+1) + (1+0+1) = 4. GATE questions stop at exactly this level: run one or two iterations, report a centroid or the final WCSS.

234 101112 μ₁ = 3 μ₂ = 11 final centroids after 2 iterations (WCSS = 4)

Properties, Initialisation and the Elbow Method

Facts GATE tests directly: k-means always converges in finitely many steps, but to a local optimum — different initial centroids can give different final clusterings (run it multiple times and keep the lowest WCSS). It assumes roughly spherical, similar-sized clusters, uses Euclidean distance, and is sensitive to outliers (a single far point drags its centroid). k-means++ initialises centroids far apart probabilistically, dramatically improving typical results.

Choosing k — the elbow method: WCSS always decreases as k grows (more centroids can only help), so you can’t just minimise it. Plot WCSS against k and pick the “elbow” where the steep drop flattens:

elbow → pick this k 123456 WCSS

Hierarchical Clustering in Brief

Agglomerative hierarchical clustering needs no k upfront: start with every point as its own cluster and repeatedly merge the two closest clusters, producing a dendrogram you can cut at any height. “Closest” depends on the linkage: single linkage = minimum pairwise distance between clusters (prone to chaining), complete linkage = maximum pairwise distance (compact clusters), average linkage = mean of all pairs. GATE numericals give a small distance matrix and ask which pair merges first, or the single/complete-linkage distance between two named clusters — pure table lookup plus min/max.

PCA: Directions of Maximum Variance, Worked

The recipe: (1) center the data (subtract each feature’s mean); (2) form the covariance matrix S; (3) find its eigenvalues and eigenvectors; (4) sort by eigenvalue — the top eigenvector is PC1, the direction along which the data varies most; (5) project onto the top components to reduce dimension.

PC1 (max variance) PC2 (⊥ PC1) PCA rotates axes to align with the data’s spread

Worked 2D example

Suppose the centered data has covariance matrix S = [ [5, 2], [2, 2] ].

Eigenvalues: det(S − λI) = (5−λ)(2−λ) − 4 = λ² − 7λ + 6 = 0 ⇒ λ₁ = 6, λ₂ = 1.

PC1 direction: solve (S − 6I)v = 0: (−1)v₁ + 2v₂ = 0 ⇒ v₁ = 2v₂ ⇒ v = (2, 1)/√5. PC2 is the orthogonal (−1, 2)/√5.

Variance explained: PC1 accounts for λ₁/(λ₁+λ₂) = 6/7 ≈ 85.7%; keeping only PC1 halves the dimension while retaining ~86% of the variance. Note the sanity check GATE loves: trace(S) = 5 + 2 = 7 = λ₁ + λ₂ — total variance is preserved by the rotation.

PCA ↔ SVD, and Choosing the Number of Components

Run SVD on the centered data matrix X and the right singular vectors V are exactly the principal components, with λᵢ = σᵢ²/(n−1). That’s why numerical libraries implement PCA via SVD — no covariance matrix ever formed. To choose how many components to keep: plot eigenvalues in decreasing order (a scree plot, same elbow logic as WCSS-vs-k) or keep the smallest m with cumulative variance Σ₁…ₘλᵢ/Σλ above a threshold like 90–95%. Two practical rules that appear as MCQs: standardise features first (else the largest-scale feature hijacks PC1), and components are orthogonal by construction. Eigen-machinery rusty? The Linear Algebra pillar rebuilds it from scratch.

K-Means vs Hierarchical vs PCA at a Glance

AspectK-meansHierarchicalPCA
TaskPartition into k clustersNested cluster treeDimensionality reduction
Needs k upfront?Yes (elbow helps)No — cut dendrogram laterChoose m by variance kept
Core computationAssign/update to local optimumMerge closest by linkageEigendecomposition of covariance
Deterministic?No — depends on initYes, given linkageYes (up to sign)

Three GATE-Style Problems, Solved

Problem 1 (MCQ). Centroids are μ₁ = (2, 3) and μ₂ = (6, 1). Where does point (4, 4) go?

Solution. Squared distances (no square roots needed for comparison): to μ₁: (4−2)² + (4−3)² = 5; to μ₂: (4−6)² + (4−1)² = 13. 5 < 13 → cluster 1. Always compare squared distances — same answer, half the work.

Problem 2 (NAT). A 4-feature dataset has covariance eigenvalues 8, 4, 2, 2. How much variance do the first two PCs explain, and what’s the minimum number of components for ≥ 85%?

Solution. Total = 16. First two: (8+4)/16 = 75%. Cumulative: 1 PC → 50%, 2 PCs → 75%, 3 PCs → 14/16 = 87.5% ≥ 85% → 3 components.

Problem 3 (NAT). Clusters A = {1, 4} and B = {7, 9} on a line. Single-linkage and complete-linkage distances?

Solution. Pairwise distances: |1−7| = 6, |1−9| = 8, |4−7| = 3, |4−9| = 5. Single linkage = min = 3; complete linkage = max = 8.

Common Mistakes to Avoid

Claiming k-means finds the global optimum. It converges, but to a local optimum that depends on initialisation — the classic true/false trap.

Updating centroids before finishing assignments. One full assign pass, then one full update pass. Interleaving gives wrong intermediate centroids in traced questions.

Forgetting to center (and usually standardise) before PCA. Uncentered PCA points PC1 at the data’s mean, not its spread.

Reporting variance explained from eigenvalues in the wrong order. Sort descending first; λ₁ is the largest.

Mixing linkage definitions. Single = closest pair, complete = farthest pair. Half of all dendrogram errors are this swap.

How GATE DA Asks Clustering and PCA

The recurring shapes: (1) NAT — one or two k-means iterations on ≤ 8 points, report a centroid coordinate or WCSS; (2) NAT — variance-explained percentages from given eigenvalues (nearly every year); (3) NAT/MCQ — linkage distances from a small distance matrix; (4) MCQ — properties: convergence, initialisation sensitivity, orthogonality of PCs, PCA-SVD relationship. All arithmetic is small integers — the marks are for procedure discipline.

Master the full ML syllabus for GATE DA 2027

My complete Machine Learning course covers clustering, PCA, regression, SVM, trees, naive Bayes and neural networks with recorded lectures, notes and GATE-level practice — aligned exactly to the DA syllabus.

Explore the Machine Learning Course →

FAQs: K-Means & PCA for GATE DA

Does k-means always converge?

Yes — each step can only decrease (or preserve) WCSS and there are finitely many partitions, so it terminates. But the endpoint is a local optimum, not necessarily the best clustering.

Is PCA supervised or unsupervised?

Unsupervised — it never looks at labels, only at the feature covariance. That’s also why maximum-variance directions aren’t guaranteed to be the most discriminative ones for a later classifier.

How is PCA related to SVD exactly?

PCA’s components are the right singular vectors of the centered data matrix, and each eigenvalue is σᵢ²/(n−1). The SVD deep dive works this machinery on numbers.

Can I use k-means for non-spherical clusters?

Poorly — WCSS with Euclidean distance favours round, similar-sized clusters. Elongated or nested shapes suit hierarchical (single linkage) or density-based methods better; GATE tests this as a “which method fails here?” MCQ.

What should I study next?

Neural networks — the last big block of the ML section. The ML pillar guide has the recommended order.

Keep building: revise the complete ML roadmap, work the eigen-machinery in the Linear Algebra pillar and the SVD & LU deep dive, and track your coverage against the GATE DA 2027 syllabus. New ML problem-solving sessions drop regularly on my YouTube channel — subscribe so you don’t miss them.

Share This Story, Choose Your Platform!
Join the PiyushAI AI & Data Science Community | Newsletter
📬 PiyushAI  ·  AI & Data Science Learning Community

Stay Ahead in AI, Data Science, Exams & Your Learning Journey

Join 20,000+ learners exploring AI & Data Science — GATE, Bank IT & PSU exam aspirants, IIT Madras BS Degree students, school teachers exploring the CBSE CT & AI curriculum, working professionals, and anyone starting their AI literacy journey. Tell us a little about yourself and get personalised updates, resources, and mentorship alerts — straight from Piyush Wairale.

🎯
Exam & Career Updates First
GATE, Bank IT Officer, PSU & Government job alerts — plus IIT Madras BS Degree guidance.
📚
Free Learning Resources
Study notes, PYQ analysis, practice questions & guides for exams, data science & AI.
🚀
AI Literacy & CBSE CT-AI
AI tools & concepts for everyone, CBSE CT & AI curriculum support for schools & teachers, plus early course access.
✍️ Join the Community — Fill the Form

Takes less than 60 seconds  •  No spam, only what helps you learn & grow

👨‍🎓 20,000+ Students
▶️ 44,000+ YouTube Subscribers
🎓 IIT Madras Alumnus Mentor

Recent Post

Connect with PiyushAI | YouTube & Telegram Community
🔗 Connect With Us

Learn Daily, Wherever You Are

Free lectures, exam updates, PYQ discussions, and job alerts — delivered through our YouTube channel and Telegram communities.

▶️
YouTube Channel
Piyush Wairale IITM
Free lectures on AI, Data Science, GATE preparation & exam strategy — trusted by 44,000+ subscribers.
Subscribe Now →
🌐
Official Website
piyushwairale.com
Complete courses, GATE DA test series, mock exams & structured preparation programs — all in one place.
Explore Courses →

Leave A Comment