Machine learning / Dimensionality reduction and anomaly
Dimensionality Reduction with Python (PCA)
Contents
- Introduction to Dimensionality Reduction
- Principal Component Analysis (PCA)
- Standardization and Mean Centering
- Explained Variance Ratio
- Visualizing Principal Components
- Dimensionality Reduction with PCA
- PCA for Visualization
- PCA for Feature Extraction
- Incremental PCA
- Kernel PCA
- PCA for Noise Reduction
- PCA for Outlier Detection
- PCA for Data Compression
Introduction to Dimensionality Reduction
Dimensionality reduction is the process of reducing the number of features or variables in a dataset while retaining most of the relevant information. This is particularly useful in machine learning when dealing with high-dimensional data, which can lead to the curse of dimensionality and computational challenges.
Code:
# No code for the introduction
Principal Component Analysis (PCA)
Principal Component Analysis (PCA) is a popular dimensionality reduction technique that transforms the data into a new coordinate system, where the new axes (principal components) are orthogonal and ordered by the amount of variance they explain in the data.
Code:
from sklearn.decomposition import PCA
# Create a PCA object
pca = PCA(n_components=2) # Reduce to 2 dimensions
# Fit and transform the data
X_transformed = pca.fit_transform(X)
Standardization and Mean Centering
Before applying PCA, it is essential to standardize the data by subtracting the mean and dividing by the standard deviation. This ensures that all features are on the same scale and prevents features with larger variances from dominating the analysis.
Code:
from sklearn.preprocessing import StandardScaler
# Standardize the data
scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)
# Apply PCA on the scaled data
pca = PCA(n_components=2)
X_transformed = pca.fit_transform(X_scaled)
Explained Variance Ratio
PCA provides the explained variance ratio, which represents the proportion of variance in the original data that is preserved by each principal component. This information can be used to determine the number of components to retain for dimensionality reduction.
Code:
# Get the explained variance ratio
explained_variance_ratio = pca.explained_variance_ratio_
# Sum of the explained variance ratios (should be close to 1)
print(sum(explained_variance_ratio))
Visualizing Principal Components
Principal components can be visualized to understand the relationships between features and the distribution of data points in the transformed space.
Code:
import matplotlib.pyplot as plt
# Plot the transformed data
plt.scatter(X_transformed[:, 0], X_transformed[:, 1])
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.show()
Dimensionality Reduction with PCA
After determining the number of principal components to retain, PCA can be used to transform the data into a lower-dimensional space, effectively reducing the number of features.
Code:
# Reduce to 3 dimensions
pca = PCA(n_components=3)
X_reduced = pca.fit_transform(X_scaled)
# Shape of the reduced data
print(X_reduced.shape)
PCA for Visualization
PCA can be used for visualizing high-dimensional data by projecting it onto a lower-dimensional space, typically 2D or 3D, for better understanding and exploration.
Code:
# Reduce to 2 dimensions for visualization
pca = PCA(n_components=2)
X_vis = pca.fit_transform(X_scaled)
# Plot the data in 2D
plt.scatter(X_vis[:, 0], X_vis[:, 1])
plt.xlabel('Principal Component 1')
plt.ylabel('Principal Component 2')
plt.show()
PCA for Feature Extraction
PCA can be used as a feature extraction technique, where the principal components themselves become the new features. This can be useful for reducing the dimensionality of the data while preserving most of the relevant information.
Code:
# Reduce to 5 principal components
pca = PCA(n_components=5)
X_features = pca.fit_transform(X_scaled)
# Use the transformed data (X_features) as input to a machine learning model
Incremental PCA
Incremental PCA is a variant of PCA that allows for efficient updates when new data points are added to the dataset, without having to recompute the principal components from scratch.
Code:
from sklearn.decomposition import IncrementalPCA
# Initialize the Incremental PCA object
ipca = IncrementalPCA(n_components=2, batch_size=100)
# Partial fit on the first batch of data
ipca.partial_fit(X[:100])
# Partial fit on the next batch of data
ipca.partial_fit(X[100:200])
# Transform the data
X_transformed = ipca.transform(X)
Kernel PCA
Kernel PCA is a non-linear extension of PCA that can capture non-linear relationships in the data by mapping the original data into a higher-dimensional feature space and then applying linear PCA in that space.
Code:
from sklearn.decomposition import KernelPCA
# Initialize the Kernel PCA object
kpca = KernelPCA(n_components=2, kernel='rbf')
# Fit and transform the data
X_transformed = kpca.fit_transform(X)
PCA for Noise Reduction
PCA can be used for noise reduction by projecting the data onto a lower-dimensional subspace formed by the principal components, effectively removing noise that is captured by the discarded components.
Code:
# Reduce to 10 principal components
pca = PCA(n_components=10)
X_denoised = pca.fit_transform(X_noisy)
# Reconstruct the denoised data
X_reconstructed = pca.inverse_transform(X_denoised)
PCA for Outlier Detection
PCA can be used for outlier detection by identifying data points that have large squared reconstruction errors when projected onto the principal component subspace.
Code:
from sklearn.decomposition import PCA
# Initialize the PCA object
pca = PCA(n_components=5)
# Fit and transform the data
X_transformed = pca.fit_transform(X)
# Reconstruct the data
X_reconstructed = pca.inverse_transform(X_transformed)
# Calculate the squared reconstruction error
squared_errors = ((X - X_reconstructed) ** 2).sum(axis=1)
# Identify outliers based on a threshold
threshold = np.percentile(squared_errors, 95)
outliers = np.where(squared_errors > threshold)[0]
PCA for Data Compression
PCA can be used for data compression by keeping only the principal components that capture most of the variance in the data, effectively reducing the storage or transmission requirements.
Code:
# Reduce to 10 principal components
pca = PCA(n_components=10)
X_compressed = pca.fit_transform(X)
# Reconstruct the compressed data
X_reconstructed = pca.inverse_transform(X_compressed)
Slide 14 (Additional Resources): Additional Resources
For further exploration and learning, here are some recommended resources from arXiv.org:
- "A Tutorial on Principal Component Analysis" by Jonathon Shlens (https://arxiv.org/abs/1404.1100)
- "Principal Component Analysis in Linear Integrated Circuit Design" by Robert Dworkin, Dan Knebel (https://arxiv.org/abs/1904.02120)
- "Principal Component Analysis: A Powerful Visual Pattern Recognition Technique" by Kazi A. Kalpoma (https://arxiv.org/abs/1704.04392)