ML Fundamentals

Mastering the Core Concepts of Machine Learning

Dimensionality Reduction

Dimensionality reduction is a technique used in machine learning to reduce the number of features (variables) in a dataset while retaining as much of the important information as possible. This process is crucial for several reasons:

  • Combating the Curse of Dimensionality: As the number of features increases, the data becomes sparser, making it harder for algorithms to generalize and increasing computational cost.
  • Improving Model Performance: Removing irrelevant or redundant features can lead to simpler models that are less prone to overfitting and often perform better.
  • Reducing Computational Cost: Fewer features mean faster training and inference times.
  • Easier Data Visualization: Reducing data to 2 or 3 dimensions allows for easier plotting and understanding of relationships.

Types of Dimensionality Reduction

Dimensionality reduction techniques can be broadly categorized into two main types:

1. Feature Selection

Feature selection involves identifying and selecting a subset of the original features that are most relevant to the problem. Features that are not useful are discarded entirely. This method retains the original meaning of the features.

  • Filter Methods: These methods assess the relevance of features based on their intrinsic properties, often using statistical measures like correlation, mutual information, or chi-squared tests, independent of any specific machine learning algorithm.
  • Wrapper Methods: These methods use a specific machine learning algorithm to evaluate the usefulness of feature subsets. They train and test the model with different combinations of features and select the subset that yields the best performance. Examples include Recursive Feature Elimination (RFE).
  • Embedded Methods: These methods perform feature selection as part of the model training process. Algorithms like LASSO regression or decision trees with feature importance inherently perform feature selection.

2. Feature Extraction

Feature extraction involves transforming the original feature space into a new, lower-dimensional feature space. The new features are combinations of the original features and are often not directly interpretable.

Principal Component Analysis (PCA)

PCA is one of the most popular feature extraction techniques. It transforms the data into a new coordinate system such that the greatest variances of the data lie along the new axes, called principal components. The first principal component captures the most variance, the second captures the second most variance orthogonal to the first, and so on.

Goal: To find a set of orthogonal axes (principal components) that capture the maximum variance in the data.

Key Concepts: Eigenvectors, Eigenvalues, Covariance Matrix.


from sklearn.decomposition import PCA
import numpy as np

# Sample data (e.g., 100 samples, 5 features)
X = np.random.rand(100, 5)

# Initialize PCA to reduce to 2 components
pca = PCA(n_components=2)

# Fit PCA on the data and transform it
X_reduced = pca.fit_transform(X)

print(f"Original shape: {X.shape}")
print(f"Reduced shape: {X_reduced.shape}")
print(f"Explained variance ratio: {pca.explained_variance_ratio_}")
                
t-Distributed Stochastic Neighbor Embedding (t-SNE)

t-SNE is a non-linear dimensionality reduction technique particularly well-suited for visualizing high-dimensional datasets. It excels at revealing local structure, such as clusters, in the data.

Goal: To preserve the local structure of the data in the lower-dimensional space, mapping similar data points to nearby locations and dissimilar points to distant locations.

Key Concepts: Probabilistic mapping, Kullback-Leibler divergence.


from sklearn.manifold import TSNE
import numpy as np

# Sample data (e.g., 100 samples, 10 features)
X = np.random.rand(100, 10)

# Initialize t-SNE to reduce to 2 components
tsne = TSNE(n_components=2, random_state=42)

# Fit and transform the data
X_reduced = tsne.fit_transform(X)

print(f"Original shape: {X.shape}")
print(f"Reduced shape: {X_reduced.shape}")
                

When to Use Dimensionality Reduction

  • When dealing with datasets having a very large number of features.
  • To visualize high-dimensional data.
  • To improve the performance and reduce training time of machine learning models.
  • To overcome the curse of dimensionality.

Considerations

  • Feature extraction methods like PCA can make features less interpretable.
  • Choosing the right number of components or dimensions is critical and often requires experimentation.
  • Non-linear techniques like t-SNE are generally computationally more expensive than linear ones like PCA.
PCA Example Visualization

Illustrative representation of PCA transforming data onto principal components.

t-SNE Example Visualization

t-SNE revealing clusters in high-dimensional data.