pycaret.regression.ensemble_model#

pycaret.regression.ensemble_model(estimator, method: str = 'Bagging', fold: int | Any | None = None, n_estimators: int = 10, round: int = 4, choose_better: bool = False, optimize: str = 'R2', fit_kwargs: dict | None = None, groups: str | Any | None = None, verbose: bool = True, return_train_score: bool = False) Any[source]#

This function ensembles a given estimator. The output of this function is a score grid with CV scores by fold. Metrics evaluated during CV can be accessed using the get_metrics function. Custom metrics can be added or removed using add_metric and remove_metric function.

Example

>>> from pycaret.datasets import get_data
>>> boston = get_data('boston')
>>> from pycaret.regression import *
>>> exp_name = setup(data = boston,  target = 'medv')
>>> dt = create_model('dt')
>>> bagged_dt = ensemble_model(dt, method = 'Bagging')
estimator: scikit-learn compatible object

Trained model object

method: str, default = ‘Bagging’

Method for ensembling base estimator. It can be ‘Bagging’ or ‘Boosting’.

fold: int or scikit-learn compatible CV generator, default = None

Controls cross-validation. If None, the CV generator in the fold_strategy parameter of the setup function is used. When an integer is passed, it is interpreted as the ‘n_splits’ parameter of the CV generator in the setup function.

n_estimators: int, default = 10

The number of base estimators in the ensemble. In case of perfect fit, the learning procedure is stopped early.

round: int, default = 4

Number of decimal places the metrics in the score grid will be rounded to.

choose_better: bool, default = False

When set to True, the returned object is always better performing. The metric used for comparison is defined by the optimize parameter.

optimize: str, default = ‘R2’

Metric to compare for model selection when choose_better is True.

fit_kwargs: dict, default = {} (empty dict)

Dictionary of arguments passed to the fit method of the model.

groups: str or array-like, with shape (n_samples,), default = None

Optional group labels when GroupKFold is used for the cross validation. It takes an array with shape (n_samples, ) where n_samples is the number of rows in training dataset. When string is passed, it is interpreted as the column name in the dataset containing group labels.

return_train_score: bool, default = False

If False, returns the CV Validation scores only. If True, returns the CV training scores along with the CV validation scores. This is useful when the user wants to do bias-variance tradeoff. A high CV training score with a low corresponding CV validation score indicates overfitting.

verbose: bool, default = True

Score grid is not printed when verbose is set to False.

Returns:

Trained Model