← Writing & notes

FROM THE NOTEBOOK

Machine Learning Algorithms 2: Unsupervised Learning

Unsupervised learning is a type of machine learning where models are trained on unlabeled data. The system tries to learn the inherent patterns, structures, and relationships from the data without any explicit guidance or correct answers.

The primary goal of unsupervised learning is to explore the data and find meaningful representations. This is often used for:

Unsupervised learning tasks can be broadly grouped into several categories:

Covered Techniques

This document covers the following key unsupervised learning techniques:


Clustering

Clustering is the task of partitioning a set of objects into groups (called clusters) such that objects in the same cluster are more similar to each other than to those in other clusters.

k-Means Clustering

k-Means is one of the most fundamental and widely-used unsupervised algorithms. It is a centroid-based clustering algorithm that aims to partition n observations into k clusters, where each observation belongs to the cluster with the nearest mean (cluster centroid).

Mathematical Framework

The objective of k-Means is to minimize the within-cluster sum of squares (WCSS), also known as inertia: J = Σ(from j=1 to k) Σ(for x in Cⱼ) ||x - μⱼ||²

This is an NP-hard problem, so k-Means uses an iterative heuristic called Lloyd's algorithm to find a local minimum.

Algorithm Steps

  1. Initialization: Choose k initial centroids. A smart initialization like k-means++ is often used to improve convergence and results.
  2. Assignment Step: Assign each data point to the cluster of its nearest centroid.
  3. Update Step: Recalculate the centroid for each cluster as the mean of all data points assigned to it.
  4. Convergence: Repeat the assignment and update steps until the centroids no longer move or the assignments no longer change.

Determining the Optimal k

Since k must be specified beforehand, several methods are used to find an optimal value:

Advantages

Limitations


Hierarchical Clustering

Hierarchical clustering builds a tree-like hierarchy of clusters called a dendrogram. This method does not require the number of clusters to be specified in advance and allows for the exploration of data structure at multiple levels of granularity.

There are two main approaches:

  1. Agglomerative (Bottom-Up): Starts with each data point as its own cluster and iteratively merges the closest pair of clusters until only one cluster remains.
  2. Divisive (Top-Down): Starts with all data points in a single cluster and recursively splits them until each data point is its own cluster. Agglomerative is more common.

Mathematical Framework (Agglomerative)

The key component is the linkage criterion, which defines the distance between two clusters.

The Dendrogram

The output is a dendrogram, which visualizes the merging process. The height of the merge in the dendrogram represents the distance at which the clusters were combined. By cutting the dendrogram at a certain height, a specific number of clusters can be obtained.

Advantages

Limitations


DBSCAN (Density-Based Spatial Clustering of Applications with Noise)

DBSCAN is a density-based clustering algorithm that groups together points that are closely packed together, marking as outliers points that lie alone in low-density regions. It is particularly effective at discovering clusters of arbitrary shapes and handling noise.

Mathematical Framework

DBSCAN is defined by two key parameters:

Based on these parameters, points are classified into three types:

  1. Core Point: A point that has at least MinPts neighbors (including itself) within its ε-neighborhood.
  2. Border Point: A point that is not a core point but is in the ε-neighborhood of a core point.
  3. Noise Point (Outlier): A point that is neither a core nor a border point.

Cluster Definition

A cluster is formed by a set of density-connected points. Two points are density-connected if they are linked by a path of core points, where each step in the path is within an ε-distance. A cluster consists of all core points and all border points that are reachable from them.

Advantages

Limitations


Gaussian Mixture Models (GMM)

Gaussian Mixture Models are a probabilistic, model-based clustering algorithm. GMM assumes that the data points are generated from a mixture of a finite number of Gaussian distributions with unknown parameters. This allows for soft clustering, where each data point has a probability of belonging to each cluster.

Mathematical Framework

The model represents the probability distribution of the data as a weighted sum of K Gaussian components: p(x) = Σ(from k=1 to K) πₖ * N(x | μₖ, Σₖ)

Expectation-Maximization (EM) Algorithm

