Docsity

PCA e LDA: Tecniche di Riduzione della Dimensionalit - Prof. Cumani, Appunti di Machine learning

Toggle sidebar
bg1
Dimensionality Reduction
Author
Marc'Antonio Lopez
AI & Data Analytics student at Polytechnic University of Turin
What is Dimensionality Reduction?
Dimensionality reduction transforms data from a high-dimensional feature space (
n dimensions)
into a new, lower-dimensional space (
m dimensions), where
m is significantly smaller than
n
(
m << n ). Essentially, it reduces the number of variables or features representing the data.
Here's a conceptual overview of this transformation:
High-Dimensional Data
(n features)
Dimensionality Reduction
Process
Lower-Dimensional Data
(m features)
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff

Anteprima parziale del testo

Scarica PCA e LDA: Tecniche di Riduzione della Dimensionalit - Prof. Cumani e più Appunti in PDF di Machine learning solo su Docsity!

Dimensionality Reduction

Author Marc'Antonio Lopez AI & Data Analytics student at Polytechnic University of Turin

What is Dimensionality Reduction?

Dimensionality reduction transforms data from a high-dimensional feature space ( n dimensions) into a new, lower-dimensional space ( m dimensions), where m is significantly smaller than n ( m << n ). Essentially, it reduces the number of variables or features representing the data.

Here's a conceptual overview of this transformation:

High-Dimensional Data (n features)

Dimensionality Reduction Process

Lower-Dimensional Data (m features)

This process serves several important purposes:

Compress Information : It significantly reduces data storage and processing time, leading to more efficient algorithms and better memory usage. Remove Unwanted Variability (Noise) : By focusing on significant data patterns, it helps filter out irrelevant details and noise, thus cleaning the data. Simplify Classification : It mitigates the "curse of high dimensionality" (sparse data, model overfitting), resulting in simpler, more robust classification models. Data Visualization : It allows complex, high-dimensional data to be projected into understandable 2D or 3D spaces, enabling visual inspection of inherent data structure and relationships.

Goals of Different Approaches

Goal Objective Method

Compress Information

To retain the maximum amount of essential information while significantly reducing the output data size.

Principal Component Analysis (PCA) : This method identifies and preserves the directions in the data that show the greatest variance (spread).

Improve Classification

To retain the information that best discriminates (separates) between different classes or categories.

Supervised techniques like Linear Discriminant Analysis (LDA) : These methods aim to maximize the separation between distinct classes in the data.

Data Visualization

To create clear 2D or 3D representations that accurately preserve the relationships between data samples.

PCA or t-distributed Stochastic Neighbor Embedding (t-SNE) : These methods are designed to maintain the proximity of original data samples when projected into lower dimensions, making their relationships visually apparent.

Reconstruction

Decomposes into Decomposes into

Matrix A (n x n, symmetric)

V (n x n) Orthogonal matrix Columns are eigenvectors

Σ (n x n) Diagonal matrix Elements are eigenvalues

Transpose of V^ Vᵀ^ (n x n)^ Multiply

Multiply

A = VΣVᵀ

Singular Value Decomposition (SVD)

Singular Value Decomposition (SVD) is a more general matrix decomposition applicable to any rectangular n x m matrix A , taking the form:.

SVD's components are:

U : An n x n orthogonal matrix whose columns are the eigenvectors of AA ᵀ. V : An m x m orthogonal matrix whose columns are the eigenvectors of A ᵀ A. Σ (Sigma): An (^) n x m rectangular diagonal matrix containing (^) A 's non-negative singular values , conventionally listed in decreasing order.

Here's a diagram illustrating the SVD process:

A = U Σ V T

Reconstruction

SVD Decomposition

Matrix A (n x m)

U (n x n) Orthogonal Eigenvectors of AAᵀ

Σ (n x m) Diagonal Singular Values

Vᵀ (m x m) Orthogonal (Eigenvectors of AᵀA)ᵀ

Multiply

Multiply

A = UΣVᵀ

SVD Diagrammatic Representation

The structure of the (^) Σ matrix in SVD adapts based on the dimensions of the input matrix (^) A :

Case 1: n > m (Tall Matrix) : When the number of rows ( n ) is greater than the number of columns ( (^) m ), the (^) Σ matrix will be padded with additional rows of zeros.

