PLSDA#

class chemotools.classification.PLSDA(n_components: int = 2, *, scale: bool = True, algorithm: int = 1, copy: bool = True, dtype: type = <class 'numpy.float64'>)[source]

Bases: ClassifierMixin, PLSRegression

PLS Discriminant Analysis (PLS-DA) classifier.

PLS-DA turns PLS regression into a classifier by regressing X against a one-hot encoded matrix of class labels [1], then assigning each sample to the class whose centroid (in the PLS score space) is closest [2].

This estimator wraps chemotools.regression.PLSRegression (itself backed by the Improved Kernel PLS algorithms from the ikpls package), so all parameters and fitted attributes from that class are available, including explained_x_variance_ratio_ and explained_y_variance_ratio_.

Parameters:
  • n_components (int, default=2) – Number of components to keep. Should be in [1, min(n_samples, n_features)].

  • scale (bool, default=True) – Whether to scale X and the one-hot encoded Y to unit standard deviation before fitting. Both are always mean-centered.

  • algorithm (int, default=1) – Improved Kernel PLS algorithm to use, either 1 or 2; see chemotools.regression.PLSRegression for details.

  • copy (bool, default=True) – Whether to copy X and Y in fit before applying centering and potentially scaling.

  • dtype (type, default=numpy.float64) – Floating point dtype used for the computations.

Variables:
  • classes (ndarray of shape (n_classes,)) – The class labels seen during fit.

  • centroids (ndarray of shape (n_classes, n_components)) – Mean PLS score vector for each class, used for nearest-centroid class assignment in predict.

References

Examples

>>> from chemotools.classification import PLSDA
>>> import numpy as np
>>>
>>> X = np.random.randn(100, 50)
>>> y = np.array([0] * 50 + [1] * 50)
>>>
>>> plsda = PLSDA(n_components=3)
>>> plsda.fit(X, y)
>>> predictions = plsda.predict(X)
>>> probabilities = plsda.predict_proba(X)

Notes

Inherited attributes:

All other fitted attributes (x_weights_, y_weights_, x_loadings_, y_loadings_, x_scores_, x_rotations_, y_rotations_, coef_, intercept_, n_features_in_, feature_names_in_, explained_x_variance_ratio_, explained_y_variance_ratio_) are inherited from chemotools.regression.PLSRegression. There is no y_scores_ attribute (see that class’s docstring for why).

Class assignment:

  • Classification uses a nearest-centroid rule in PLS score space rather than a fixed decision threshold, which generalizes cleanly to any number of classes without assuming an ordering between them.

  • predict_proba returns a softmax over negative distances to each class centroid. These are bounded, sum-to-one pseudo-probabilities useful for ranking, but are not calibrated probabilities in the statistical sense.

  • decision_function derives its scores from those same centroid distances (negated, so closer is higher). For binary problems it collapses this to a single signed score per sample, matching scikit-learn’s convention that the sign of decision_function agrees with predict.

See also

chemotools.regression.PLSRegression

Underlying PLS regression model.

Attributes

center_X

center_Y

ddof

scale_X

scale_Y

y_rotations_

The rotations mapping Y directly to its scores.

fit(X: ndarray, y: ndarray) → PLSDA[source]

Fit the PLS-DA model.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training vectors.

  • y (array-like of shape (n_samples,)) – Class labels.

Returns:

self – Fitted estimator with populated classes_ and centroids_.

Return type:

PLSDA

transform(X: ndarray, y: ndarray | None = None) → ndarray[source]

Project X onto the fitted latent components (X-scores only).

Unlike the underlying PLSRegression, PLS-DA does not support transforming an external y alongside X: the y consumed during fit is an internally constructed one-hot class matrix, not a quantity meaningful to transform for new data. Any y passed here is ignored, and only the X-scores are returned (never the (x_scores, y_scores) tuple the base class can return).

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Samples to transform.

  • y (ignored) – Present for API consistency; always ignored.

Returns:

X_scores – X projected into the latent space (X-scores).

Return type:

ndarray of shape (n_samples, n_components)

fit_transform(X: ndarray, y: ndarray) → ndarray[source]

Fit the model to X and y, then return the X-scores.

Overridden to avoid delegating to the underlying regression backend’s fit_transform, which would incorrectly forward the raw class labels into transform a second time.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training vectors.

  • y (array-like of shape (n_samples,)) – Class labels.

Returns:

X_scores – X projected into the latent space (X-scores).

Return type:

ndarray of shape (n_samples, n_components)

predict(X: ndarray) → ndarray[source]

Predict class labels for X.

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples to classify.

Returns:

y_pred – Predicted class label for each sample, assigned to the nearest class centroid in PLS score space.

Return type:

ndarray of shape (n_samples,)

predict_proba(X: ndarray) → ndarray[source]

Estimate pseudo-probabilities for each class.

These are derived from a softmax over negative distances to each class centroid in PLS score space, and are not calibrated probabilities in the statistical sense — see Notes.

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples to classify.

Returns:

proba – Pseudo-probability of each class, rows summing to 1.

Return type:

ndarray of shape (n_samples, n_classes)

decision_function(X: ndarray) → ndarray[source]

Return per-class confidence scores based on distance to centroids.

Scores are derived from the same nearest-centroid distances used by predict, so the class (or sign, for binary problems) that decision_function favors always agrees with predict.

For binary classification, returns a single signed score per sample: positive values favor classes_[1], negative values favor classes_[0], matching scikit-learn’s convention for binary decision functions.

For multiclass problems, returns one score per class, where the class with the highest score is the predicted class (mirroring the nearest-centroid rule used by predict).

Parameters:

X (array-like of shape (n_samples, n_features)) – Samples to classify.

Returns:

scores – of shape (n_samples, n_classes) for multiclass problems. Higher values indicate greater confidence in the corresponding class; see above for how to interpret them.

Return type:

ndarray of shape (n_samples,) for binary problems, or

set_fit_request() → PLSDA

No-op.

Calling this method has no effect.

Returns:

self – The updated object.

Return type:

object

set_score_request(*, sample_weight: bool | None | str = '$UNCHANGED$') → PLSDA

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.

Added in version 1.3.

Parameters:

sample_weight (str, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED) – Metadata routing for sample_weight parameter in score.

Returns:

self – The updated object.

Return type:

object