nirs4all.operators.transforms.feature_selection module
Feature selection and dimensionality reduction operators for NIRS spectral data.
This module provides wavelength/variable selection and dimensionality reduction methods commonly used in chemometrics for NIRS data:
Feature Selection: - CARS (Competitive Adaptive Reweighted Sampling) - MC-UVE (Monte-Carlo Uninformative Variable Elimination)
Dimensionality Reduction: - FlexiblePCA: PCA with flexible component specification (count or variance ratio) - FlexibleSVD: SVD with flexible component specification (count or variance ratio)
These selectors identify informative wavelengths and reduce feature dimensionality while preserving predictive performance.
- class nirs4all.operators.transforms.feature_selection.FlexiblePCA(n_components: int | float = 0.95, whiten: bool = False, svd_solver: Literal['auto', 'full', 'arpack', 'randomized'] = 'auto', random_state: int | None = None, copy: bool = True)[source]
Bases:
TransformerMixin,BaseEstimatorPCA with flexible component specification.
This transformer wraps sklearn’s PCA with a convenient interface for specifying the number of components either as an integer count or as a variance ratio.
If n_components >= 1: interpreted as the exact number of components to keep
If 0 < n_components < 1: interpreted as the minimum variance ratio to explain
This is useful for NIRS data where you may want to retain either a fixed number of spectral features or a percentage of the variance.
- Parameters:
n_components (int or float, default=0.95) – Number of components to keep: - If int >= 1: exact number of components - If float in (0, 1): minimum variance ratio to explain Default is 0.95 (95% of variance).
whiten (bool, default=False) – If True, whiten the output (each component has unit variance).
svd_solver ({'auto', 'full', 'arpack', 'randomized'}, default='auto') – SVD solver to use.
random_state (int or None, default=None) – Random seed for reproducibility.
copy (bool, default=True) – If False, try in-place transformation.
- Variables:
n_features_in (int) – Number of features in input data.
n_features_out (int) – Number of output components.
components (ndarray of shape (n_components, n_features)) – Principal axes in feature space.
explained_variance (ndarray of shape (n_components,)) – Variance explained by each component.
explained_variance_ratio (ndarray of shape (n_components,)) – Percentage of variance explained by each component.
cumulative_variance_ratio (ndarray of shape (n_components,)) – Cumulative variance ratio.
mean (ndarray of shape (n_features,)) – Per-feature empirical mean, estimated from training data.
pca (PCA) – The underlying fitted sklearn PCA object.
Examples
>>> from nirs4all.operators.transforms import FlexiblePCA >>> import numpy as np >>> >>> # Spectral data with 200 wavelengths >>> X = np.random.randn(100, 200) >>> >>> # Keep 95% of variance >>> pca = FlexiblePCA(n_components=0.95) >>> X_reduced = pca.fit_transform(X) >>> print(f"Reduced to {X_reduced.shape[1]} components") >>> >>> # Keep exactly 10 components >>> pca = FlexiblePCA(n_components=10) >>> X_reduced = pca.fit_transform(X) >>> print(f"Reduced to {X_reduced.shape[1]} components")
Notes
For variance ratio mode, the number of components is determined by finding the minimum number that explains at least n_components of variance.
The fitted PCA object is accessible via the
pca_attribute.
- fit(X, y=None)[source]
Fit the PCA transformer.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Training data.
y (None) – Ignored.
- Returns:
self – Fitted transformer.
- Return type:
- inverse_transform(X)[source]
Transform data back to its original space.
- Parameters:
X (array-like of shape (n_samples, n_components)) – Transformed data.
- Returns:
X_original – Original data (approximation).
- Return type:
ndarray of shape (n_samples, n_features)
- transform(X)[source]
Apply dimensionality reduction to X.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Data to transform.
- Returns:
X_transformed – Transformed data.
- Return type:
ndarray of shape (n_samples, n_components)
- class nirs4all.operators.transforms.feature_selection.FlexibleSVD(n_components: int | float = 0.95, algorithm: Literal['arpack', 'randomized'] = 'randomized', n_iter: int = 5, random_state: int | None = None, copy: bool = True)[source]
Bases:
TransformerMixin,BaseEstimatorTruncated SVD with flexible component specification.
This transformer wraps sklearn’s TruncatedSVD with a convenient interface for specifying the number of components either as an integer count or as a variance ratio.
If n_components >= 1: interpreted as the exact number of components to keep
If 0 < n_components < 1: interpreted as the minimum variance ratio to explain
Unlike PCA, TruncatedSVD does not center the data before computing the SVD, making it suitable for sparse matrices and data that should not be centered.
- Parameters:
n_components (int or float, default=0.95) – Number of components to keep: - If int >= 1: exact number of components - If float in (0, 1): minimum variance ratio to explain Default is 0.95 (95% of variance).
algorithm ({'arpack', 'randomized'}, default='randomized') – SVD solver to use.
n_iter (int, default=5) – Number of iterations for randomized SVD solver.
random_state (int or None, default=None) – Random seed for reproducibility.
copy (bool, default=True) – If False, try in-place transformation.
- Variables:
n_features_in (int) – Number of features in input data.
n_features_out (int) – Number of output components.
components (ndarray of shape (n_components, n_features)) – Components with maximum variance.
explained_variance (ndarray of shape (n_components,)) – Variance explained by each component.
explained_variance_ratio (ndarray of shape (n_components,)) – Percentage of variance explained by each component.
cumulative_variance_ratio (ndarray of shape (n_components,)) – Cumulative variance ratio.
singular_values (ndarray of shape (n_components,)) – Singular values corresponding to each component.
svd (TruncatedSVD) – The underlying fitted sklearn TruncatedSVD object.
Examples
>>> from nirs4all.operators.transforms import FlexibleSVD >>> import numpy as np >>> >>> # Spectral data with 200 wavelengths >>> X = np.random.randn(100, 200) >>> >>> # Keep 95% of variance >>> svd = FlexibleSVD(n_components=0.95) >>> X_reduced = svd.fit_transform(X) >>> print(f"Reduced to {X_reduced.shape[1]} components") >>> >>> # Keep exactly 10 components >>> svd = FlexibleSVD(n_components=10) >>> X_reduced = svd.fit_transform(X) >>> print(f"Reduced to {X_reduced.shape[1]} components")
Notes
For variance ratio mode, an initial SVD with more components is computed to estimate the total variance, then the optimal number is determined.
TruncatedSVD is recommended over PCA for sparse data or when centering is not desired.
The fitted SVD object is accessible via the
svd_attribute.
- fit(X, y=None)[source]
Fit the SVD transformer.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Training data.
y (None) – Ignored.
- Returns:
self – Fitted transformer.
- Return type:
- inverse_transform(X)[source]
Transform data back to its original space.
- Parameters:
X (array-like of shape (n_samples, n_components)) – Transformed data.
- Returns:
X_original – Original data (approximation).
- Return type:
ndarray of shape (n_samples, n_features)
- transform(X)[source]
Apply dimensionality reduction to X.
- Parameters:
X (array-like of shape (n_samples, n_features)) – Data to transform.
- Returns:
X_transformed – Transformed data.
- Return type:
ndarray of shape (n_samples, n_components)