K means and Hierarchial Clustering

 

K-means Clustering:

  1. Purpose: To partition a set of data points into K clusters, where each data point belongs to the cluster with the nearest mean.

  2. Process:

    • Initialization: Select K initial centroids randomly.

    • Assignment: Assign each data point to the nearest centroid.

    • Update: Recalculate the centroids as the mean of all data points in each cluster.

    • Repeat: Repeat the assignment and update steps until the centroids no longer change significantly.

Example:

Imagine you have data points representing the locations of several stores in a city, and you want to group them into clusters based on their proximity.

Key Parameters:

  • K: The number of clusters you want to form.

  • Centroids: The center of each cluster.

Applications:

  • Customer segmentation

  • Market basket analysis

  • Image compression


Intra-Segment Homogeneity:

  • Definition: Intra-segment homogeneity refers to the similarity of items within a single segment or cluster.

  • Goal: The aim is to ensure that the members of each segment are as similar to each other as possible.

  • Example: In a customer segmentation scenario, intra-segment homogeneity would mean that customers in the same segment have similar purchasing behaviors.

Inter-Segment Heterogeneity:

  • Definition: Inter-segment heterogeneity refers to the differences between different segments or clusters.

  • Goal: The goal is to ensure that the segments are distinct from one another.

  • Example: Continuing with the customer segmentation example, inter-segment heterogeneity would mean that customers in different segments have significantly different purchasing behaviors.

Importance in Clustering:

  • Balanced Clusters: Effective clustering should achieve high intra-segment homogeneity and high inter-segment heterogeneity. This means items within a cluster are very similar, while clusters themselves are very different from each other.

  • Applications: These concepts are crucial in various applications like market segmentation, image segmentation, and bioinformatics.


Visual Representation:

If you're a visual thinker, imagine clusters in a scatter plot:

  • Intra-Segment Homogeneity: Points within a cluster are close to each other.

  • Inter-Segment Heterogeneity: Clusters are well-separated from each other.


Great question! Distance metrics play a crucial role in K-means clustering as they determine how the similarity between data points is measured. Here are some commonly used distance metrics:

1. Euclidean Distance:

  • Definition: It measures the straight-line distance between two points in Euclidean space.

  • Formula: For two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2), $$ d = \sqrt{(x_2 - x_1)^2 + (y_2 - y_1)^2} $$

  • Properties: Simple to compute, but can be sensitive to differences in scale among features.

2. Manhattan Distance (L1 Distance):

  • Definition: It measures the distance between two points along axes at right angles.

  • Formula: For two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2), $$ d = |x_2 - x_1| + |y_2 - y_1| $$

  • Properties: Less sensitive to outliers compared to Euclidean distance.

3. Minkowski Distance:

  • Definition: Generalization of Euclidean and Manhattan distances.

  • Formula: For two points (x1,y1)(x_1, y_1) and (x2,y2)(x_2, y_2) with parameter pp, $$ d = \left( |x_2 - x_1|^p + |y_2 - y_1|^p \right)^{1/p} $$

  • Properties:

    • When p=1p = 1, it becomes Manhattan distance.

    • When p=2p = 2, it becomes Euclidean distance.

    • Flexible depending on the choice of pp.

4. Cosine Distance:

  • Definition: It measures the cosine of the angle between two vectors.

  • Formula: For vectors A\mathbf{A} and B\mathbf{B}, $$ \text{cosine\_distance} = 1 - \frac{\mathbf{A} \cdot \mathbf{B}}{\|\mathbf{A}\| \|\mathbf{B}\|} $$

  • Properties: Useful for high-dimensional data, particularly when the magnitude of the vectors is not important.

5. Hamming Distance:

  • Definition: It measures the number of positions at which the corresponding elements are different. Commonly used for categorical data.

  • Formula: For binary strings of equal length, it's the number of bit positions in which they differ.

  • Properties: Particularly useful in binary and categorical data.


Centroid:

  • Definition: A centroid is the center of a cluster. In K-means clustering, it is the mean position of all the points in that cluster.

  • Computation: For a set of data points within a cluster, the centroid is calculated as the average of the feature values of those data points. If you have nn data points and each point has mm features, the centroid for each feature dimension is: $$ \text{Centroid}i = \frac{1}{n} \sum{j=1}^n x_{ij} $$ where xijx_{ij} is the value of the ii-th feature of the jj-th data point.

