mojolearn.PLSRegression#
- class mojolearn.PLSRegression(n_components=2, *, scale=True, max_iter=500, tol=1e-06, copy=True, numeric_mode=None)#
Partial least squares regression.
Follows scikit-learn’s
PLSRegression(NIPALS algorithm). If Y’s residual becomes constant beforen_componentsare found, a warning is issued and fewer components are kept.- Parameters:
n_components (int, default 2) – Number of components to keep, between 1 and
n_features.scale (bool, default True) – Scale X and Y to unit variance.
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). Inidenticalmode, 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)
y_scores_ (x_scores_,) – Scores of the training data.
Examples
>>> import numpy as np, mojolearn >>> rng = np.random.default_rng(0) >>> X = rng.random((1000, 8), dtype=np.float32) >>> y = X @ rng.random(8, dtype=np.float32) >>> pred = mojolearn.PLSRegression(n_components=2).fit(X, y).predict(X)
Methods
fit(X[, y, Y])Fit the model to X and the targets (
yorY).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=None, Y=None)#
Fit the model to X and the targets (
yorY).- Parameters:
X (array-like of shape (n_samples, n_features))
y, Y (array-like of shape (n_samples,) or (n_samples, n_targets)) – Targets (either name).
- Returns:
self
- 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.