code: def predict_best(model):
      r"""Select the best model based on score.
  
      Parameters
      ----------
      model : alphapy.Model
          The model object with all of the estimators.
  
      Returns
      -------
      model : alphapy.Model
          The model object with the best estimator.
  
      Notes
      -----
      Best model selection is based on a scoring function. If the
      objective is to minimize (e.g., negative log loss), then we
      select the model with the algorithm that has the lowest score.
      If the objective is to maximize, then we select the algorithm
      with the highest score (e.g., AUC).
  
      For multiple algorithms, AlphaPy always creates a blended model.
      Therefore, the best algorithm that is selected could actually
      be the blended model itself.
  
      """
  
      logger.info("=" * 80)
      logger.info("Selecting Best Model")
  
      # Define model tags
  
      best_tag = "BEST"
      blend_tag = "BLEND"
  
      # Extract model parameters.
  
      model_type = model.specs["model_type"]
      rfe = model.specs["rfe"]
      scorer = model.specs["scorer"]
      test_labels = model.test_labels
  
      # Determine the correct partition to select the best model
  
      partition = Partition.test if test_labels else Partition.train
      logger.info("Scoring for: %s", partition)
  
      # Initialize best parameters.
  
      maximize = (
          True if scorers[scorer][1] == Objective.maximize else False
      )
      if maximize:
          best_score = -sys.float_info.max
      else:
          best_score = sys.float_info.max
  
      # Initialize the model selection process.
  
      start_time = datetime.now()
      logger.info("Best Model Selection Start: %s", start_time)
  
      # Add blended model to the list of algorithms.
  
      if len(model.algolist) > 1:
          algolist = copy(model.algolist)
          algolist.append(blend_tag)
      else:
          algolist = model.algolist
  
      # Iterate through the models, getting the best score for each one.
  
      for algorithm in algolist:
          logger.info("Scoring %s Model", algorithm)
          top_score = model.metrics[(algorithm, partition, scorer)]
          # objective is to either maximize or minimize score
          if maximize:
              if top_score > best_score:
                  best_score = top_score
                  best_algo = algorithm
          else:
              if top_score < best_score:
                  best_score = top_score
                  best_algo = algorithm
  
      # Record predictions of best estimator
  
      logger.info(
          "Best Model is %s with a %s score of %.4f",
          best_algo,
          scorer,
          best_score,
      )
      model.best_algo = best_algo
      model.estimators[best_tag] = model.estimators[best_algo]
      model.preds[(best_tag, Partition.train)] = model.preds[
          (best_algo, Partition.train)
      ]
      model.preds[(best_tag, Partition.test)] = model.preds[
          (best_algo, Partition.test)
      ]
      if model_type == ModelType.classification:
          model.probas[(best_tag, Partition.train)] = model.probas[
              (best_algo, Partition.train)
          ]
          model.probas[(best_tag, Partition.test)] = model.probas[
              (best_algo, Partition.test)
          ]
  
      # Record support vector for any recursive feature elimination
  
      if rfe and "XGB" not in best_algo:
          try:
              model.feature_map["rfe_support"] = model.support[
                  best_algo
              ]
          except:
              # no RFE support for best algorithm
              pass
  
      # Return the model with best estimator and predictions.
  
      end_time = datetime.now()
      time_taken = end_time - start_time
      logger.info("Best Model Selection Complete: %s", time_taken)
  
      return model
  
imports: from alphapy.estimators import scorers
  from alphapy.estimators import xgb_score_map
  from alphapy.features import feature_scorers
  from alphapy.frame import read_frame
  from alphapy.frame import write_frame
  from alphapy.globals import Encoders
  from alphapy.globals import ModelType
  from alphapy.globals import Objective
  from alphapy.globals import Partition, datasets
  from alphapy.globals import PSEP, SSEP, USEP
  from alphapy.globals import SamplingMethod
  from alphapy.globals import Scalers
  from alphapy.utilities import get_datestamp
  from alphapy.utilities import most_recent_file
  from copy import copy
  from datetime import datetime
  import itertools
  import joblib
  from keras.models import load_model
  import logging
  import numpy
  import pandas
  from sklearn.calibration import CalibratedClassifierCV
  from sklearn.linear_model import LogisticRegression
  from sklearn.linear_model import RidgeCV
  from sklearn.metrics import accuracy_score
  from sklearn.metrics import auc
  from sklearn.metrics import average_precision_score
  from sklearn.metrics import balanced_accuracy_score
  from sklearn.metrics import brier_score_loss
  from sklearn.metrics import classification_report
  from sklearn.metrics import cohen_kappa_score
  from sklearn.metrics import confusion_matrix
  from sklearn.metrics import explained_variance_score
  from sklearn.metrics import f1_score
  from sklearn.metrics import log_loss
  from sklearn.metrics import mean_absolute_error
  from sklearn.metrics import mean_squared_error
  from sklearn.metrics import mean_squared_log_error
  from sklearn.metrics import median_absolute_error
  from sklearn.metrics import precision_score
  from sklearn.metrics import r2_score
  from sklearn.metrics import recall_score
  from sklearn.metrics import roc_auc_score
  from sklearn.metrics import roc_curve
  from sklearn.metrics.cluster import adjusted_rand_score
  from sklearn.model_selection import cross_val_score
  from sklearn.model_selection import train_test_split
  import sys
  import yaml
label_desc: Use image node_id 8 for calling a global function with example usage: predict_best(model) and returns: model