pycaret.classification.stack_models#

pycaret.classification.stack_models(estimator_list: list, meta_model=None, meta_model_fold: int | Any | None = 5, fold: int | Any | None = None, round: int = 4, method: str = 'auto', restack: bool = False, choose_better: bool = False, optimize: str = 'Accuracy', fit_kwargs: dict | None = None, groups: str | Any | None = None, probability_threshold: float | None = None, verbose: bool = True, return_train_score: bool = False) Any[source]#

This function trains a meta model over select estimators passed in the estimator_list parameter. 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
>>> juice = get_data('juice')
>>> from pycaret.classification import *
>>> exp_name = setup(data = juice,  target = 'Purchase')
>>> top3 = compare_models(n_select = 3)
>>> stacker = stack_models(top3)
estimator_list: list of scikit-learn compatible objects

List of trained model objects

meta_model: scikit-learn compatible object, default = None

When None, Logistic Regression is trained as a meta model.

meta_model_fold: integer or scikit-learn compatible CV generator, default = 5

Controls internal cross-validation. Can be an integer or a scikit-learn CV generator. If set to an integer, will use (Stratifed)KFold CV with that many folds. See scikit-learn documentation on Stacking for more details.

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.

round: int, default = 4

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

method: str, default = ‘auto’

When set to ‘auto’, it will invoke, for each estimator, ‘predict_proba’, ‘decision_function’ or ‘predict’ in that order. Other, manually pass one of the value from ‘predict_proba’, ‘decision_function’ or ‘predict’.

restack: bool, default = False

When set to False, only the predictions of estimators will be used as training data for the meta_model.

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 = ‘Accuracy’

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.

probability_threshold: float, default = None

Threshold for converting predicted probability to class label. It defaults to 0.5 for all classifiers unless explicitly defined in this parameter. Only applicable for binary classification.

verbose: bool, default = True

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

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.

Returns:

Trained Model

Warning

  • When method is not set to ‘auto’, it will check if the defined method is available for all estimators passed in estimator_list. If the method is not implemented by any estimator, it will raise an error.