Example:

Consider a 2D space with data points:

  • Point A: (1, 2)

  • Point B: (3, 4)

  • Point C: (5, 6)

The centroid of these points is calculated as: $$ \text{Centroid}_x = \frac{1 + 3 + 5}{3} = 3 $$ $$ \text{Centroid}_y = \frac{2 + 4 + 6}{3} = 4 $$ So, the centroid is at (3, 4).

Role in K-means:

  • Initialization: Initially, K-means selects K random centroids.

  • Assignment: Each data point is assigned to the nearest centroid based on a distance metric (usually Euclidean distance).

  • Update: The centroids are then recalculated as the mean of all data points assigned to each cluster.

  • Repeat: This process of assignment and update is repeated until the centroids no longer change significantly (convergence).


K-means++ is an enhanced version of the K-means clustering algorithm. Its primary goal is to improve the initialization of cluster centroids to achieve better clustering results and faster convergence. Let's explore how it works and why it's beneficial:

K-means++ Initialization:

  1. First Centroid:

    • Choose the first centroid randomly from the data points.

  2. Subsequent Centroids:

    • For each subsequent centroid, choose a data point with a probability proportional to its distance from the nearest existing centroid. This means that points farther from any already chosen centroid are more likely to be selected.

  3. Distance Calculation:

    • Compute the distance of each data point to the nearest existing centroid.

    • Select the next centroid with a probability proportional to the square of this distance.

Benefits of K-means++:

  • Better Initialization: By carefully selecting the initial centroids, K-means++ reduces the chances of poor clustering due to suboptimal starting points.

  • Faster Convergence: Since the initial centroids are chosen in a more informed manner, the algorithm typically converges faster and requires fewer iterations.

  • Higher Accuracy: The final clusters are generally of higher quality, with improved intra-segment homogeneity and inter-segment heterogeneity.

Steps in K-means++:

  1. Initialization: Use the K-means++ method to select K initial centroids.

  2. Assignment: Assign each data point to the nearest centroid.

  3. Update: Recalculate the centroids as the mean of the data points assigned to each cluster.

  4. Repeat: Repeat the assignment and update steps until the centroids no longer change significantly.


 the major practical considerations involved in K-Means clustering are:

  • The number of clusters that you want to divide your data points into, i.e. the value of K has to be pre-determined.

  • The choice of the initial cluster centres can have an impact on the final cluster formation.

  • The clustering process is very sensitive to the presence of outliers in the data.

  • Since the distance metric used in the clustering process is the Euclidean distance, you need to bring all your attributes on the same scale. This can be achieved through standardisation.

  • The K-Means algorithm does not work with categorical data.

  • The process may not converge in the given number of iterations. You should always check for convergence.


Evaluate the quality of clustering

1. Silhouette Metric:

  • Definition: The Silhouette score measures how similar a data point is to its own cluster compared to other clusters.

  • Formula: For a data point ii, the Silhouette score s(i)s(i) is calculated as: $$ s(i) = \frac{b(i) - a(i)}{\max(a(i), b(i))} $$ where:

    • a(i)a(i) is the average distance of ii to all other points in the same cluster.

    • b(i)b(i) is the average distance of ii to all points in the nearest different cluster.

  • Range: The Silhouette score ranges from -1 to 1:

    • A score close to 1 indicates that the data point is well-clustered.

    • A score close to 0 indicates that the data point is on or very close to the decision boundary between two clusters.

    • A score close to -1 indicates that the data point is in the wrong cluster.

  • Interpretation: The average Silhouette score of all data points provides an overall assessment of clustering quality. Higher scores indicate better-defined clusters.

2. Davies-Bouldin Index:

  • Definition: Measures the average similarity ratio of each cluster with the cluster that is most similar to it.

  • Formula: For a cluster ii, $$ DB = \frac{1}{N} \sum_{i=1}^{N} \max_{j \neq i} \left( \frac{s_i + s_j}{d_{ij}} \right) $$ where:

    • sis_i and sjs_j are the average distances of points in clusters ii and jj to their respective centroids.

    • dijd_{ij} is the distance between the centroids of clusters ii and jj.

  • Range: Lower values indicate better clustering.

