Documentation
Classes
BaggingClassifier

BaggingClassifier

A Bagging classifier.

A Bagging classifier is an ensemble meta-estimator that fits base classifiers each on random subsets of the original dataset and then aggregate their individual predictions (either by voting or by averaging) to form a final prediction. Such a meta-estimator can typically be used as a way to reduce the variance of a black-box estimator (e.g., a decision tree), by introducing randomization into its construction procedure and then making an ensemble out of it.

This algorithm encompasses several works from the literature. When random subsets of the dataset are drawn as random subsets of the samples, then this algorithm is known as Pasting [1]. If samples are drawn with replacement, then the method is known as Bagging [2]. When random subsets of the dataset are drawn as random subsets of the features, then the method is known as Random Subspaces [3]. Finally, when base estimators are built on subsets of both samples and features, then the method is known as Random Patches [4].

Read more in the User Guide.

Python Reference (opens in a new tab)

Constructors

constructor()

Signature

new BaggingClassifier(opts?: object): BaggingClassifier;

Parameters

NameTypeDescription
opts?object-
opts.base_estimator?anyUse estimator instead. Default Value 'deprecated'
opts.bootstrap?booleanWhether samples are drawn with replacement. If false, sampling without replacement is performed. Default Value true
opts.bootstrap_features?booleanWhether features are drawn with replacement. Default Value false
opts.estimator?anyThe base estimator to fit on random subsets of the dataset. If undefined, then the base estimator is a DecisionTreeClassifier.
opts.max_features?numberThe number of features to draw from X to train each base estimator ( without replacement by default, see bootstrap\_features for more details). Default Value 1
opts.max_samples?numberThe number of samples to draw from X to train each base estimator (with replacement by default, see bootstrap for more details). Default Value 1
opts.n_estimators?numberThe number of base estimators in the ensemble. Default Value 10
opts.n_jobs?numberThe number of jobs to run in parallel for both fit and predict. undefined means 1 unless in a joblib.parallel\_backend (opens in a new tab) context. \-1 means using all processors. See Glossary for more details.
opts.oob_score?booleanWhether to use out-of-bag samples to estimate the generalization error. Only available if bootstrap=true. Default Value false
opts.random_state?numberControls the random resampling of the original dataset (sample wise and feature wise). If the base estimator accepts a random\_state attribute, a different seed is generated for each instance in the ensemble. Pass an int for reproducible output across multiple function calls. See Glossary.
opts.verbose?numberControls the verbosity when fitting and predicting. Default Value 0
opts.warm_start?booleanWhen set to true, reuse the solution of the previous call to fit and add more estimators to the ensemble, otherwise, just fit a whole new ensemble. See the Glossary. Default Value false

Returns

BaggingClassifier

Defined in: generated/ensemble/BaggingClassifier.ts:27 (opens in a new tab)

Properties

_isDisposed

boolean = false

Defined in: generated/ensemble/BaggingClassifier.ts:25 (opens in a new tab)

_isInitialized

boolean = false

Defined in: generated/ensemble/BaggingClassifier.ts:24 (opens in a new tab)

_py

PythonBridge

Defined in: generated/ensemble/BaggingClassifier.ts:23 (opens in a new tab)

id

string

Defined in: generated/ensemble/BaggingClassifier.ts:20 (opens in a new tab)

opts

any

Defined in: generated/ensemble/BaggingClassifier.ts:21 (opens in a new tab)

Accessors

classes_

The classes labels.

Signature

classes_(): Promise<ArrayLike>;

Returns

Promise<ArrayLike>

Defined in: generated/ensemble/BaggingClassifier.ts:587 (opens in a new tab)

estimator_

The base estimator from which the ensemble is grown.

Signature

estimator_(): Promise<any>;

Returns

Promise<any>

Defined in: generated/ensemble/BaggingClassifier.ts:452 (opens in a new tab)

estimators_

The collection of fitted base estimators.

Signature

estimators_(): Promise<any>;

Returns

Promise<any>

Defined in: generated/ensemble/BaggingClassifier.ts:533 (opens in a new tab)

estimators_features_

The subset of drawn features for each base estimator.

Signature

estimators_features_(): Promise<any>;

Returns

Promise<any>

Defined in: generated/ensemble/BaggingClassifier.ts:560 (opens in a new tab)

feature_names_in_

Names of features seen during fit. Defined only when X has feature names that are all strings.

Signature

feature_names_in_(): Promise<ArrayLike>;

Returns

Promise<ArrayLike>

Defined in: generated/ensemble/BaggingClassifier.ts:506 (opens in a new tab)

n_classes_

The number of classes.

Signature

n_classes_(): Promise<number | any[]>;

Returns

Promise<number | any[]>

Defined in: generated/ensemble/BaggingClassifier.ts:614 (opens in a new tab)

