code: def optimize_threshold(
      estimator,
      optimize: str = "Accuracy",
      return_data: bool = False,
      plot_kwargs: Optional[dict] = None,
      **shgo_kwargs,
  ):
      """
      This function optimizes probability threshold for a trained classifier. It
      uses the SHGO optimizer from ``scipy`` to optimize for the given metric.
      This function will display a plot of the performance metrics at each probability
      threshold checked by the optimizer and returns the best model based on the metric
      defined under ``optimize`` parameter.
  
  
      Example
      -------
      >>> from pycaret.datasets import get_data
      >>> juice = get_data('juice')
      >>> experiment_name = setup(data = juice,  target = 'Purchase')
      >>> lr = create_model('lr')
      >>> best_lr_threshold = optimize_threshold(lr)
  
  
      Parameters
      ----------
      estimator : object
          A trained model object should be passed as an estimator.
  
  
      optimize : str, default = 'Accuracy'
          Metric to be used for selecting best model.
  
  
      return_data :  bool, default = False
          When set to True, data used for visualization is also returned.
  
  
      plot_kwargs :  dict, default = {} (empty dict)
          Dictionary of arguments passed to the visualizer class.
  
  
      **shgo_kwargs:
          Kwargs to pass to ``scipy.optimize.shgo``.
  
  
      Returns
      -------
      Trained Model
  
  
      Warnings
      --------
      - This function does not support multiclass classification problems.
      """
  
      return _CURRENT_EXPERIMENT.optimize_threshold(
          estimator=estimator,
          optimize=optimize,
          return_data=return_data,
          plot_kwargs=plot_kwargs,
          **shgo_kwargs,
      )
  
imports: import logging
  import os
  from typing import Any, BinaryIO, Callable, Dict, List, Optional, Union
  import pandas
  from joblib.memory import Memory
  from pycaret.classification.oop import ClassificationExperiment
  from pycaret.internal.parallel.parallel_backend import ParallelBackend
  from pycaret.loggers.base_logger import BaseLogger
  from pycaret.utils.constants import DATAFRAME_LIKE, SEQUENCE_LIKE, TARGET_LIKE
  from pycaret.utils.generic import check_if_global_is_not_none
label_desc: Use image node_id 14 for calling a global function with example usage: optimize_threshold(estimator, optimize, return_data, plot_kwargs) and returns: _CURRENT_EXPERIMENT