mojolearn.BaggingRegressor#

class mojolearn.BaggingRegressor(estimator=None, n_estimators=10, *, max_samples=1.0, max_features=1.0, bootstrap=True, bootstrap_features=False, oob_score=False, warm_start=False, n_jobs=None, random_state=None, verbose=0)[source]#

Bases: _BaggingBase

A bagging ensemble of regressors.

Follows scikit-learn’s BaggingRegressor: each member is fitted on a random draw of rows and features, and predict is the members’ mean, returned as float64. Any mojolearn regressor can be a member; the default is DecisionTreeRegressor(). When the members are best-splitter decision trees over every feature, without sample_weight, oob_score or feature bootstrap, the whole bag is grown as one random forest fit.

Parameters:
  • estimator (estimator or None, default=None) – The member to clone. None uses DecisionTreeRegressor().

  • n_estimators (int, default=10) – Number of members; must be at least 1.

  • max_samples (int or float, default=1.0) – Rows drawn for each member: an int count, or a fraction in (0, 1] of the rows.

  • max_features (int or float, default=1.0) – Features drawn for each member: an int count, or a fraction in (0, 1] of the features.

  • bootstrap (bool, default=True) – Draw rows with replacement.

  • bootstrap_features (bool, default=False) – Draw features with replacement.

  • oob_score (bool, default=False) – Score the ensemble on the rows each member did not draw. Requires bootstrap=True (ValueError otherwise).

  • warm_start (bool, default=False) – Only False is accepted; True raises NotImplementedError.

  • n_jobs (None, default=None) – Only None is accepted; members are fitted one after another on the device.

  • random_state (int or None, default=None) – Seed for the row and feature draws and the members’ seeds. None means seed 0, so repeated fits are the same (unlike scikit-learn). A RandomState object raises ValueError.

  • verbose (int, default=0) – Only 0 is accepted.

Variables:
  • estimators_ (list of estimators) – The fitted members.

  • estimators_features_ (list of arrays) – The feature indices each member was fitted on.

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

  • oob_score_ (float) – Out-of-bag R^2 (with oob_score=True).

  • oob_prediction_ (array of shape (n_samples,)) – Out-of-bag prediction of each row (with oob_score=True).

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)
>>> model = mojolearn.BaggingRegressor(n_estimators=20, random_state=0).fit(X, y)
>>> pred = model.predict(X)
__init__(estimator=None, n_estimators=10, *, max_samples=1.0, max_features=1.0, bootstrap=True, bootstrap_features=False, oob_score=False, warm_start=False, n_jobs=None, random_state=None, verbose=0)[source]#

Methods

fit(X, y[, sample_weight])

Fit the bag.

get_params([deep])

numeric_mode_used()

The numeric mode this estimator runs in.

predict(X)

Mean of the members' predictions, float64.

set_params(**params)

Set constructor parameters.

vendor_used()

The GPU backend this estimator runs on.

Attributes

estimators_

The fitted members, as a list of estimators.

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

Fit the bag.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data, read as float32.

  • y (array-like of shape (n_samples,)) – Target values, read as float32.

  • sample_weight (array-like of shape (n_samples,), default=None) – Per-row weights, passed to each member for the rows it drew. The member must accept sample_weight (the default DecisionTreeRegressor does not).

Returns:

self

predict(X)[source]#

Mean of the members’ predictions, float64.