docstr: This function calibrates the probability of a given estimator using isotonic
  or logistic regression. 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. The ouput of the original estimator
  and the calibrated estimator (created using this function) might not differ much.
  In order to see the calibration differences, use 'calibration' plot in ``plot_model``
  to see the difference before and after.
  
  
  Example
  -------
  >>> from pycaret.datasets import get_data
  >>> juice = get_data('juice')
  >>> from pycaret.classification import *
  >>> exp_name = setup(data = juice,  target = 'Purchase')
  >>> dt = create_model('dt')
  >>> calibrated_dt = calibrate_model(dt)
  
  
  estimator: scikit-learn compatible object
      Trained model object
  
  
  method: str, default = 'sigmoid'
      The method to use for calibration. Can be 'sigmoid' which corresponds to
      Platt's method or 'isotonic' which is a non-parametric approach.
  
  
  calibrate_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.
  
  
  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.
  
  
  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
  
  
  Warnings
  --------
  - Avoid isotonic calibration with too few calibration samples (< 1000) since it
  tends to overfit.
docstr_tok: ["This","function","calibrates","the","probability","of","a","given","estimator","using","isotonic","or","logistic","regression",".","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",".","The","ouput","of","the","original","estimator","and","the","calibrated","estimator","(","created","using","this","function",")","might","not","differ","much",".","In","order","to","see","the","calibration","differences",",","use","'calibration","'","plot","in","``","plot_model","``","to","see","the","difference","before","and","after",".","Example","--","--","--","-",">",">",">","from","pycaret.datasets","import","get_data",">",">",">","juice","=","get_data","(","'juice","'",")",">",">",">","from","pycaret.classification","import","*",">",">",">","exp_name","=","setup","(","data","=","juice",",","target","=","'Purchase","'",")",">",">",">","dt","=","create_model","(","'dt","'",")",">",">",">","calibrated_dt","=","calibrate_model","(","dt",")","estimator",":","scikit-learn","compatible","object","Trained","model","object","method",":","str",",","default","=","'sigmoid'","The","method","to","use","for","calibration",".","Can","be","'sigmoid","'","which","corresponds","to","Platt","'s","method","or","'isotonic","'","which","is","a","non-parametric","approach",".","calibrate_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",".","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",".","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","Warnings","--","--","--","--","-","Avoid","isotonic","calibration","with","too","few","calibration","samples","(","<","1000",")","since","it","tends","to","overfit","."]
code: def calibrate_model(
      self,
      estimator,
      method: str = "sigmoid",
      calibrate_fold: Optional[Union[int, Any]] = 5,
      fold: Optional[Union[int, Any]] = None,
      round: int = 4,
      fit_kwargs: Optional[dict] = None,
      groups: Optional[Union[str, Any]] = None,
      verbose: bool = True,
      return_train_score: bool = False,
  ) -> Any:
      """
      This function calibrates the probability of a given estimator using isotonic
      or logistic regression. 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. The ouput of the original estimator
      and the calibrated estimator (created using this function) might not differ much.
      In order to see the calibration differences, use 'calibration' plot in ``plot_model``
      to see the difference before and after.
  
  
      Example
      -------
      >>> from pycaret.datasets import get_data
      >>> juice = get_data('juice')
      >>> from pycaret.classification import *
      >>> exp_name = setup(data = juice,  target = 'Purchase')
      >>> dt = create_model('dt')
      >>> calibrated_dt = calibrate_model(dt)
  
  
      estimator: scikit-learn compatible object
          Trained model object
  
  
      method: str, default = 'sigmoid'
          The method to use for calibration. Can be 'sigmoid' which corresponds to
          Platt's method or 'isotonic' which is a non-parametric approach.
  
  
      calibrate_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.
  
  
      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.
  
  
      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
  
  
      Warnings
      --------
      - Avoid isotonic calibration with too few calibration samples (< 1000) since it
      tends to overfit.
  
      """
  
      function_params_str = ", ".join(
          [f"{k}={v}" for k, v in locals().items()]
      )
  
      self.logger.info("Initializing calibrate_model()")
      self.logger.info(f"calibrate_model({function_params_str})")
  
      self.logger.info("Checking exceptions")
  
      # run_time
      runtime_start = time.time()
  
      if not fit_kwargs:
          fit_kwargs = {}
  
      # checking fold parameter
      if fold is not None and not (
          type(fold) is int or is_sklearn_cv_generator(fold)
      ):
          raise TypeError(
              "fold parameter must be either None, an integer or a scikit-learn compatible CV generator object."
          )
  
      # checking round parameter
      if type(round) is not int:
          raise TypeError("Round parameter only accepts integer value.")
  
      # checking verbose parameter
      if type(verbose) is not bool:
          raise TypeError(
              "Verbose parameter can only take argument as True or False."
          )
  
      # checking return_train_score parameter
      if type(return_train_score) is not bool:
          raise TypeError(
              "return_train_score can only take argument as True or False"
          )
  
      """
  
          ERROR HANDLING ENDS HERE
  
          """
  
      fold = self._get_cv_splitter(fold)
  
      groups = self._get_groups(groups)
  
      self.logger.info("Preloading libraries")
  
      # pre-load libraries
  
      self.logger.info("Preparing display monitor")
  
      progress_args = {"max": 2 + 4}
      timestampStr = datetime.datetime.now().strftime("%H:%M:%S")
      monitor_rows = [
          [
              "Initiated",
              ". . . . . . . . . . . . . . . . . .",
              timestampStr,
          ],
          [
              "Status",
              ". . . . . . . . . . . . . . . . . .",
              "Loading Dependencies",
          ],
          [
              "Estimator",
              ". . . . . . . . . . . . . . . . . .",
              "Compiling Library",
          ],
      ]
      display = CommonDisplay(
          verbose=verbose,
          html_param=self.html_param,
          progress_args=progress_args,
          monitor_rows=monitor_rows,
      )
  
      np.random.seed(self.seed)
  
      probability_threshold = None
      if isinstance(estimator, CustomProbabilityThresholdClassifier):
          probability_threshold = estimator.probability_threshold
          estimator = get_estimator_from_meta_estimator(estimator)
  
      self.logger.info("Getting model name")
  
      full_name = self._get_model_name(estimator)
  
      self.logger.info(f"Base model : {full_name}")
  
      display.update_monitor(2, full_name)
  
      """
          MONITOR UPDATE STARTS
          """
  
      display.update_monitor(1, "Selecting Estimator")
  
      """
          MONITOR UPDATE ENDS
          """
  
      # calibrating estimator
  
      self.logger.info("Importing untrained CalibratedClassifierCV")
  
      calibrated_model_definition = self._all_models_internal[
          "CalibratedCV"
      ]
      model = calibrated_model_definition.class_def(
          base_estimator=estimator,
          method=method,
          cv=calibrate_fold,
          **calibrated_model_definition.args,
      )
  
      display.move_progress()
  
      self.logger.info(
          "SubProcess create_model() called =================================="
      )
      model, model_fit_time = self._create_model(
          estimator=model,
          system=False,
          display=display,
          fold=fold,
          round=round,
          fit_kwargs=fit_kwargs,
          groups=groups,
          probability_threshold=probability_threshold,
          return_train_score=return_train_score,
      )
      model_results = self.pull()
      self.logger.info(
          "SubProcess create_model() end =================================="
      )
  
      model_results = model_results.round(round)
  
      display.move_progress()
  
      # end runtime
      runtime_end = time.time()
      runtime = np.array(runtime_end - runtime_start).round(2)
  
      # mlflow logging
      if self.logging_param:
          avgs_dict_log = {
              k: v
              for k, v in model_results.loc[
                  self._get_return_train_score_indices_for_logging(
                      return_train_score=return_train_score
                  )
              ].items()
          }
          self.logging_param.log_model_comparison(
              model_results,
              f"calibrate_models_{self._get_model_name(model)}",
          )
  
          self._log_model(
              model=model,
              model_results=model_results,
              score_dict=avgs_dict_log,
              source="calibrate_models",
              runtime=runtime,
              model_fit_time=model_fit_time,
              pipeline=self.pipeline,
              log_plots=self.log_plots_param,
              display=display,
          )
  
      model_results = self._highlight_and_round_model_results(
          model_results, return_train_score, round
      )
      display.display(model_results)
  
      self.logger.info(
          f"_master_model_container: {len(self._master_model_container)}"
      )
      self.logger.info(
          f"_display_container: {len(self._display_container)}"
      )
  
      self.logger.info(str(model))
      self.logger.info(
          "calibrate_model() successfully completed......................................"
      )
  
      gc.collect()
      return model
  
