spectrochempy.PLSRegressionο
- class PLSRegression(*, log_level='WARNING', warm_start=False, max_iter=500, n_components=2, scale=True, tol=1e-06)[source]ο
- Parameters:
max_iter (
int, optional, default:500) β The maximum number of iterations of the power method when algorithm=βnipalsβ. Ignored otherwise.n_components (
int, optional, default:2) β Number of components to keep. Should be in the range [1, min(n_samples, n_features, n_targets)].scale (
bool, optional, default:True) β Whether to scale X and Y.tol (
float, optional, default:1e-06) β The tolerance used as convergence criteria in the power method:the algorithm stops whenever the squared norm of u_i - u_{i-1} is less than tol, where u corresponds to the left singular vector.
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 the X input dataset (eventually modified by the model).
The
Yinput.NDDatasetwith components in feature space (n_components, n_features).traitlets.config.Configobject.Return
logoutput.The maximum number of iterations of the power method when algorithm='nipals'.
Number of components to keep.
Object name
Whether to scale X and Y.
the algorithm stops whenever the squared norm of u_i - u_{i-1} is less than tol, where u corresponds to the left singular vector.
Methods Summary
fit(X,Β Y)Fit the PLSRegression model on X and Y.
fit_transform(X,Β Y[,Β both])Fit the model with
XandYand apply the dimensionality reduction onXand optionally onY.get_components([n_components])Return the component's dataset: (selected n_components, n_features).
get_params([deep])Get the configuration parameters of this estimator.
inverse_transform([X_transform,Β ...])Transform data back to its original space.
params([default])Return current or default configuration values.
parityplot([replace,Β removed,Β Y,Β Y_hat,Β ax,Β ...])Backward-compatible alias for
plot_parity.plot_merit([X,Β X_hat])Plot the input (
X), reconstructed (X_hat) and residuals.plot_parity(self[,Β Y,Β Y_hat,Β ax,Β clear,Β show])Plot the predicted (\(\hat{Y}\)) vs measured (\(Y\)) values.
plotmerit([replace,Β removed,Β X,Β X_hat])Backward-compatible alias for
plot_merit.predict([X])Predict targets of given observations.
reset()Reset configuration parameters to their default values.
score([X,Β Y,Β sample_weight])Return the coefficient of determination of the prediction.
set_params(**params)Set configuration parameters on this estimator.
to_dict()Return config value in a dict form.
transform([X,Β Y,Β both])Apply dimensionality reduction to
X`and `Y.Attributes Documentation
- Xο
Return the X input dataset (eventually modified by the model).
- componentsο
NDDatasetwith components in feature space (n_components, n_features).See also
get_componentsRetrieve only the specified number of components.
- configο
traitlets.config.Configobject.
- logο
Return
logoutput.
- max_iterο
The maximum number of iterations of the power method when algorithm=βnipalsβ. Ignored otherwise.
- n_componentsο
Number of components to keep. Should be in the range [1, min(n_samples, n_features, n_targets)].
- nameο
Object name
- scaleο
Whether to scale X and Y.
- tolο
the algorithm stops whenever the squared norm of u_i - u_{i-1} is less than tol, where u corresponds to the left singular vector.
- Type:
The tolerance used as convergence criteria in the power method
Methods Documentation
- fit(X, Y)[source]ο
Fit the PLSRegression model on X and Y.
- Parameters:
X (
NDDatasetor array-like of shape (n_observations, n_features)) β Training data.Y (array-like of shape (n_samples,) or (n_samples, n_targets)) β Target vectors, where n_samples is the number of samples and n_targets is the number of response variables.
- Returns:
self β The fitted instance itself.
See also
fit_transformFit the model with an input dataset
Xand apply the dimensionality reduction onX.
- fit_transform(X, Y, both=False)[source]ο
Fit the model with
XandYand apply the dimensionality reduction onXand optionally onY.- Parameters:
X (
NDDatasetor array-like of shape (n_observations, n_features)) β Training data.Y (
NDDatasetor array-like of shape (n_observations, n_features)) β Training data.both (
bool, optional) β Whether to also apply the dimensionality reduction to Y when neither X nor Y are provided.
- Returns:
- 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, Y_transform=None, both=False, **kwargs)ο
Transform data back to its original space.
In other words, return reconstructed
XandYwhose reduce/transform would beX_transformandY_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.Y_transform (
NDDatasetor array-like of shape (n_observations,n_components), optional) β New data, where n_targets is the number of variables to predict. IfY_transformis not provided, a transform ofYprovided infitis performed first.**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.
See also
reconstructAlias of inverse_transform (Deprecated).
- parityplot(replace="plot_parity", removed="0.12") def parityplot( self, Y=None, Y_hat=None, *, ax=None, clear=True, show=True, **kwargs)[source]ο
Backward-compatible alias for
plot_parity. Deprecated.- Returns:
Axesβ Matplotlib axes containing the parity plot.
- 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. Common options includecolor,linewidth,linestyle,alpha, and standard Matplotlib kwargs.
- plot_parity(self, Y=None, Y_hat=None, *, ax=None, clear=True, show=True, **kwargs)[source]ο
Plot the predicted (\(\hat{Y}\)) vs measured (\(Y\)) values.
\(Y\) and \(\hat{Y}\) can be passed as arguments. If not, the
Yattribute is used for \(\hat{Y}\) computed by thepredictmethod.- Parameters:
Y (
NDDataset, optional) β Measured values. If not provided, usesself.Yand computesY_hatviaself.predict(self.X).Y_hat (
NDDataset, optional) β Predicted values. IfYis provided,Y_hatmust also be provided as computed externally.ax (
Axes, optional) β Axes to plot on. If None, a new figure is created.clear (
bool, optional) β Whether to clear the axes before plotting. Default: True. Only used whenaxis provided.show (
bool, optional) β Whether to display the figure. Default: True.**kwargs (keyword arguments, optional) β Additional keyword arguments passed to
scatter. Includess,c,marker,cmap,norm,vmin,vmax,alpha,linewidths,edgecolors,plotnonfinite.
- Returns:
Axesβ Matplotlib axes containing the parity plot.
See also
parityplotDeprecated alias for this method.
- plotmerit(replace="plot_merit", removed="0.12") def plotmerit(self, X=None, X_hat=None, **kwargs)[source]ο
Backward-compatible alias for
plot_merit. Deprecated.- Returns:
Axesβ Matplotlib axes containing the plot.
- predict(X=None)ο
Predict targets of given observations.
- 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.- Returns:
NDDatasetβ Datasets with shape (n_observations,) or ( n_observations, n_targets).
- score(X=None, Y=None, sample_weight=None)[source]ο
Return the coefficient of determination of the prediction.
The coefficient of determination \(R^2\) is defined as \((1 - \frac{u}{v})\) , where \(u\) is the residual sum of squares
((y_true - y_pred)** 2).sum()and \(v\) is the total sum of squares((y_true - y_true.mean()) ** 2).sum(). The best possible score is1.0and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value ofY, disregarding the input features, would get a \(R^2\) score of 0.0.- Parameters:
X (
NDDatasetor array-like of shape (n_observations, n_features), optional) β Test samples. If not given, the X attribute is used.Y (
NDDatasetor array-like of shape (n_observations, n_targets), optional) β True values forX.sample_weight (
NDDatasetor array-like of shape (n_samples,), default:None) β Sample weights.
- Returns:
floatβ \(R^2\) ofpredict`(X) w.r.t `Y.
- set_params(**params)[source]ο
Set configuration parameters on this estimator.
Returns
selfso that calls can be chained.- Parameters:
**params β Parameter names and values to update.
- Returns:
self β The estimator instance.
- Raises:
SpectroChemPyError β If a parameter name does not correspond to a configurable trait.
- transform(X=None, Y=None, both=False, **kwargs)ο
Apply dimensionality reduction to
X`and `Y.- 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.Y (
NDDatasetor array-like of shape (n_observations, n_targets), optional) β New data, where n_targets is the number of variables to predict. if not provided, the input dataset of thefitmethod will be used.both (
bool, default:False) β Whether to also apply the dimensionality reduction to Y when neither X nor Y are provided.
- Returns:
x_score, y_score (
NDDatasetor tuple ofNDDataset) β Datasets with shape (n_observations, n_components).
Examples using spectrochempy.PLSRegression