PLSRegression
- class sklearn.cross_decomposition.PLSRegression(n_components=2, *, scale=True, max_iter=500, tol=1e-06, copy=True)
PLS regression.
PLS regression.
PLSRegression is also known as PLS2 or PLS1, depending on the number of targets.
For a comparison between other cross decomposition algorithms, see Compare cross decomposition methods.
Read more in the User Guide.
Number of components to keep. Should be in [1, n_features].
Whether to scale X and y.
The maximum number of iterations of the power method when
algorithm='nipals'. Ignored otherwise.
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.
Whether to copy X and y in fit before applying centering,
and potentially scaling. If False, these operations will be done
inplace, modifying both arrays.
The left singular vectors of the cross-covariance matrices of each iteration.
The right singular vectors of the cross-covariance matrices of each iteration.
The loadings of X.
The loadings of y.
The transformed training samples.
>>> from sklearn.cross_decomposition import PLSRegression
>>> X = [[0., 0., 1.], [1.,0.,0.], [2.,2.,2.], [2.,5.,4.]]
>>> y = [[0.1, -0.2], [0.9, 1.1], [6.2, 5.9], [11.9, 12.3]]
>>> pls2 = PLSRegression(n_components=2)
>>> pls2.fit(X, y)
PLSRegression()
>>> y_pred = pls2.predict(X)
For a comparison between PLS Regression and PCA, see
Principal Component Regression vs Partial Least Squares Regression.
Fit model to data.
Training vectors, where n_samples is the number of samples and
n_features is the number of predictors.
Target vectors, where n_samples is the number of samples and
n_targets is the number of response variables.
Fitted model.
Learn and apply the dimension reduction on the train data.
Training vectors, where n_samples is the number of samples and
n_features is the number of predictors.
Target vectors, where n_samples is the number of samples and
n_targets is the number of response variables.
Return x_scores if y is not given, (x_scores, y_scores) otherwise.
Get output feature names for transformation.
The feature names out will prefixed by the lowercased class name. For
example, if the transformer outputs 3 features, then the feature names
out are: ["class_name0", "class_name1", "class_name2"].
Only used to validate feature names with the names seen in fit.
Transformed feature names.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating
routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Transform data back to its original space.
New data, where n_samples is the number of samples
and n_components is the number of pls components.
New target, where n_samples is the number of samples
and n_components is the number of pls components.
Return the reconstructed X data.
Return the reconstructed X target. Only returned when y is given.
This transformation will only be exact if n_components=n_features.
Predict targets of given samples.
Samples.
Whether to copy X or perform in-place normalization.
Returns predicted values.
This call requires the estimation of a matrix of shape
(n_features, n_targets), which may be an issue in high dimensional
space.
Return coefficient of determination on test data.
The coefficient of determination, , is defined as
, where is the residual
sum of squares ((y_true - y_pred)** 2).sum() and
is the total sum of squares ((y_true - y_true.mean()) ** 2).sum().
The best possible score is 1.0 and it can be negative (because the
model can be arbitrarily worse). A constant model that always predicts
the expected value of y, disregarding the input features, would get
a score of 0.0.
Test samples. For some estimators this may be a precomputed
kernel matrix or a list of generic objects instead with shape
(n_samples, n_samples_fitted), where n_samples_fitted
is the number of samples used in the fitting for the estimator.
True values for X.
Sample weights.
The score used when calling score on a regressor uses
multioutput='uniform_average' from version 0.23 to keep consistent
with default value of r2_score.
This influences the score method of all the multioutput
regressors (except for
MultiOutputRegressor).
Set output container.
Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.
Configure output of transform and fit_transform.
"default": Default output format of a transformer
"pandas": DataFrame output
"polars": Polars output
None: Transform configuration is unchanged
Estimator instance.
Set the parameters of this estimator.
The method works on simple estimators as well as on nested objects
(such as Pipeline). The latter have
parameters of the form <component>__<parameter> so that it’s
possible to update each component of a nested object.
Estimator parameters.
Estimator instance.
Configure whether metadata should be requested to be passed to the predict method.
Note that this method is only relevant when this estimator is used as a
sub-estimator within a meta-estimator and metadata routing is enabled
with enable_metadata_routing=True (see sklearn.set_config).
Please check the User Guide on how the routing
mechanism works.
The options for each parameter are:
True: metadata is requested, and passed to predict if provided. The request is ignored if metadata is not provided.
False: metadata is not requested and the meta-estimator will not pass it to predict.
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (sklearn.utils.metadata_routing.UNCHANGED) retains the
existing request. This allows you to change the request for some
parameters and not others.
Metadata routing for copy parameter in predict.
The updated object.
Configure whether metadata should be requested to be passed to the score method.
Note that this method is only relevant when this estimator is used as a
sub-estimator within a meta-estimator and metadata routing is enabled
with enable_metadata_routing=True (see sklearn.set_config).
Please check the User Guide on how the routing
mechanism works.
The options for each parameter are:
True: metadata is requested, and passed to score if provided. The request is ignored if metadata is not provided.
False: metadata is not requested and the meta-estimator will not pass it to score.
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (sklearn.utils.metadata_routing.UNCHANGED) retains the
existing request. This allows you to change the request for some
parameters and not others.
Metadata routing for sample_weight parameter in score.
The updated object.
Configure whether metadata should be requested to be passed to the transform method.
Note that this method is only relevant when this estimator is used as a
sub-estimator within a meta-estimator and metadata routing is enabled
with enable_metadata_routing=True (see sklearn.set_config).
Please check the User Guide on how the routing
mechanism works.
The options for each parameter are:
True: metadata is requested, and passed to transform if provided. The request is ignored if metadata is not provided.
False: metadata is not requested and the meta-estimator will not pass it to transform.
None: metadata is not requested, and the meta-estimator will raise an error if the user provides it.
str: metadata should be passed to the meta-estimator with this given alias instead of the original name.
The default (sklearn.utils.metadata_routing.UNCHANGED) retains the
existing request. This allows you to change the request for some
parameters and not others.
Metadata routing for copy parameter in transform.
The updated object.
Apply the dimension reduction.
Samples to transform.
Target vectors.
Whether to copy X and y, or perform in-place normalization.
Return x_scores if y is not given, (x_scores, y_scores) otherwise.
Principal Component Regression vs Partial Least Squares Regression
The transformed training targets.
The projection matrix used to transform X.
The projection matrix used to transform y.
The coefficients of the linear model such that y is approximated as
y = X @ coef_.T + intercept_.
The intercepts of the linear model such that y is approximated as
y = X @ coef_.T + intercept_.
Number of iterations of the power method, for each component.
Number of features seen during fit.
n_features_in_,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
of self.predict(X) w.r.t. y.