ArPls#

class chemotools.baseline.ArPls(lam: float = 10000.0, ratio: float = 0.01, nr_iterations: int = 100, solver_type: Literal['banded', 'sparse'] = 'banded', max_iter_after_warmstart: int = 20)[source]

Bases: _BaselineWhittakerMixin, _BaseWhittaker

Asymmetrically Reweighted Penalized Least Squares (ArPLS) baseline correction.

This algorithm estimates and removes smooth baselines from spectroscopic data by iteratively reweighting residuals in a penalized least squares framework. A second-order difference operator is used as the penalty term, which promotes a smooth baseline estimate.

The Whittaker smoothing step can be solved using either:

  • a banded solver (fast and memory-efficient, recommended for most spectra)

  • a sparse LU solver (more stable for ill-conditioned problems)

For efficiency, the algorithm supports warm-starting: when processing multiple spectra with similar baseline structure, weights from a previous fit can be reused, typically reducing the number of iterations needed.

Parameters:
  • lam (float, default=1e4) – Regularization parameter controlling smoothness of the baseline. Larger values yield smoother baselines.

  • ratio (float, default=0.01) – Convergence threshold for weight updates.

  • nr_iterations (int, default=100) – Maximum number of reweighting iterations.

  • solver_type (Literal["banded", "sparse"], default="banded") – If “banded”, use the banded solver for Whittaker smoothing. If “sparse”, use a sparse LU decomposition.

  • max_iter_after_warmstart (int, default=20) – Maximum iterations allowed when warm-starting from previous weights.

Variables:
  • n_features_in (int) – The number of features in the input data.

  • DtD (np.ndarray) –

    The precomputed banded representation of \(D^T D\) for the second-order difference operator.

    • Stored as a banded representation (solveh_banded format) if solver_type='banded'

    • Stored as a scipy.sparse CSC matrix if solver_type='sparse'

  • self.w_init (np.ndarray) – The weights set for warm-starting.

References

[1] Sung-June Baek, Aaron Park, Young-Jin Ahn, Jaebum Choo.

“Baseline correction using asymmetrically reweighted penalized least squares smoothing.” Analyst 140 (1), 250–257 (2015).

Examples

>>> from chemotools.baseline import ArPls
>>> from chemotools.datasets import load_fermentation_train
>>> # Load sample data
>>> X, _ = load_fermentation_train()
>>> # Instantiate the transformer
>>> transformer = ArPls(lam=1e4, nr_iterations=100)
ArPls()
>>> transformer.fit(X)
>>> # Generate baseline-corrected data
>>> X_corrected = transformer.transform(X)

Attributes

w_init_

fit(X: ndarray, y=None) ArPls[source]

Fit ArPLS model to spectra.

Parameters:
  • X (np.ndarray of shape (n_samples, n_features)) – The input spectra to fit the model to.

  • y (None) – Ignored to align with API.

Returns:

self – Fitted estimator.

Return type:

ArPlS

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

Apply ArPLS baseline correction.

Parameters:
  • X (np.ndarray of shape (n_samples, n_features)) – The input spectra to transform.

  • y (None) – Ignored to align with API.

Returns:

X_transformed – The baseline-corrected spectra.

Return type:

np.ndarray of shape (n_samples, n_features)