nirs4all.operators.models.residual module

Residual model-combination operator.

Fits a BASE model, computes leakage-safe out-of-fold (OOF) residuals r = y - yhat_base_oof, trains a LEARNER on those residuals, and predicts base(X) + lambda * gate * learner(X). Both stages are ordinary nirs4all models routed through their own framework controllers, so the learner can be ANY nirs4all model (sklearn, the PyTorch nicon CNN, TabPFN, XGBoost, MLP) with no wrapper.

The combination is a first-class pipeline construct and composes under _cartesian_ / _or_:

{"residual": {"base": {"_or_": [PLSRegression(10), Ridge()]},
              "learner": {"_or_": [RandomForestRegressor(), nicon, TabPFNRegressor()]}}}

expands to every base x learner combination.

Example

>>> from nirs4all.operators.models import ResidualModel
>>> from sklearn.cross_decomposition import PLSRegression
>>> from sklearn.ensemble import RandomForestRegressor
>>> pipeline = [
...     KFold(n_splits=5),
...     {"model": ResidualModel(base=PLSRegression(10), learner=RandomForestRegressor())},
... ]