Principal Component Analysis (PCAExtractor) for feature extraction.
This module provides PCAExtractor for dimensionality reduction and feature extraction.
Classes
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:- Centering the data (subtracting the mean).
- Computing the SVD: X = U \Sigma V^T.
- Selecting the first k components from V^T.
- 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.
- 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.
- 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_transform
(self, X: np.ndarray, y: Optional[np.ndarray]=None) -> np.ndarray
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_feature_names_out
(self, input_features: Optional[List[str]]=None) -> np.ndarray
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, ...).