Optimize
Optimization functions in PyCaret
Last updated
Optimization functions in PyCaret
Last updated
This function tunes the hyperparameters of the model. The output of this function is a scoring grid with cross-validated scores by fold. The best model is selected based on the metric defined in optimize
parameter. Metrics evaluated during cross-validation can be accessed using the get_metrics
function. Custom metrics can be added or removed using add_metric
and remove_metric
function.
To compare the hyperparameters.
Hyperparameter tuning at the end of the day is an optimization that is constrained by the number of iterations, which eventually depends on how much time and resources you have available. The number of iterations is defined by n_iter
. By default, it is set to 10
.
When you are tuning the hyperparameters of the model, you must know which metric to optimize for. That can be defined under optimize
parameter. By default, it is set to Accuracy
for classification experiments and R2
for regression.
The tuning grid for hyperparameters is already defined by PyCaret for all the models in the library. However, if you wish you can define your own search space by passing a custom grid using custom_grid
parameter.
PyCaret integrates seamlessly with many different libraries for hyperparameter tuning. This gives you access to many different types of search algorithms including random, bayesian, optuna, TPE, and a few others. All of this just by changing a parameter. By default, PyCaret using RandomGridSearch
from the sklearn and you can change that by using search_library
and search_algorithm
parameter in the tune_model
function.
By default PyCaret's tune_model
function only returns the best model as selected by the tuner. Sometimes you may need access to the tuner object as it may contain important attributes, you can use return_tuner
parameter.
Often times the tune_model
will not improve the model performance. In fact, it may end up making performance worst than the model with default hyperparameters. This may be problematic when you are not actively experimenting in the Notebook rather you have a python script that runs a workflow of create_model
--> tune_model
or compare_models
--> tune_model
. To overcome this issue, you can use choose_better
. When set to True
it will always return a better performing model meaning that if hyperparameter tuning doesn't improve the performance, it will return the input model.
NOTE: choose_better
doesn't affect the scoring grid that is displayed on the screen. The scoring grid will always present the performance of the best model as selected by the tuner, regardless of the fact that output performance < input performance.
This function ensembles a given estimator. The output of this function is a scoring 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.
The model returned by this is the same as above, however, the performance evaluation is done using 5 fold cross-validation.
Bagging, also known as Bootstrap aggregating, is a machine learning ensemble meta-algorithm designed to improve the stability and accuracy of machine learning algorithms used in statistical classification and regression. It also reduces variance and helps to avoid overfitting. Although it is usually applied to decision tree methods, it can be used with any type of method. Bagging is a special case of the model averaging approach.
Boosting is an ensemble meta-algorithm for primarily reducing bias and variance in supervised learning. Boosting is in the family of machine learning algorithms that convert weak learners to strong ones. A weak learner is defined to be a classifier that is only slightly correlated with the true classification (it can label examples better than random guessing). In contrast, a strong learner is a classifier that is arbitrarily well-correlated with the true classification.
There are two possible ways you can ensemble your machine learning model with ensemble_model
. You can define this in the method
parameter.
By default, PyCaret uses 10 estimators for both Bagging
or Boosting
. You can increase that by changing n_estimators
parameter.
Often times the ensemble_model
will not improve the model performance. In fact, it may end up making performance worst than the model with ensembling. This may be problematic when you are not actively experimenting in the Notebook rather you have a python script that runs a workflow of create_model
--> ensemble_model
or compare_models
--> ensemble_model
. To overcome this issue, you can use choose_better
. When set to True
it will always return a better performing model meaning that if hyperparameter tuning doesn't improve the performance, it will return the input model.
Notice that with choose_better = True
the model returned from the ensemble_model
is a simple LinearRegression
instead of BaggedRegressor
. This is because the performance of the model didn't improve after ensembling and hence input model is returned.
This function trains a Soft Voting / Majority Rule classifier for select models passed in the estimator_list
parameter. The output of this function is a scoring 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.
The model returned by this is the same as above, however, the performance evaluation is done using 5 fold cross-validation.
You can also automatically generate the list of input estimators using the compare_models function. The benefit of this is that you do not have the change your script at all. Every time the top N models are used as an input list.
Notice here what happens. We passed compare_models(n_select = 3
as an input to blend_models
. What happened internally is that the compare_models
function got executed first and the top 3 models are then passed as an input to the blend_models
function.
In this example, the top 3 models as evaluated by the compare_models
are LogisticRegression
, LinearDiscriminantAnalysis
, and RandomForestClassifier
.
When method = 'soft'
, it predicts the class label based on the argmax of the sums of the predicted probabilities, which is recommended for an ensemble of well-calibrated classifiers.
When the method = 'hard'
, it uses the predictions (hard labels) from input models instead of probabilities.
The default method is set to auto
which means it will try to use soft
method and fall back to hard
if the former is not supported, this may happen when one of your input models does not support predict_proba
attribute.
NOTE: Method parameter is only available in Classification module.
By default, all the input models are given equal weight when blending them but you can explicitly pass the weights to be given to each input model.
You can also tune the weights of the blender using the tune_model
.
Often times the blend_models
will not improve the model performance. In fact, it may end up making performance worst than the model with blending. This may be problematic when you are not actively experimenting in the Notebook rather you have a python script that runs a workflow of compare_models
--> blend_models
. To overcome this issue, you can use choose_better
. When set to True
it will always return a better performing model meaning that if blending the models doesn't improve the performance, it will return the single best performing input model.
Notice that because choose_better=True
the final model returned by this function is LogisticRegression
instead of VotingClassifier
because the performance of Logistic Regression was most optimized out of all the given input models plus the blender.
This function trains a meta-model over select estimators passed in the estimator_list
parameter. The output of this function is a scoring 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.
The model returned by this is the same as above, however, the performance evaluation is done using 5 fold cross-validation.
You can also automatically generate the list of input estimators using the compare_models function. The benefit of this is that you do not have the change your script at all. Every time the top N models are used as an input list.
Notice here what happens. We passed compare_models(n_select = 3
as an input to stack_models
. What happened internally is that the compare_models
function got executed first and the top 3 models are then passed as an input to the stack_models
function.
In this example, the top 3 models as evaluated by the compare_models
are LogisticRegression
, RandomForestClassifier
, and LGBMClassifier
.
There are a few different methods you can explicitly choose for stacking or pass auto
to be automatically determined. When set to auto
, it will invoke, for each model, predict_proba
, decision_function
or predict
function in that order. Alternatively, you can define the method explicitly.
When no meta_model
is passed explicitly, LogisticRegression
is used for Classification experiments and LinearRegression
is used for Regression experiments. You can also pass a specific model to be used as a meta-model.
There are two ways you can stack models. (i) only the predictions of input models will be used as training data for meta-model, (ii) predictions as well as the original training data is used for training meta-model.
This function optimizes the probability threshold for a trained model. It iterates over performance metrics at different probability_threshold
with a step size defined in grid_interval
parameter. This function will display a plot of the performance metrics at each probability threshold and returns the best model based on the metric defined under optimize
parameter.
This function calibrates the probability of a given model using isotonic or logistic regression. The output of this function is a scoring 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.