mojolearn.StackingClassifier#

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

Bases: _StackingBase

Stack classifiers and train a final classifier on their cross-validated outputs.

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

Differs from scikit-learn: the default final_estimator is mojolearn’s LogisticRegression.

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

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

  • cv (int, splitter or iterable of (train, test), default None) – An int is the number of unshuffled stratified 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.

  • stack_method ({“auto”, “predict_proba”, “decision_function”, “predict”}, default “auto”) – The member method whose output is stacked. "auto" takes the first of predict_proba, decision_function, predict the member has. For a binary problem predict_proba contributes only its second column.

  • 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 (fitted on integer class codes).

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

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

  • final_estimator_ (estimator) – The fitted final classifier.

  • classes_ (array) – The class labels seen in fit, sorted.

  • 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[:, 0] + X[:, 1] > 1.0).astype(np.int32)
>>> model = mojolearn.StackingClassifier(
...     [("rf", mojolearn.RandomForestClassifier()),
...      ("dt", mojolearn.DecisionTreeClassifier())]).fit(X, y)
>>> model.predict_proba(X[:5]).shape
(5, 2)
__init__(estimators, final_estimator=None, *, cv=None, stack_method='auto', n_jobs=None, passthrough=False, verbose=0)[source]#

Methods

fit(X, y[, sample_weight])

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

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Predict class labels with the final estimator on the stacked outputs.

predict_proba(X)

The final estimator's class probabilities on the stacked outputs.

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 outputs, and the final estimator.

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

  • y (array of shape (n_samples,)) – Class labels.

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

Returns:

self

Raises:

ValueError – When a member’s output width differs between folds (a class missing from a fold’s training rows).

predict(X)[source]#

Predict class labels with the final estimator on the stacked outputs.

Returns:

array of shape (n_samples,) – Labels from classes_.

predict_proba(X)[source]#

The final estimator’s class probabilities on the stacked outputs.

Returns:

array of shape (n_samples, n_classes), float64