mojolearn.BaggingClassifier#
- class mojolearn.BaggingClassifier(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:
_BaggingBaseA bagging ensemble of classifiers.
Follows scikit-learn’s
BaggingClassifier: each member is fitted on a random draw of rows and features, andpredict_probaaverages the members’predict_proba(a member withoutpredict_probavotes withpredict), returned as float64. Any mojolearn classifier can be a member; the default isDecisionTreeClassifier(). When the members are best-splitter decision trees over every feature, withoutsample_weight,oob_scoreor 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
DecisionTreeClassifier().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(ValueErrorotherwise).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
RandomStateobject raisesValueError.verbose (int, default=0) – Only 0 is accepted.
- Variables:
classes_ (array of shape (n_classes,)) – Class labels seen in
fit.n_classes_ (int) – Number of classes.
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 accuracy (with
oob_score=True).oob_decision_function_ (array of shape (n_samples, n_classes)) – Out-of-bag class scores, normalized per row (with
oob_score=True). A row that no member left out gets the uniform 1 / n_classes, where scikit-learn would divide 0 by 0.
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.BaggingClassifier(n_estimators=20, random_state=0).fit(X, y) >>> proba = model.predict_proba(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)Predict the class with the highest mean probability.
Mean of the members' class probabilities, 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,)) – Class labels; at least two classes are required.
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.
- Returns:
self