imports: import datetime
  import gc
  import logging
  import re
  import time
  from typing import Any, Callable, Dict, List, Optional, Tuple, Union
  from unittest.mock import patch
  import numpy
  import pandas
  import plotly.express
  import sklearn
  from joblib.memory import Memory
  from scipy.optimize import shgo
  from pycaret.containers.metrics.classification import get_all_metric_containers
  from pycaret.containers.models.classification import ALL_ALLOWED_ENGINES, get_all_model_containers, get_container_default_engines
  from pycaret.internal.display import CommonDisplay
  from pycaret.internal.logging import get_logger
  from pycaret.internal.meta_estimators import CustomProbabilityThresholdClassifier, get_estimator_from_meta_estimator
  from pycaret.internal.parallel.parallel_backend import ParallelBackend
  from pycaret.internal.pipeline import Pipeline
  from pycaret.internal.preprocess.preprocessor import Preprocessor
  from pycaret.internal.pycaret_experiment.non_ts_supervised_experiment import _NonTSSupervisedExperiment
  from pycaret.internal.validation import is_sklearn_cv_generator
  from pycaret.loggers.base_logger import BaseLogger
  from pycaret.utils.constants import DATAFRAME_LIKE, SEQUENCE_LIKE, TARGET_LIKE
  from pycaret.utils.generic import MLUsecase, get_classification_task, get_label_encoder, highlight_setup
label_desc: Use image node_id 16 for calling the ClassificationExperiment obj's underlying member method code with example usage: obj.calibrate_model(estimator, method, calibrate_fold, fold, round, fit_kwargs, groups, verbose, return_train_score) and returns: model