The parameters (πₖ, μₖ, Σₖ) are learned using the Expectation-Maximization (EM) algorithm, an iterative process for finding maximum likelihood estimates in models with latent variables.

  1. Initialization: Initialize the parameters, often using the results of k-Means.
  2. E-Step (Expectation): For each data point, calculate the posterior probability (responsibility) that it belongs to each component k. This is the "soft" assignment.
  3. M-Step (Maximization): Update the model parameters (πₖ, μₖ, Σₖ) using the responsibilities calculated in the E-step.
  4. Convergence: Repeat the E and M steps until the log-likelihood of the data converges.

Advantages

Limitations


Dimensionality Reduction

Dimensionality reduction is the process of reducing the number of input variables in a dataset. This is important for visualizing high-dimensional data, reducing computational cost, and mitigating the "curse of dimensionality."

Principal Component Analysis (PCA)

PCA is a linear dimensionality reduction technique that transforms the data into a new coordinate system. The new axes (principal components) are orthogonal and are ordered by the amount of variance they explain in the data. The first principal component captures the most variance, the second captures the second most, and so on.

Mathematical Framework

PCA finds the principal components by performing an eigenvalue decomposition on the covariance matrix of the data. The eigenvectors of the covariance matrix are the principal components, and the corresponding eigenvalues represent the amount of variance captured by each component.

Alternatively, PCA can be computed using Singular Value Decomposition (SVD) of the centered data matrix, which is often more numerically stable.

Key Steps

  1. Standardize the data: It is crucial to scale the features to a common range.
  2. Compute the covariance matrix: Calculate the covariance between all pairs of features.
  3. Compute eigenvectors and eigenvalues: Find the principal components and their corresponding variance.
  4. Select principal components: Choose the top k eigenvectors that capture a desired amount of variance (e.g., 95%).
  5. Project the data: Transform the original data onto the new, lower-dimensional subspace defined by the selected components.

Advantages

Limitations


t-Distributed Stochastic Neighbor Embedding (t-SNE)

t-SNE is a non-linear dimensionality reduction technique that is particularly well-suited for visualizing high-dimensional datasets in 2D or 3D. It models the similarity between high-dimensional data points as a probability distribution and then optimizes a similar distribution in the low-dimensional space.

Mathematical Framework

  1. High-Dimensional Similarities: t-SNE converts the high-dimensional Euclidean distances between data points into conditional probabilities that represent similarities. It uses a Gaussian distribution centered on each point to model these probabilities.
  2. Low-Dimensional Similarities: It models the similarities between the corresponding low-dimensional points using a Student's t-distribution with one degree of freedom. The heavy tails of the t-distribution allow dissimilar points to be modeled far apart in the low-dimensional map, helping to alleviate the "crowding problem."
  3. Optimization: The algorithm minimizes the Kullback-Leibler (KL) divergence between the two distributions of pairwise similarities using gradient descent, effectively arranging the points in the low-dimensional space to best represent the high-dimensional structure.

Key Hyperparameter: Perplexity

Perplexity is related to the number of nearest neighbors that are considered for each point. Typical values are between 5 and 50. It influences the balance between preserving local and global structure.

Advantages

Limitations


Association Rule Learning

Association rule learning is a rule-based machine learning method for discovering interesting relationships between variables in large datasets. The most common application is Market Basket Analysis, which aims to find associations between products frequently purchased together.

Key Metrics

Apriori Algorithm

Apriori is a classic algorithm for mining frequent itemsets and learning association rules. It is based on the Apriori property: "All non-empty subsets of a frequent itemset must also be frequent."

How it works

  1. Set a minimum support threshold.
  2. Find all itemsets with one item that meet the threshold (frequent 1-itemsets).
  3. Iteratively generate candidate k-itemsets from frequent (k-1)-itemsets.
  4. Prune the candidates by checking if all their subsets are frequent (the Apriori property).
  5. Repeat until no more frequent itemsets are found.
  6. Generate rules from the final frequent itemsets that meet a minimum confidence threshold.

FP-Growth Algorithm

The FP-Growth (Frequent Pattern Growth) algorithm is an efficient alternative to Apriori. It avoids the costly candidate generation step by constructing a compact, tree-based data structure called an FP-tree.

How it works

  1. It scans the database once to find the frequent 1-itemsets and orders them.
  2. It scans the database a second time to construct the FP-tree, which stores the transaction data in a compressed form.
  3. It extracts frequent itemsets directly from the FP-tree by recursively mining it.

FP-Growth is significantly faster than Apriori, especially for large datasets.

All writing