Principal Component Analysis (PCAExtractor) for feature extraction.

This module provides PCAExtractor for dimensionality reduction and feature extraction.

Classes

PCAExtractor

class features.extraction.pca.PCAExtractor(FeatureExtractor)

Principal Component Analysis (PCAExtractor).

Linear dimensionality reduction using Singular Value Decomposition (SVD) of the data to project it to a lower dimensional space.
Constructor
__init__(
    self,
    n_components: Optional[Union[int, float]] = None,
    center: bool = True,
    whiten: bool = False,
)

Overview

PCA identifies the axes (principal components) that maximize the variance in the data. The first component accounts for the most variance, the second for the next most, and so on.

The algorithm works by:
  1. Centering the data (subtracting the mean).
  2. Computing the SVD: X = U \Sigma V^T.
  3. Selecting the first k components from V^T.
  4. Projecting the data onto these components.

Parameters

n_components
int, float, or None = None

Number of components to keep:

  • int: Keep exactly this many components.
  • float (0 < x < 1): Select enough components to explain this proportion of variance.
  • None: Keep all components (min(samples, features)).
center
bool = True
If True, center the data by subtracting the mean (standard PCA). If False, also scales the data (correlation matrix based PCA).
whiten
bool = False
If True, the transformation ensures uncorrelated outputs with unit variances by dividing by the singular values.

Attributes

components_
np.ndarray of shape (n_components, n_features)
Principal axes representing directions of maximum variance.
explained_variance_
np.ndarray of shape (n_components,)
The amount of variance explained by each selected component.
explained_variance_ratio_
np.ndarray of shape (n_components,)
Percentage of total variance explained by each component.
singular_values_
np.ndarray of shape (n_components,)
Singular values (square roots of eigenvalues) from SVD.
mean_
np.ndarray of shape (n_features,)
Per-feature empirical mean estimated from the training set.
n_components_
int
Actual number of components kept.

Notes

Complexity:
  • O(min(n^2 p, n p^2)) where n = samples, p = features.
When to use:
  • To reduce dimensionality while preserving global structure.
  • To visualize high-dimensional data (usually with 2 or 3 components).
  • To remove multicollinearity before regression or classification.
  • To compress data.
Limitations:
  • Sensitive to the scale of the input features (scaling is recommended).
  • Only captures linear relationships.
  • Principal components are not always easily interpretable.

References

Jolliffe2002
Jolliffe, I. T. (2002). Principal Component Analysis. Springer Series in Statistics, 2nd Edition.

Reduce dimensions while explaining 95% of variance:

python
>>> from tuiml.features.extraction import PCAExtractor
>>> import numpy as np
>>> X = np.random.randn(100, 10)
>>> pca = PCAExtractor(n_components=0.95)
>>> X_reduced = pca.fit_transform(X)
>>> print(f"Reduced to {pca.n_components_} dimensions")

Methods

fit (self, X: np.ndarray, y: Optional[np.ndarray]=None) -> 'PCAExtractor'

Fit the PCAExtractor model.

Parameters
X
ndarray of shape (n_samples, n_features)
Training data.
y
Ignored
Not used, present for API consistency.
Returns
self
PCAExtractor
The fitted PCAExtractor model.
transform (self, X: np.ndarray) -> np.ndarray

Apply dimensionality reduction to X.

Parameters
X
ndarray of shape (n_samples, n_features)
New data to transform.
Returns
X_new
ndarray of shape (n_samples, n_components)
Transformed values.
inverse_transform (self, X: np.ndarray) -> np.ndarray

Transform data back to its original space.

Parameters
X
ndarray of shape (n_samples, n_components)
Data in transformed space.
Returns
X_original
ndarray of shape (n_samples, n_features)
Data in original space.
fit_transform (self, X: np.ndarray, y: Optional[np.ndarray]=None) -> np.ndarray

Fit the model and apply dimensionality reduction.

Parameters
X
ndarray of shape (n_samples, n_features)
Training data.
y
Ignored
Not used, present for API consistency.
Returns
X_new
ndarray of shape (n_samples, n_components)
Transformed values.
get_covariance (self) -> np.ndarray

Compute data covariance with the generative model.

Returns
cov
ndarray of shape (n_features, n_features)
Estimated covariance of data.
get_precision (self) -> np.ndarray

Compute data precision matrix with the generative model.

Returns
precision
ndarray of shape (n_features, n_features)
Estimated precision of data.
get_feature_names_out (self, input_features: Optional[List[str]]=None) -> np.ndarray

Get output feature names for transformation.

Parameters
input_features
list of str
Ignored, output names are always PC1, PC2, etc.
Returns
feature_names_out
ndarray of str
Names of the output features (PC1, PC2, ...).
get_parameter_schema (cls) -> Dict[str, Any]

Return JSON Schema for constructor parameters.

__repr__ (self) -> str

Return string representation.