3. Calinski-Harabasz Index (Variance Ratio Criterion):

  • Definition: Measures the ratio of the sum of between-cluster dispersion and within-cluster dispersion.

  • Formula: For a dataset with NN points, kk clusters, and centroids cc, $$ CH = \frac{\sum_{i=1}^{k} n_i \|c_i - c\|2}{\sum_{i=1}{k} \sum_{x \in C_i} \|x - c_i\|^2} \cdot \frac{N - k}{k - 1} $$ where:

    • nin_i is the number of points in cluster ii.

    • cic_i is the centroid of cluster ii.

    • cc is the overall centroid of the dataset.

  • Range: Higher values indicate better-defined clusters.

4. Within-Cluster Sum of Squares (WCSS):

  • Definition: Measures the total variance within each cluster.

  • Formula: For a cluster CC with centroid cc, $$ WCSS = \sum_{i=1}^{k} \sum_{x \in C_i} \|x - c_i\|^2 $$

    • Range: Lower values indicate more compact clusters.

Summary Table:

MetricDefinitionIdeal Value
Silhouette ScoreSimilarity measure within clustersClose to 1
Davies-Bouldin IndexSimilarity between clustersClose to 0
Calinski-Harabasz IndexBetween/within cluster varianceHigher the better
WCSSTotal variance within clustersLower the better


Cluster tendency refers to the inherent propensity of a dataset to form meaningful clusters. Before applying a clustering algorithm like K-means, it's helpful to determine whether the data actually has a natural clustering structure. Here are some methods and techniques to assess cluster tendency:

1. Visual Inspection:

  • Scatter Plots: For small datasets with 2 or 3 features, scatter plots can visually reveal clustering tendencies.

  • Dimensionality Reduction: Techniques like PCA (Principal Component Analysis) or t-SNE (t-distributed Stochastic Neighbor Embedding) can help visualize high-dimensional data in 2D or 3D space.

2. Hopkins Statistic:

  • Definition: Measures the probability that a dataset is uniformly distributed (i.e., has no clusters).

  • Calculation:

    • Randomly select a subset of points.

    • Generate a random set of points in the same space.

    • Calculate the distance of each selected point to its nearest neighbor in the original dataset.

    • Calculate the distance of each random point to its nearest neighbor in the original dataset.

    • The Hopkins statistic is the ratio of the sum of nearest-neighbor distances of random points to the sum of nearest-neighbor distances of selected points.

  • Interpretation:

    • Values close to 0.5 indicate a uniform distribution (no cluster tendency).

    • Values close to 1 indicate a strong cluster tendency.

3. Gap Statistic:

  • Definition: Compares the total within-cluster variation for different numbers of clusters with their expected values under a null reference distribution of the data.

  • Calculation:

    • Compute the within-cluster dispersion for various numbers of clusters.

    • Generate reference datasets by sampling from a uniform distribution.

    • Compute the within-cluster dispersion for the reference datasets.

    • The gap statistic is the difference between the logarithm of the within-cluster dispersion for the data and the average logarithm of the within-cluster dispersion for the reference datasets.

  • Interpretation: The optimal number of clusters is the value that maximizes the gap statistic.

4. Clustering Tendency Plot (VAT - Visual Assessment of cluster Tendency):

  • Definition: Visual tool to assess the presence of clusters in the data.

  • Plot:

    • Create a dissimilarity matrix of the data.

    • Reorder the matrix using a hierarchical clustering method.

    • Visualize the reordered matrix as a heatmap.

  • Interpretation: Diagonal blocks in the heatmap indicate the presence of clusters.


RFM (Recency, Frequency, Monetary) analysis is a marketing technique used to segment customers based on their purchasing behavior. When combined with K-means clustering, RFM analysis can be a powerful tool for customer segmentation. Let's break down how it works:

RFM Analysis:

  • Recency (R): How recently a customer made a purchase.

  • Frequency (F): How often a customer makes a purchase.

  • Monetary (M): How much money a customer spends.

