mojolearn.PLSCanonical#

class mojolearn.PLSCanonical(n_components=2, *, scale=True, algorithm='nipals', max_iter=500, tol=1e-06, copy=True, numeric_mode=None)#

Partial least squares in canonical form.

Follows scikit-learn’s PLSCanonical: finds paired directions in X and Y with maximal covariance, deflating both blocks symmetrically.

Parameters:
  • n_components (int, default 2) – Number of components to keep, between 1 and min(n_samples, n_features, n_targets).

  • scale (bool, default True) – Scale X and Y to unit variance.

  • algorithm ({“nipals”, “svd”}, default “nipals”) – How the first singular vectors of each step are found. Any other value raises ValueError at construction.

  • max_iter (int, default 500) – Maximum NIPALS iterations per component.

  • tol (float, default 1e-06) – NIPALS convergence tolerance.

  • copy (bool, default True) – Accepted for scikit-learn compatibility; inputs are never modified.

  • numeric_mode ({“identical”, “fast”} or None, default None) – Numeric mode for this estimator. None uses the package default ("identical" unless changed). In identical mode, mojolearn returns the same bits on Apple, NVIDIA and AMD GPUs. "fast" is available on Apple GPUs only.

Variables:
  • y_weights_ (x_weights_,)

  • y_loadings_ (x_loadings_,)

  • y_rotations_ (x_rotations_,)

  • coef_ (ndarray of shape (n_targets, n_features))

  • intercept_ (ndarray of shape (n_targets,))

  • n_iter_ (list of int) – NIPALS iterations for each component.

  • n_features_in_ (int)

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X, Y = rng.random((1000, 8), dtype=np.float32), rng.random((1000, 3), dtype=np.float32)
>>> xs, ys = mojolearn.PLSCanonical(n_components=2).fit(X, Y).transform(X, Y)

Methods

fit(X, Y)

Fit the model to X and Y.

fit_transform(X[, y])

Fit the model, then return transform(X, y).

get_params([deep])

Return this estimator's constructor parameters.

inverse_transform(X[, y, Y])

Map scores back to the original X (and optionally Y) space.

predict(X[, copy])

Predict targets for X.

set_params(**params)

Set constructor parameters by keyword.

transform(X[, y, Y, copy])

Project X (and optionally Y) onto the fitted rotations.

fit(X, Y)#

Fit the model to X and Y.

Parameters:
  • X (array-like of shape (n_samples, n_features))

  • Y (array-like of shape (n_samples,) or (n_samples, n_targets))

Returns:

self

Raises:

ValueError – If Y is missing or n_components is out of range.

fit_transform(X, y=None)#

Fit the model, then return transform(X, y).

Parameters:
  • X (array-like of shape (n_samples, n_features))

  • y (array-like of shape (n_samples,) or (n_samples, n_targets))

Returns:

x_scores, y_scores (ndarray of shape (n_samples, n_components), float32)

get_params(deep=True)#

Return this estimator’s constructor parameters.

Parameters:

deep (bool, default True) – Accepted for scikit-learn compatibility; it has no effect.

Returns:

dict – Parameter name to current value.

inverse_transform(X, y=None, Y=None)#

Map scores back to the original X (and optionally Y) space.

Parameters:
  • X (array-like of shape (n_samples, n_components)) – X scores.

  • y, Y (array-like of shape (n_samples, n_components), optional) – Y scores (either name).

Returns:

  • X_reconstructed (ndarray of shape (n_samples, n_features), float32)

  • Y_reconstructed (ndarray of shape (n_samples, n_targets), float32) – Returned only when Y scores are given.

predict(X, copy=True)#

Predict targets for X.

Parameters:
  • X (array-like of shape (n_samples, n_features))

  • copy (bool, default True) – Accepted for scikit-learn compatibility; X is never modified.

Returns:

ndarray of shape (n_samples,) or (n_samples, n_targets), float32 – One-dimensional when the model was fitted with a 1-D Y.

set_params(**params)#

Set constructor parameters by keyword.

Raises:

ValueError – If a keyword is not a parameter of this estimator.

Returns:

self

transform(X, y=None, Y=None, copy=True)#

Project X (and optionally Y) onto the fitted rotations.

Parameters:
  • X (array-like of shape (n_samples, n_features))

  • y, Y (array-like of shape (n_samples, n_targets), optional) – Targets to project as well (either name).

  • copy (bool, default True) – Accepted for scikit-learn compatibility; inputs are never modified.

Returns:

  • x_scores (ndarray of shape (n_samples, n_components), float32)

  • y_scores (ndarray of shape (n_samples, n_components), float32) – Returned only when Y is given.