SVD for A (n x m) where n < m

=

Structure of Σ (n rows, m columns) σ₁ σ₂ ... σₙ

0 0 ... 0 (m-n columns of zeros)

A n x m

U n x n

Σ n x m

VT m x m

V m x m

Projecting a Point onto a Direction

To project a data point (^) x onto a direction defined by a unit vector (^) u ( ):

  1. Projection (Scalar Value) : y = uᵀx is the scalar coordinate of x along u , indicating x 's extent in that direction.
  2. Reconstruction (Point on Line) : (^) x̂ = y ⋅ u = (uᵀx) u is the point on (^) u 's line closest to original x , representing x projected onto that direction.

Here's a diagram illustrating the projection process:

∣∣ u ∣∣ = 1

Projection onto a Direction

Project onto u

Reconstruct in original space

Original Point (x)

Scalar Projection (y = uᵀx)

Reconstructed Point (x̂ = y * u)

Direction (u) ||u||=

Projecting a Point onto a Subspace

To project a data point (^) x onto an (^) m -dimensional subspace defined by orthonormal basis vectors :

  1. Projection (Vector of Coordinates) : y = Uᵀx provides x 's coordinates in the new m - dimensional basis U. Each element of y is x 's projection onto a uᵢ.
  2. Reconstruction (Point in Subspace) : x̂ = U y = U Uᵀ x is the best linear combination of u₁, ..., uₘ approximating^ x , representing the point in the^ m -dimensional subspace closest to original x. If m=n (subspace spans original space) and U is full rank, x̂ is identical to x.

U = [ u 1 , ..., u m ]

  1. Form Projection Matrix P : Select the first m columns from the sorted eigenvector matrix U to form P , representing your m principal components.
  2. Project the Data : Transform each centered data point zᵢ into the new m -dimensional space via . The resulting yᵢ is xᵢ 's m -dimensional representation.
  3. (Optional) Reconstruct the Data : To assess how well m components capture original data, reconstruct an approximation:. (Add back the mean, as yᵢ was from centered data).

Practical Considerations for PCA

Non-Zero-Mean Data : Importance of Centering : PCA axes always pass through the origin. Applying PCA to non- centered data may cause the first principal component to capture overall data location rather than internal spread. Always center data by subtracting the mean before PCA. This ensures principal components truly represent directions of maximum variance within the data's inherent structure. Interpreting PCA as Preserving Variance : Directions of Highest Variance : PCA identifies and preserves directions of maximal data variability (spread). Covariance Ellipse : In a data cloud, the axes of its descriptive ellipse align with principal directions (eigenvectors). Their lengths are proportional to standard deviation (square root of eigenvalues) in those directions. Decorrelation : Projecting data onto Uᵀ (eigenvector transpose) rotates it, aligning principal directions with coordinate axes. This beneficially makes new features (principal components) uncorrelated. How to Select (^) m (Number of Components) : Two primary methods exist for selecting m (number of components): i. Cross-Validation (for Supervised Tasks) : When PCA preprocesses supervised learning (e.g., classification), determine optimal m via cross-validation by testing values and selecting m yielding best classifier performance on a separate validation set. ii. Retaining a Percentage of Variance : A common heuristic selects the smallest (^) m retaining a target percentage ( t , e.g., 0.95) of total data variance. This is calculated as: , where σᵢ are descending-sorted eigenvalues. PCA for Large Feature Spaces : Computing the full covariance matrix C is computationally expensive and memory-intensive for datasets with many features. To address this, alternative methods include:

y i = P T^ z i = P T^ ( x i − x ˉ)

x ^ i = P y i + x ˉ

(Σ im^ σ i )/(Σ in^ σ i ) ≥ t

Truncated Singular Value Decomposition (SVD) : This method efficiently computes only the top m singular values and corresponding vectors (directly related to principal components), rather than all. Probabilistic PCA : A PCA variant formulated within a probabilistic framework, offering more robustness and missing data handling in certain scenarios.

PCA Example: MNIST Dataset

The MNIST dataset is a classic machine learning benchmark, with these properties:

Property Description

Task Classification of handwritten digits

Classes Digits 0, 1, ..., 9 (a total of 10 classes)

Image Shape 28x28 pixels (images are normalized and centered)

Image Type Gray-scale

Training Samples 60,000 images (approximately 6,000 for each digit)