Steps to Perform RFM Analysis with K-means:

  1. Data Preparation:

    • Gather data on customer transactions.

    • Calculate the RFM scores for each customer.

  2. Standardization:

    • Normalize the R, F, and M values to bring them to a similar scale.

  3. Applying K-means Clustering:

    • Determine the optimal number of clusters (K) using methods like the Elbow Method, Silhouette Score, or Gap Statistic.

    • Apply the K-means algorithm to the standardized RFM data.

    • Assign each customer to a cluster based on their RFM values.

  4. Interpretation:

    • Analyze the characteristics of each cluster to understand customer segments.

    • Typical segments could include "High-value customers," "Recent buyers," "Frequent buyers," etc.

Example:

Imagine you have the following RFM data for three customers:

  • Customer A: Recency = 10 days, Frequency = 5, Monetary = $200

  • Customer B: Recency = 20 days, Frequency = 3, Monetary = $150

  • Customer C: Recency = 5 days, Frequency = 7, Monetary = $300

Steps:

  1. Normalization: Convert RFM values to a common scale (e.g., between 0 and 1).

  2. Clustering: Apply K-means clustering to the normalized data.

  3. Segmentation: Interpret the clusters to identify customer segments.

Benefits:

  • Targeted Marketing: Identify specific segments for tailored marketing campaigns.

  • Customer Retention: Understand the behavior of different customer groups and develop retention strategies.

  • Resource Allocation: Allocate marketing resources more effectively based on customer value.


RPI Segmentation:

  • Recency: How recently an event or interaction occurred.

  • Participation: How often a user engages or participates.

  • Intensity: The level of involvement or engagement intensity.

CDJ Segmentation (Consumer Decision Journey):

  • Stages: Typically includes Awareness, Consideration, Purchase, Retention, and Advocacy.

  • Behavior Analysis: Segments customers based on their behavior at each stage of the journey.


RPI: Used in community management, engagement analysis, and participation tracking.
CDJ: Applied in marketing strategies, customer journey optimization, and personalized messaging.



Hierarchical clustering 

another popular clustering technique in machine learning. Unlike K-means, hierarchical clustering creates a hierarchy of clusters that can be visualized using a tree-like structure called a dendrogram. Here's an overview of hierarchical clustering:

Types of Hierarchical Clustering:

  1. Agglomerative (Bottom-Up):

    • Start with each data point as its own cluster.

    • Iteratively merge the closest clusters until only one cluster remains or the desired number of clusters is achieved.

  2. Divisive (Top-Down):

    • Start with all data points in one cluster.

    • Iteratively split the clusters until each data point is its own cluster or the desired number of clusters is achieved.

Key Steps in Agglomerative Hierarchical Clustering:

  1. Calculate Distance Matrix:

    • Compute the distance between every pair of data points using a distance metric (e.g., Euclidean, Manhattan).

  2. Merge Clusters:

    • Find the pair of clusters with the smallest distance.

    • Merge them into a single cluster.

    • Update the distance matrix to reflect the merging.

  3. Repeat:

    • Repeat the merging process until the desired number of clusters is achieved.

Linkage Criteria:

The method used to determine the distance between clusters in hierarchical clustering. Common linkage methods include:

  • Single Linkage (Minimum Linkage): Distance between the closest points of two clusters.

  • Complete Linkage (Maximum Linkage): Distance between the farthest points of two clusters.

  • Average Linkage: Average distance between all pairs of points in two clusters.

  • Ward's Method: Minimizes the increase in total within-cluster variance when two clusters are merged.

Dendrogram:

  • A dendrogram is a tree-like diagram that shows the arrangement of the clusters formed by hierarchical clustering.

  • The x-axis represents the data points, and the y-axis represents the distance at which clusters are merged.

  • By cutting the dendrogram at a specific level, you can determine the number of clusters.

Example:

Suppose you have a dataset with five data points:

  • (1, 2), (2, 3), (3, 4), (8, 8), (9, 9)

Steps in Agglomerative Clustering:

  1. Calculate the distance matrix.

  2. Merge the closest points (e.g., (1, 2) and (2, 3)).

  3. Update the distance matrix and merge the next closest clusters.

  4. Continue until only one cluster remains.

Advantages:

  • No need to specify the number of clusters in advance.

  • Can capture complex hierarchical structures.

Disadvantages:

  • Computationally intensive for large datasets.

  • Sensitive to noise and outliers.

