code: def tune_model(
      estimator,
      fold: Optional[Union[int, Any]] = None,
      round: int = 4,
      n_iter: int = 10,
      custom_grid: Optional[Union[Dict[str, list], Any]] = None,
      optimize: str = "MASE",
      custom_scorer=None,
      search_algorithm: Optional[str] = None,
      choose_better: bool = True,
      fit_kwargs: Optional[dict] = None,
      return_tuner: bool = False,
      verbose: bool = True,
      tuner_verbose: Union[int, bool] = True,
      **kwargs,
  ):
      """
      This function tunes the hyperparameters of a given estimator. The output of
      this function is a score grid with CV scores by fold of the best selected
      model based on ``optimize`` parameter. 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
      >>> airline = get_data('airline')
      >>> from pycaret.time_series import *
      >>> exp_name = setup(data = airline,  fh = 12)
      >>> dt = create_model('dt_cds_dt')
      >>> tuned_dt = tune_model(dt)
  
  
      estimator: sktime compatible object
          Trained model object
  
  
      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.
  
  
      n_iter: int, default = 10
          Number of iterations in the grid search. Increasing 'n_iter' may improve
          model performance but also increases the training time.
  
  
      custom_grid: dictionary, default = None
          To define custom search space for hyperparameters, pass a dictionary with
          parameter name and values to be iterated. Custom grids must be in a format
          supported by the defined ``search_library``.
  
  
      optimize: str, default = 'MASE'
          Metric name to be evaluated for hyperparameter tuning. It also accepts custom
          metrics that are added through the ``add_metric`` function.
  
  
      custom_scorer: object, default = None
          custom scoring strategy can be passed to tune hyperparameters of the model.
          It must be created using ``sklearn.make_scorer``. It is equivalent of adding
          custom metric using the ``add_metric`` function and passing the name of the
          custom metric in the ``optimize`` parameter.
          Will be deprecated in future.
  
  
      search_algorithm: str, default = 'random'
          use 'random' for random grid search and 'grid' for complete grid search.
  
  
      choose_better: bool, default = True
          When set to True, the returned object is always better performing. The
          metric used for comparison is defined by the ``optimize`` parameter.
  
  
      fit_kwargs: dict, default = {} (empty dict)
          Dictionary of arguments passed to the fit method of the tuner.
  
  
      return_tuner: bool, default = False
          When set to True, will return a tuple of (model, tuner_object).
  
  
      verbose: bool, default = True
          Score grid is not printed when verbose is set to False.
  
  
      tuner_verbose: bool or in, default = True
          If True or above 0, will print messages from the tuner. Higher values
          print more messages. Ignored when ``verbose`` param is False.
  
  
      **kwargs:
          Additional keyword arguments to pass to the optimizer.
  
  
      Returns:
          Trained Model and Optional Tuner Object when ``return_tuner`` is True.
  
      """
  
      return _CURRENT_EXPERIMENT.tune_model(
          estimator=estimator,
          fold=fold,
          round=round,
          n_iter=n_iter,
          custom_grid=custom_grid,
          optimize=optimize,
          custom_scorer=custom_scorer,
          search_algorithm=search_algorithm,
          choose_better=choose_better,
          fit_kwargs=fit_kwargs,
          return_tuner=return_tuner,
          verbose=verbose,
          tuner_verbose=tuner_verbose,
          **kwargs,
      )
  
imports: import logging
  import os
  from typing import TYPE_CHECKING, Any, BinaryIO, Callable, Dict, List, Optional, Tuple, Union
  import numpy
  import pandas
  from pycaret.internal.parallel.parallel_backend import ParallelBackend
  from pycaret.time_series.forecasting.oop import TSForecastingExperiment
  from pycaret.utils.generic import check_if_global_is_not_none
label_desc: Use image node_id 6 for calling a global function with example usage: tune_model(estimator, fold, round, n_iter, custom_grid, optimize, custom_scorer, search_algorithm, choose_better, fit_kwargs, return_tuner, verbose, tuner_verbose) and returns: _CURRENT_EXPERIMENT