mojolearn.StackingRegressor#

class mojolearn.StackingRegressor(estimators, final_estimator=None, *, cv=None, n_jobs=None, passthrough=False, verbose=0)[source]#

Bases: _StackingBase

Stack regressors and train a final regressor on their cross-validated outputs.

Follows scikit-learn’s StackingRegressor. fit refits every member on all rows, and trains final_estimator on the members’ out-of-fold predict outputs (cross_val_predict). The members and folds fit one after another.

Differs from scikit-learn: the default final_estimator is mojolearn’s Ridge (scikit-learn uses RidgeCV).

Parameters:
  • estimators (list of (str, estimator)) – Named mojolearn regressors. Names must be unique; an entry whose estimator is the string "drop" is skipped.

  • final_estimator (estimator, default None) – The regressor fitted on the meta features; None uses Ridge().

  • cv (int, splitter or iterable of (train, test), default None) – An int is the number of unshuffled KFold folds (>= 2); None means 5. A splitter object’s split(X, y) or an iterable of index pairs is used as given, but its test sets must cover every row exactly once, else ValueError.

  • n_jobs (None) – Only None is accepted; any other value raises NotImplementedError.

  • passthrough (bool, default False) – Also give the final estimator the original features.

  • verbose (int, default 0) – Only 0 is accepted; any other value raises NotImplementedError.

Variables:
  • estimators_ (list) – The members refitted on all rows.

  • named_estimators_ (dict) – The fitted members by name.

  • stack_method_ (list of str) – The method stacked for each member ("predict").

  • final_estimator_ (estimator) – The fitted final regressor.

  • n_features_in_ (int) – Number of features seen in fit.

Examples

>>> import numpy as np, mojolearn
>>> rng = np.random.default_rng(0)
>>> X = rng.random((1000, 8), dtype=np.float32)
>>> y = X @ np.arange(8, dtype=np.float32)
>>> model = mojolearn.StackingRegressor(
...     [("rf", mojolearn.RandomForestRegressor()),
...      ("dt", mojolearn.DecisionTreeRegressor())]).fit(X, y)
>>> model.predict(X[:5]).shape
(5,)
__init__(estimators, final_estimator=None, *, cv=None, n_jobs=None, passthrough=False, verbose=0)[source]#

Methods

fit(X, y[, sample_weight])

Fit the members, their out-of-fold predictions, and the final estimator.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict with the final estimator on the stacked member predictions.

set_params(**params)

Set constructor parameters.

transform(X)

The meta features the final estimator reads.

vendor_used()

The GPU backend this estimator runs on.

fit(X, y, sample_weight=None)[source]#

Fit the members, their out-of-fold predictions, and the final estimator.

Parameters:
  • X (array of shape (n_samples, n_features)) – Training data, converted to float32.

  • y (array of shape (n_samples,)) – Targets, converted to float32.

  • sample_weight (None) – Only None is accepted; weights raise NotImplementedError.

Returns:

self

predict(X)[source]#

Predict with the final estimator on the stacked member predictions.

Returns:

array of shape (n_samples,)