spectrochempy.CPο
- class CP(*, log_level='WARNING', warm_start=False, cvg_criterion='abs_rec_error', fixed_modes, hard_sparsity=None, init='svd', l1_reg=None, l2_reg=None, l2_square_reg=None, monotonicity=None, n_components=0, n_iter_max=100, n_iter_max_inner=10, non_negative=False, normalize=None, normalized_sparsity=None, random_state=None, return_errors=False, simplex=None, smoothness=None, soft_sparsity=None, svd='truncated_svd', tol_inner=1e-06, tol_outer=1e-08, unimodality=None, verbose=0)[source]ο
CP/PARAFAC decomposition of 3D datasets using TensorLy.
CP (Canonical Polyadic) decomposition, also known as PARAFAC, factorizes a 3D tensor into a sum of rank-1 tensors. By default, this implementation uses TensorLyβs
parafacfunction. When constraints or penalties are active (e.g.,non_negative,l1_reg,smoothness, etc.), it automatically switches to TensorLyβsconstrained_parafacfunction with AO-ADMM optimization.Note:
fixed_modesis supported by bothparafacandconstrained_parafac, so usingfixed_modesalone does not trigger the constrained path.Only 3D datasets are supported. For 2D data, use PCA, NMF, or SVD instead.
- Parameters:
Number of components (rank) for the decomposition.
Maximum number of outer iterations for the ALS algorithm.
Number of iterations for inner loop (ADMM optimization).
Type of factor matrix initialization. If a CPTensor is passed, it is used directly.
Function to use for SVD computation during initialization.
Relative reconstruction error tolerance for outer loop convergence.
Absolute reconstruction error tolerance for inner loop (ADMM optimization).
Seed for random number generator or RandomState instance for reproducibility.
Level of verbosity for iteration logging.
Whether to store iteration errors after fitting. Errors are only
available when using constrained_parafac (i.e., when constraints
are active). For unconstrained parafac, errors will be None.
If True, applies non-negative constraint to all modes. Can also be a dict
specifying constraints per mode.
L1 norm regularization parameter for sparsity. Applied to factors.
L2 norm regularization parameter. Applied to factors.
L2 square norm regularization parameter. Applied to factors.
If True, enforces unimodality constraint on all modes.
If True, normalizes factors by dividing by maximum value.
Projects factors onto the simplex with the given parameter.
Normalizes factors with L1 norm after hard thresholding.
Imposes L1 norm bound on factor columns.
Optimizes factors by solving a banded system for smoothness.
If True, projects factor columns to monotonically decreasing distribution.
Applies hard thresholding with the given threshold.
Stopping criterion for ALS. βabs_rec_errorβ uses absolute difference,
βrec_errorβ uses relative difference.
Modes for which initial values are not modified during optimization.
The last mode cannot be fixed.
- cvg_criterionany value of [
'abs_rec_error','rec_error'], optional, default:'abs_rec_error' Stopping criterion for ALS, works if
tolis not None.If βrec_errorβ, ALS stops at current iteration if
(previous rec_error - current rec_error) < tol. If βabs_rec_errorβ, ALS terminates when|previous rec_error - current rec_error| < tol.- fixed_modes
list, optional, default: [] - A list of modes for which the initial value is not modified.
The last mode cannot be fixed due to error computation.
- hard_sparsitya float or a list or a dict, optional, default:
None Hard thresholding with the given threshold.
- initany value of [
'random','svd','CPTensor'], optional, default:'svd' Type of factor matrix initialization.
If a CPTensor is passed, this is directly used for initialization. See
initialize_factors.- l1_rega float or a list or a dict, optional, default:
None Penalizes the factor with the l1 norm using the input value as regularization parameter.
- l2_rega float or a list or a dict, optional, default:
None Penalizes the factor with the l2 norm using the input value as regularization parameter.
- l2_square_rega float or a list or a dict, optional, default:
None Penalizes the factor with the l2 square norm using the input value as regularization parameter.
- monotonicitya boolean or a dict, optional, default:
None - Projects columns to monotonically decreasing distribution.
Applied to each column separately. If True, monotonicity constraint is applied to all modes.
- n_components
int, optional, default:0 Number of components (this is the βrankβ parameter used in TensorLy).
- n_iter_max
int, optional, default:100 Maximum number of outer iterations.
- n_iter_max_inner
int, optional, default:10 Number of iterations for inner loop (ADMM optimization).
- non_negativea boolean or a dict, optional, default:
False This constraint clips negative values to β0β.
If True, non-negative constraint is applied to all modes.
- normalizea boolean or a dict, optional, default:
None - This constraint divides all the values by maximum value of the input array.
If True, normalize constraint is applied to all modes.
- normalized_sparsitya float or a list or a dict, optional, default:
None Normalizes with the norm after hard thresholding.
- random_statean int or a RandomState, optional, default:
None - If int, used to set the seed of the random number generator.
If numpy.random.RandomState, used to initialize factor matrices with uniform distribution.
- return_errors
bool, optional, default:False Activate return of iteration errors.
- simplexa float or a list or a dict, optional, default:
None - Projects on the simplex with the given parameter.
Applied to each column separately.
- smoothnessa float or a list or a dict, optional, default:
None Optimizes the factors by solving a banded system.
- soft_sparsitya float or a list or a dict, optional, default:
None Impose that the columns of factors have L1 norm bounded by a user-defined threshold.
- svdany value of [
'numpy_svd','truncated_svd','randomized_svd'], optional, default:'truncated_svd' Function to use to compute the SVD. Maps to tensorly SVD functions.
- tol_inner
float, optional, default:1e-06 - Absolute reconstruction error tolerance for factor update during inner loop,
i.e., ADMM optimization.
- tol_outer
float, optional, default:1e-08 Relative reconstruction error tolerance for outer loop.
The algorithm is considered to have found a local minimum when the reconstruction error is less than
tol_outer.- unimodalitya boolean or a dict, optional, default:
None - If True, enforces unimodality constraint on all modes.
Applied to each mode separately.
- verbose
int, optional, default:0 Level of verbosity.
Notes
This method requires the optional dependency
tensorly. Install it with:pip install tensorly
CP decomposition is sensitive to initialization and rank choice. The results may vary with different random seeds. Use
random_statefor reproducibility.Core consistency (CORCONDIA) helps assess model validity. Values close to 100% indicate a good fit, while negative values suggest overfactoring.
Examples
>>> import spectrochempy as scp >>> import numpy as np >>> X = np.random.rand(6, 8, 10) >>> ds = scp.NDDataset(X) >>> cp = scp.CP(n_components=2) >>> cp.fit(ds) >>> A, B, C = cp.loadings >>> Xr = cp.inverse_transform()
- weightsο
Weights from CP decomposition.
- Type:
ndarray
- errorsο
Iteration errors during fitting. Available when TensorLy returns them (typically with constrained_parafac). Returns None if unavailable or if not fitted yet.
- Type:
list or None
- core_consistencyο
CORCONDIA (Core Consistency) diagnostic value. Can be negative if overfactoring.
- Type:
Initialize the BaseConfigurable class.
- Parameters:
log_level (int, optional) β The log level at startup. Default is logging.WARNING.
**kwargs (dict) β Additional keyword arguments for configuration.
Attributes Summary
Return factor matrix A (mode 0 loadings).
Return factor matrix B (mode 1 loadings).
Return factor matrix C (mode 2 loadings).
Return Sum of Squared Errors.
Return the X input dataset (eventually modified by the model).
The
Yinput.Return factor B (mode 1) as components, following PCA convention.
traitlets.config.Configobject.Return CORCONDIA (Core Consistency) diagnostic.
Stopping criterion for ALS, works if
tolis not None.Return iteration errors if return_errors was True.
Return explained variance percentage.
A list of modes for which the initial value is not modified.
Hard thresholding with the given threshold.
Type of factor matrix initialization.
Penalizes the factor with the l1 norm using the input value as regularization parameter.
Penalizes the factor with the l2 norm using the input value as regularization parameter.
Penalizes the factor with the l2 square norm using the input value as regularization parameter.
Return tuple of factor matrices (A, B, C).
Return
logoutput.Projects columns to monotonically decreasing distribution.
Number of components (this is the 'rank' parameter used in TensorLy).
Maximum number of outer iterations.
Number of iterations for inner loop (ADMM optimization).
Object name
This constraint clips negative values to '0'.
This constraint divides all the values by maximum value of the input array.
Normalizes with the norm after hard thresholding.
If int, used to set the seed of the random number generator.
Activate return of iteration errors.
Projects on the simplex with the given parameter.
Optimizes the factors by solving a banded system.
Impose that the columns of factors have L1 norm bounded by a user-defined threshold.
Function to use to compute the SVD.
Absolute reconstruction error tolerance for factor update during inner loop, i.e., ADMM optimization.
Relative reconstruction error tolerance for outer loop.
If True, enforces unimodality constraint on all modes.
Level of verbosity.
Return the weights from CP decomposition.
Methods Summary
fit(X)Fit the CP model on X.
fit_transform(X,Β **kwargs)Fit the CP model on X and return the factors.
get_components([n_components])Return the component's dataset: (selected n_components, n_features).
inverse_transform([X_transform])Transform data back to its original space.
parameters([replace,Β removed,Β default])Alias for
paramsmethod.params([default])Return current or default configuration values.
plot_merit([X,Β X_hat])Plot the input (
X), reconstructed (X_hat) and residuals.plotmerit([X,Β X_hat])Plot the input (
X), reconstructed (X_hat) and residuals.reconstruct([X_transform])Transform data back to its original space.
reduce([X])Apply dimensionality reduction to
X.reset()Reset configuration parameters to their default values.
to_dict()Return config value in a dict form.
transform([X])Apply dimensionality reduction to
X.Attributes Documentation
- Aο
Return factor matrix A (mode 0 loadings).
- Bο
Return factor matrix B (mode 1 loadings).
- Cο
Return factor matrix C (mode 2 loadings).
- SSEο
Return Sum of Squared Errors.
- Xο
Return the X input dataset (eventually modified by the model).
- componentsο
Return factor B (mode 1) as components, following PCA convention.
- Returns:
NDDataset β Factor B with shape (mode_1_size, n_components).
- configο
traitlets.config.Configobject.
- core_consistencyο
Return CORCONDIA (Core Consistency) diagnostic.
- Returns:
float β Core consistency value. Can be negative if overfactoring occurred.
- cvg_criterionο
Stopping criterion for ALS, works if
tolis not None.If βrec_errorβ, ALS stops at current iteration if
(previous rec_error - current rec_error) < tol. If βabs_rec_errorβ, ALS terminates when|previous rec_error - current rec_error| < tol.
- errorsο
Return iteration errors if return_errors was True.
- explained_varianceο
Return explained variance percentage.
- fixed_modesο
A list of modes for which the initial value is not modified. The last mode cannot be fixed due to error computation.
- hard_sparsityο
Hard thresholding with the given threshold.
- initο
Type of factor matrix initialization.
If a CPTensor is passed, this is directly used for initialization. See
initialize_factors.
- l1_regο
Penalizes the factor with the l1 norm using the input value as regularization parameter.
- l2_regο
Penalizes the factor with the l2 norm using the input value as regularization parameter.
- l2_square_regο
Penalizes the factor with the l2 square norm using the input value as regularization parameter.
- loadingsο
Return tuple of factor matrices (A, B, C).
- logο
Return
logoutput.
- monotonicityο
Projects columns to monotonically decreasing distribution. Applied to each column separately. If True, monotonicity constraint is applied to all modes.
- n_componentsο
Number of components (this is the βrankβ parameter used in TensorLy).
- n_iter_maxο
Maximum number of outer iterations.
- n_iter_max_innerο
Number of iterations for inner loop (ADMM optimization).
- nameο
Object name
- non_negativeο
This constraint clips negative values to β0β.
If True, non-negative constraint is applied to all modes.
- normalizeο
This constraint divides all the values by maximum value of the input array. If True, normalize constraint is applied to all modes.
- normalized_sparsityο
Normalizes with the norm after hard thresholding.
- random_stateο
If int, used to set the seed of the random number generator. If numpy.random.RandomState, used to initialize factor matrices with uniform distribution.
- return_errorsο
Activate return of iteration errors.
- simplexο
Projects on the simplex with the given parameter. Applied to each column separately.
- smoothnessο
Optimizes the factors by solving a banded system.
- soft_sparsityο
Impose that the columns of factors have L1 norm bounded by a user-defined threshold.
- svdο
Function to use to compute the SVD. Maps to tensorly SVD functions.
- tol_innerο
Absolute reconstruction error tolerance for factor update during inner loop, i.e., ADMM optimization.
- tol_outerο
Relative reconstruction error tolerance for outer loop.
The algorithm is considered to have found a local minimum when the reconstruction error is less than
tol_outer.
- unimodalityο
If True, enforces unimodality constraint on all modes. Applied to each mode separately.
- verboseο
Level of verbosity.
- weightsο
Return the weights from CP decomposition.
Methods Documentation
- fit(X)[source]ο
Fit the CP model on X.
- Parameters:
X (NDDataset) β 3D dataset to decompose.
- Returns:
self (CP) β The fitted CP instance.
- fit_transform(X, **kwargs)[source]ο
Fit the CP model on X and return the factors.
- Parameters:
X (NDDataset) β 3D dataset to decompose.
**kwargs β Additional keyword arguments passed to fit.
- Returns:
tuple of NDDataset β The factor matrices (A, B, C).
- get_components(n_components=None)ο
Return the componentβs dataset: (selected n_components, n_features).
- Parameters:
n_components (
int, optional, default:None) β The number of components to keep in the output dataset. IfNone, all calculated components are returned.- Returns:
NDDatasetβ Dataset with shape (n_components, n_features)
- inverse_transform(X_transform=None, **kwargs)[source]ο
Transform data back to its original space.
Reconstruct the original tensor from the CP factors.
- Parameters:
X_transform (None) β Ignored. Present for API compatibility.
**kwargs β Additional keyword arguments (ignored).
- Returns:
NDDataset β Reconstructed dataset with shape matching the input.
- parameters(replace="params", removed="0.8.0") def parameters(self, default=False)[source]ο
Alias for
paramsmethod.Deprecated since version 0.8.0: Use
paramsinstead.
- plot_merit(X=None, X_hat=None, **kwargs)[source]ο
Plot the input (
X), reconstructed (X_hat) and residuals.\(X\) and \(\hat{X}\) can be passed as arguments. If not, the
Xattribute is used for \(X`and :math:\)hat{X}`is computed by theinverse_transformmethod- Parameters:
X (
NDDataset, optional) β Original dataset. If is not provided (default), theXattribute is used and X_hat is computed usinginverse_transform.X_hat (
NDDataset, optional) β Inverse transformed dataset. ifXis provided,X_hatmust also be provided as compuyed externally.
- Returns:
Axesβ Matplotlib subplot axe.- Other Parameters:
exp_c (color, colormap, or list of colors, optional) β Color(s) for experimental spectra. - None: use unified semantic resolver (auto-detect categorical/sequential) - Single color: use for all experimental spectra - Colormap name/object: sample colors from colormap - List/tuple: use as explicit color cycle
calc_c (color, colormap, or list of colors, optional) β Color(s) for calculated spectra. - None: use default blue β#2a6fbbβ - Single color: use for all calculated spectra - Colormap name/object: sample colors from colormap - List/tuple: use as explicit color cycle
resid_c (color, colormap, or list of colors, optional) β Color(s) for residual spectra. - None: use default grey β0.4β - Single color: use for all residual spectra - Colormap name/object: sample colors from colormap - List/tuple: use as explicit color cycle
exp_linestyle (str, optional) β Line style for experimental spectra. Default: β-β.
calc_linestyle (str, optional) β Line style for calculated spectra. Default: βββ.
resid_linestyle (str, optional) β Line style for residual spectra. Default: β-β.
exp_linewidth (float, optional) β Line width for experimental spectra. Default: 1.2.
calc_linewidth (float, optional) β Line width for calculated spectra. Default: 1.0.
resid_linewidth (float, optional) β Line width for residual spectra. Default: 1.0.
min_contrast (float, optional) β Minimum contrast ratio for sequential colormaps. Default: 1.5.
offset (
float, optional, default:None) β Specify the separation (in percent) between the \(X\) , \(X_hat\) and \(E\).nb_traces (
intor'all', optional) β Number of lines to display. Default is'all'.**others (Other keywords parameters) β Parameters passed to the internal
plotmethod of theXdataset.
- plotmerit(X=None, X_hat=None, **kwargs)[source]ο
Plot the input (
X), reconstructed (X_hat) and residuals.\(X\) and \(\hat{X}\) can be passed as arguments. If not, the
Xattribute is used for \(X`and :math:\)hat{X}`is computed by theinverse_transformmethod- Parameters:
X (
NDDataset, optional) β Original dataset. If is not provided (default), theXattribute is used and X_hat is computed usinginverse_transform.X_hat (
NDDataset, optional) β Inverse transformed dataset. ifXis provided,X_hatmust also be provided as compuyed externally.
- Returns:
Axesβ Matplotlib subplot axe.- Other Parameters:
exp_c (color, colormap, or list of colors, optional) β Color(s) for experimental spectra. - None: use unified semantic resolver (auto-detect categorical/sequential) - Single color: use for all experimental spectra - Colormap name/object: sample colors from colormap - List/tuple: use as explicit color cycle
calc_c (color, colormap, or list of colors, optional) β Color(s) for calculated spectra. - None: use default blue β#2a6fbbβ - Single color: use for all calculated spectra - Colormap name/object: sample colors from colormap - List/tuple: use as explicit color cycle
resid_c (color, colormap, or list of colors, optional) β Color(s) for residual spectra. - None: use default grey β0.4β - Single color: use for all residual spectra - Colormap name/object: sample colors from colormap - List/tuple: use as explicit color cycle
exp_linestyle (str, optional) β Line style for experimental spectra. Default: β-β.
calc_linestyle (str, optional) β Line style for calculated spectra. Default: βββ.
resid_linestyle (str, optional) β Line style for residual spectra. Default: β-β.
exp_linewidth (float, optional) β Line width for experimental spectra. Default: 1.2.
calc_linewidth (float, optional) β Line width for calculated spectra. Default: 1.0.
resid_linewidth (float, optional) β Line width for residual spectra. Default: 1.0.
min_contrast (float, optional) β Minimum contrast ratio for sequential colormaps. Default: 1.5.
offset (
float, optional, default:None) β Specify the separation (in percent) between the \(X\) , \(X_hat\) and \(E\).nb_traces (
intor'all', optional) β Number of lines to display. Default is'all'.**others (Other keywords parameters) β Parameters passed to the internal
plotmethod of theXdataset.
- reconstruct(X_transform=None, **kwargs)[source]ο
Transform data back to its original space.
In other words, return an input
X_originalwhose reduce/transform would beX_transform.- Parameters:
X_transform (array-like of shape (n_observations, n_components), optional) β Reduced
Xdata, wheren_observationsis the number of observations andn_componentsis the number of components. IfX_transformis not provided, a transform ofXprovided infitis performed first.**kwargs (keyword parameters, optional) β See Other Parameters.
- Returns:
NDDatasetβ Dataset with shape (n_observations, n_features).- Other Parameters:
n_components (
int, optional) β The number of components to use for the reconstruction.
See also
reconstructAlias of inverse_transform (Deprecated).
Notes
Deprecated in version 0.6.
- reduce(X=None, **kwargs)[source]ο
Apply dimensionality reduction to
X.- Parameters:
X (
NDDatasetor array-like of shape (n_observations, n_features), optional) β New data, where n_observations is the number of observations and n_features is the number of features. if not provided, the input dataset of thefitmethod will be used.**kwargs (keyword parameters, optional) β See Other Parameters.
- Returns:
NDDatasetβ Dataset with shape (n_observations, n_components).- Other Parameters:
n_components (
int, optional) β The number of components to use for the reduction. If not given the number of components is eventually the one specified or determined in thefitprocess.
Notes
Deprecated in version 0.6.
- transform(X=None, **kwargs)ο
Apply dimensionality reduction to
X.- Parameters:
X (
NDDatasetor array-like of shape (n_observations, n_features), optional) β New data, where n_observations is the number of observations and n_features is the number of features. if not provided, the input dataset of thefitmethod will be used.**kwargs (keyword parameters, optional) β See Other Parameters.
- Returns:
NDDatasetβ Dataset with shape (n_observations, n_components).- Other Parameters:
n_components (
int, optional) β The number of components to use for the reduction. If not given the number of components is eventually the one specified or determined in thefitprocess.
Examples using spectrochempy.CP