Hierarchical clustering is particularly useful when you want to explore the data's underlying structure and visualize the relationships between clusters.



Types of Linkages

1. Single Linkage (Minimum Linkage):

  • Definition: Measures the distance between the closest points (or the shortest distance) of two clusters.

  • Characteristic: Tends to form elongated and "chain-like" clusters.

  • Use Case: Useful when detecting elongated clusters or when clusters are expected to form a continuum.

2. Complete Linkage (Maximum Linkage):

  • Definition: Measures the distance between the farthest points (or the longest distance) of two clusters.

  • Characteristic: Tends to create more compact and spherical clusters.

  • Use Case: Suitable when tight and clearly separated clusters are desired.

3. Average Linkage:

  • Definition: Measures the average distance between all pairs of points in two clusters.

  • Characteristic: Balances between single and complete linkage, forming moderately compact clusters.

  • Use Case: Useful when clusters of moderate compactness and separation are needed.

4. Centroid Linkage:

  • Definition: Measures the distance between the centroids (mean points) of two clusters.

  • Characteristic: Can create clusters with varying shapes and sizes.

  • Use Case: Effective when clusters are expected to have different shapes and densities.

5. Ward's Method:

  • Definition: Minimizes the increase in total within-cluster variance when merging clusters.

  • Characteristic: Tends to create clusters of roughly equal size with minimal variance.

  • Use Case: Useful when clusters are expected to be compact and similar in size.

Comparison Table:

Linkage TypeDefinitionCluster Characteristics
Single LinkageShortest distance between clustersElongated, chain-like clusters
Complete LinkageLongest distance between clustersCompact, spherical clusters
Average LinkageAverage distance between clustersModerately compact clusters
Centroid LinkageDistance between cluster centroidsVaries in shape and size
Ward's MethodMinimizes within-cluster varianceCompact, equal-sized clusters

K modes clustering

K-modes clustering is an extension of the K-means algorithm specifically designed to handle categorical data. While K-means works well with numerical data, K-modes is better suited for clustering categorical attributes. Here’s a breakdown of K-modes clustering:

Key Concepts:

  1. Distance Measure: Instead of using Euclidean distance, K-modes uses a simple matching dissimilarity measure. The distance between two categorical data points is the number of mismatches in corresponding categories. $$ d(X, Y) = \sum_{i=1}^{m} \delta(x_i, y_i) $$ where δ(xi,yi)\delta(x_i, y_i) is 0 if xi=yix_i = y_i and 1 otherwise.

  2. Centroids: The centroid of a cluster in K-modes is defined using the mode of the categorical attributes, rather than the mean. The mode is the most frequent category for each attribute within the cluster.

Algorithm Steps:

  1. Initialization:

    • Randomly select K initial modes (centroids) from the dataset.

  2. Assignment:

    • Assign each data point to the nearest mode based on the matching dissimilarity measure.

  3. Update:

    • Recalculate the modes for each cluster. The new mode for a cluster is determined by the most frequent category for each attribute.

  4. Repeat:

    • Repeat the assignment and update steps until the modes no longer change significantly or a maximum number of iterations is reached.

Example:

Consider a dataset with the following categorical attributes:

  • Data points: (Red,High),(Blue,Low),(Red,Medium),(Blue,High)(Red, High), (Blue, Low), (Red, Medium), (Blue, High)

  1. Initialization: Select initial modes, e.g., (Red,High)(Red, High) and (Blue,Low)(Blue, Low).

  2. Assignment: Assign each data point to the nearest mode.

  3. Update: Recalculate the modes based on the assigned points.

  4. Repeat: Continue the assignment and update steps until convergence.

Advantages:

  • Handles Categorical Data: Specifically designed for clustering categorical variables.

  • Interpretable Results: Clusters are easily interpretable based on the modes of the attributes.

Disadvantages:

  • Choice of K: Like K-means, determining the optimal number of clusters (K) can be challenging.

  • Initialization Sensitivity: The initial choice of modes can impact the final clusters.

Applications:

  • Market Segmentation: Grouping customers based on categorical attributes like preferences or behaviors.

  • Bioinformatics: Clustering genetic data with categorical attributes.




Comments

Popular posts from this blog

Resume Work and Project Details

Time Series and MMM basics

LINEAR REGRESSION