n_features_in_

Number of features seen during fit.

Signature

n_features_in_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/ensemble/BaggingClassifier.ts:479 (opens in a new tab)

oob_decision_function_

Decision function computed with out-of-bag estimate on the training set. If n_estimators is small it might be possible that a data point was never left out during the bootstrap. In this case, oob\_decision\_function\_ might contain NaN. This attribute exists only when oob\_score is true.

Signature

oob_decision_function_(): Promise<ArrayLike[]>;

Returns

Promise<ArrayLike[]>

Defined in: generated/ensemble/BaggingClassifier.ts:668 (opens in a new tab)

oob_score_

Score of the training dataset obtained using an out-of-bag estimate. This attribute exists only when oob\_score is true.

Signature

oob_score_(): Promise<number>;

Returns

Promise<number>

Defined in: generated/ensemble/BaggingClassifier.ts:641 (opens in a new tab)

py

Signature

py(): PythonBridge;

Returns

PythonBridge

Defined in: generated/ensemble/BaggingClassifier.ts:110 (opens in a new tab)

Signature

py(pythonBridge: PythonBridge): void;

Parameters

NameType
pythonBridgePythonBridge

Returns

void

Defined in: generated/ensemble/BaggingClassifier.ts:114 (opens in a new tab)

Methods

decision_function()

Average of the decision functions of the base classifiers.

Signature

decision_function(opts: object): Promise<ArrayLike[]>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeThe training input samples. Sparse matrices are accepted only if they are supported by the base estimator.

Returns

Promise<ArrayLike[]>

Defined in: generated/ensemble/BaggingClassifier.ts:198 (opens in a new tab)

dispose()

Disposes of the underlying Python resources.

Once dispose() is called, the instance is no longer usable.

Signature

dispose(): Promise<void>;

Returns

Promise<void>

Defined in: generated/ensemble/BaggingClassifier.ts:181 (opens in a new tab)

fit()

Build a Bagging ensemble of estimators from the training set (X, y).

Signature

fit(opts: object): Promise<any>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeThe training input samples. Sparse matrices are accepted only if they are supported by the base estimator.
opts.sample_weight?ArrayLikeSample weights. If undefined, then samples are equally weighted. Note that this is supported only if the base estimator supports sample weighting.
opts.y?ArrayLikeThe target values (class labels in classification, real numbers in regression).

Returns

Promise<any>

Defined in: generated/ensemble/BaggingClassifier.ts:236 (opens in a new tab)

init()

Initializes the underlying Python resources.

This instance is not usable until the Promise returned by init() resolves.

Signature

init(py: PythonBridge): Promise<void>;

Parameters

NameType
pyPythonBridge

Returns

Promise<void>

Defined in: generated/ensemble/BaggingClassifier.ts:123 (opens in a new tab)

predict()

Predict class for X.

The predicted class of an input sample is computed as the class with the highest mean predicted probability. If base estimators do not implement a predict\_proba method, then it resorts to voting.

Signature

predict(opts: object): Promise<ArrayLike>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeThe training input samples. Sparse matrices are accepted only if they are supported by the base estimator.

Returns

Promise<ArrayLike>

Defined in: generated/ensemble/BaggingClassifier.ts:287 (opens in a new tab)

predict_log_proba()

Predict class log-probabilities for X.

The predicted class log-probabilities of an input sample is computed as the log of the mean predicted class probabilities of the base estimators in the ensemble.

Signature

predict_log_proba(opts: object): Promise<ArrayLike[]>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeThe training input samples. Sparse matrices are accepted only if they are supported by the base estimator.

Returns

Promise<ArrayLike[]>

Defined in: generated/ensemble/BaggingClassifier.ts:324 (opens in a new tab)

predict_proba()

Predict class probabilities for X.

The predicted class probabilities of an input sample is computed as the mean predicted class probabilities of the base estimators in the ensemble. If base estimators do not implement a predict\_proba method, then it resorts to voting and the predicted class probabilities of an input sample represents the proportion of estimators predicting each class.

Signature

predict_proba(opts: object): Promise<ArrayLike[]>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLikeThe training input samples. Sparse matrices are accepted only if they are supported by the base estimator.

Returns

Promise<ArrayLike[]>

Defined in: generated/ensemble/BaggingClassifier.ts:364 (opens in a new tab)

score()

Return the mean accuracy on the given test data and labels.

In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.

Signature

score(opts: object): Promise<number>;

Parameters

NameTypeDescription
optsobject-
opts.X?ArrayLike[]Test samples.
opts.sample_weight?ArrayLikeSample weights.
opts.y?ArrayLikeTrue labels for X.

Returns

Promise<number>

Defined in: generated/ensemble/BaggingClassifier.ts:403 (opens in a new tab)