Skip to main content
Workforce LibreTexts

10.3.5: Clustering Techniques (K-Means and Hierarchical Clustering)

  • Page ID
    53370
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\dsum}{\displaystyle\sum\limits} \)

    \( \newcommand{\dint}{\displaystyle\int\limits} \)

    \( \newcommand{\dlim}{\displaystyle\lim\limits} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

    ( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\kernel}{\mathrm{null}\,}\)

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \(\newcommand{\longvect}{\overrightarrow}\)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \(\newcommand{\ket}[1]{\left| #1 \right>}\)
    \(\newcommand{\bra}[1]{\left< #1 \right|}\)
    \(\newcommand{\braket}[2]{\left< #1 \vphantom{#2} \right| \left. #2 \vphantom{#1} \right>}\)
    \(\newcommand{\braopket}[3]{\left< #1 \vphantom{#2}\vphantom{#3} \right| #2 \vphantom{#1}\vphantom{#3} \left| #3 \vphantom{#1}\vphantom{#2} \right>}\)
    \(\newcommand{\qmvec}[1]{\mathbf{\vec{#1}}}\)
    \(\newcommand{\op}[1]{\hat{\mathbf{#1}}}\)
    \(\newcommand{\expect}[1]{\langle #1 \rangle}\)
    \(\newcommand{\dfn}[1]{\emph{\textbf{#1}}}\)

    \(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)

    Clustering Techniques (K-Means and Hierarchical Clustering)

    Clustering algorithms are unsupervised methods aimed at discovering natural groupings in data. They play a different role in predictive analytics: rather than directly predicting an outcome, they help reveal structure or segments in data which can then inform predictive models or business actions (for instance, clustering customers into segments and then treating each segment differently in a marketing strategy). We will discuss two common clustering techniques: K-Means and Hierarchical Clustering.

    K-Means Clustering: K-Means is one of the simplest and most popular clustering algorithms. The goal of K-Means is to partition the dataset into K clusters, where each data point belongs to the cluster with the nearest mean (centroid). The algorithm works iteratively: 1. Choose K initial centroids (often randomly selected data points or based on some heuristic). 2. Assign each data point to the nearest centroid (usually using Euclidean distance). 3. Recompute the centroid of each cluster as the mean of all points assigned to it. 4. Repeat the assign-and-update-centroids steps until assignments no longer change (or change very little), indicating convergence.

    The result is K clusters defined by their centroids – effectively, we have segmented the data such that each point is closer to its own cluster’s center than to any other center. The user must specify K (the number of clusters) in advance, which is a drawback when the optimal number of clusters is unknown – one often runs the algorithm for different K values and evaluates using metrics like the elbow method or silhouette score. K-Means assumes clusters are roughly spherical and balanced in size; it may struggle with clusters of irregular shapes or widely differing sizes.

    In the figure, K-Means successfully discovered the 3 underlying clusters in the data. Clustering has many practical uses in analytics: grouping similar customers, finding communities in social networks, segmenting images, detecting anomalies (points that don’t fit any cluster well), etc. It’s often an early exploratory step – by clustering, one can identify patterns or homogeneous groups in data which might warrant separate predictive models or targeted policies.

    Hierarchical Clustering: Hierarchical clustering builds a hierarchy of clusters in a tree-like structure (often visualized as a dendrogram). There are two main approaches: - Agglomerative (bottom-up): Start with each data point as its own cluster. Then, iteratively merge the two closest clusters until eventually all points are in one cluster. This creates a nested hierarchy of clusters from fine-grained to one big cluster. - Divisive (top-down): Start with all points in one cluster and recursively split clusters until each data point is alone.

    Agglomerative clustering is more common. The user does not have to pre-specify the number of clusters K; instead, one can decide how far to cut the dendrogram to get a desired number of clusters or use a distance threshold. Hierarchical clustering uses a linkage criterion to decide which clusters to merge – e.g., single linkage (merge clusters with the closest pair of points), complete linkage (merge clusters based on the farthest pair of points distance), average linkage, etc. Each choice can lead to different cluster shapes and characteristics.

    Hierarchical clustering is computationally more expensive than K-Means (especially for large datasets) but has the advantage of providing a multi-resolution view of the data grouping. For example, one might observe that at a broad level data separates into 2 clusters, but each of those can be further divided into sub-clusters, etc., which can be insightful. Hierarchical methods can capture non-spherical cluster shapes better than K-Means in many cases. The dendrogram also allows analysts to decide an appropriate number of clusters by seeing at what point further merging becomes less sensible.

    In practice, clustering results are often evaluated qualitatively (since we usually don’t have “true labels” for clusters in unsupervised learning). Nonetheless, metrics like silhouette score or Davies-Bouldin index provide numerical ways to gauge clustering quality. It’s also common to incorporate domain knowledge – e.g., in customer segmentation, clusters should ideally align with meaningful differences in customer behavior that a business can act on.

    Python Implementation – K-Means Example: Using scikit-learn for K-Means clustering:

    from sklearn.cluster import KMeans
    kmeans = KMeans(n_clusters=3, random_state=42)
    kmeans.fit(X)               # Unsupervised training on data X
    labels = kmeans.labels_     # Cluster labels for each point
    centers = kmeans.cluster_centers_

    After fitting, labels contains the cluster index (0, 1, ..., K-1) for each data sample, and cluster_centers_ gives the coordinates of the final centroids. One could then analyze these clusters (e.g., by examining mean feature values per cluster, or visualizing as we did above if data is low-dimensional).

    For hierarchical clustering, scikit-learn offers AgglomerativeClustering in sklearn.cluster, or one can use SciPy’s linkage and dendrogram functions to perform and visualize hierarchical clustering.

    In summary, clustering techniques like K-Means and hierarchical clustering are invaluable for exploratory data analysis within predictive analytics. They help uncover structure in data that may not be immediately apparent, guiding further analysis or model development. While clustering itself is unsupervised and doesn’t “predict” a specific outcome, it often sets the stage for predictive tasks (for example, cluster first, then build a classifier within each cluster) and helps inform better feature engineering and understanding of the data landscape.


    This page titled 10.3.5: Clustering Techniques (K-Means and Hierarchical Clustering) was last modified on Wed, 10 Jun 2026 22:00:28 GMT and is shared under a CC BY 4.0 license and was authored, remixed, and/or curated by Felix Amoruwa.

    • Was this article helpful?