Test Samples 10,000 images

MNIST Reconstruction with PCA : When PCA is applied to 28x28 (784-dimensional) MNIST images: 50-dimensional PCA (784 to 50 features) yields very clear digit reconstructions, showing essential information retention despite significant reduction. 10-dimensional PCA (to 10 features) produces blurrier but recognizable digits, highlighting PCA's ability for significant information compression while preserving key visual characteristics. PCA as Pre-processing for a Simple Classifier (Euclidean Distance) : A simple Euclidean distance classifier was tested on MNIST, with and without PCA preprocessing. Error rates are shown below:

Dimensions ( (^) m ) PCA Error Rate PCA+LDA Error Rate

Full (w/o PCA) 18.0% -

100 18.1% - 50 18.2% -

Fisher's LDA Criterion

Fisher's LDA aims to find a projection vector w maximizing a ratio that compares between-class variability to within-class variability for projected data.

The criterion to maximize is:

Between-Class Variability ( ) : Quantifies class mean separation after projection onto w. is the between-class scatter matrix , essentially the covariance of class means. Within-Class Variability ( ) : Quantifies data spread within each class after projection onto w. is the within-class scatter matrix , a weighted average of individual class covariance matrices.

Maximizing this ratio ensures well-separated classes that remain internally compact in the projected space.

Solving for the LDA Direction

Maximizing Fisher's criterion leads to a generalized eigenvalue problem :

The optimal projection vector w (or vectors, for multiclass) is the eigenvector of corresponding to the largest eigenvalue. This defines the best class-separation direction.

Binary Case (two classes) : The optimal direction w is directly proportional to , where and are the class mean vectors.

Classification with LDA

Once the optimal projection w is found, a new test sample x_t can be classified by:

  1. Project the Test Sample : Project (^) x_t onto discriminant (^) w by computing scalar score:.
  2. Compare to a Threshold : Compare this score to a threshold t , typically the midpoint of projected class means.
  3. Classification Rule : If , x_t is classified into Class 1 ( C₁ ). Otherwise ( (^) w^{T}x_t \ge t ), it is classified into Class 2 ( (^) C₂ ).

( w ST^ B w )/( w ST^ Ww )

w ST^ Bw

SB

w ST^ Ww

SW

S B w = λS Ww

S W^ −1^ SB

S W^ −1^ ( μ 2 − μ 1 )

w xT^ t

w xT^ t < t

LDA for Multiclass Dimensionality Reduction

For problems with more than two classes, LDA extends its approach to find multiple discriminant directions.

Multiple Discriminant Directions : LDA finds m discriminant directions, which are the eigenvectors of corresponding to the m largest eigenvalues. These form the columns of transformation matrix W. Maximum Number of Directions : LDA can find at most C - 1 meaningful discriminant directions, where C is the total number of classes (e.g., 9 for MNIST's 10 classes). Transformed Points : Original data points x are transformed into the new lower-dimensional space by multiplying with the transpose of W :.

Practical Considerations for LDA

Assumptions : If violated, LDA's performance might degrade. Assumes data within each class is approximately Gaussian-distributed (bell-curve shape). Assumes all classes share similar covariance structures (i.e., roughly equal scatter matrices). Singular S_W (Within-Class Scatter Matrix) : A common challenge occurs if the within-class scatter matrix is singular (non-invertible). This can happen if: Features greatly outnumber samples in some classes. Features are perfectly correlated. Class data lies on a lower-dimensional subspace than total feature space. If is singular, its inverse ( ) cannot be computed, which is required for the generalized eigenvalue problem. PCA+LDA (A Common Solution) : To overcome singular or improve robustness, a common strategy is to first apply PCA as a preprocessing step. Process : a. Apply PCA to original data to reduce dimensionality to (^) k (where (^) k < samples/features) and decorrelate features. This ensures a non-singular covariance matrix. b. Then, apply LDA to the PCA-transformed data to find discriminant directions. Example (MNIST) : The MNIST table shows PCA+LDA with m=9 (9 discriminant dimensions) resulted in a 12.2% error rate, a substantial improvement over PCA alone (25.5%) at the same dimensionality. This demonstrates how combining both methods' strengths improves classification.

S W^ −1^ SB

x ^ = W Tx

SW

SW SW^ −

SW