code: def _fit(
      self, predictions, labels, time_limit=None, sample_weight=None
  ):
      ensemble_size = self.ensemble_size
      self.num_input_models_ = len(predictions)
      ensemble = []
      trajectory = []
      order = []
      used_models = set()
  
      # if self.sorted_initialization:
      #     n_best = 20
      #     indices = self._sorted_initialization(predictions, labels, n_best)
      #     for idx in indices:
      #         ensemble.append(predictions[idx])
      #         order.append(idx)
      #         ensemble_ = np.array(ensemble).mean(axis=0)
      #         ensemble_performance = calculate_score(
      #             labels, ensemble_, self.task_type, self.metric,
      #             ensemble_.shape[1])
      #         trajectory.append(ensemble_performance)
      #     ensemble_size -= n_best
  
      time_start = time.time()
      round_scores = False
      epsilon = 1e-4
      round_decimals = 6
      for i in range(ensemble_size):
          scores = np.zeros((len(predictions)))
          s = len(ensemble)
          if s == 0:
              weighted_ensemble_prediction = np.zeros(
                  predictions[0].shape
              )
          else:
              # Memory-efficient averaging!
              ensemble_prediction = np.zeros(ensemble[0].shape)
              for pred in ensemble:
                  ensemble_prediction += pred
              ensemble_prediction /= s
  
              weighted_ensemble_prediction = (
                  s / float(s + 1)
              ) * ensemble_prediction
          fant_ensemble_prediction = np.zeros(
              weighted_ensemble_prediction.shape
          )
          for j, pred in enumerate(predictions):
              fant_ensemble_prediction[:] = (
                  weighted_ensemble_prediction
                  + (1.0 / float(s + 1)) * pred
              )
              if self.problem_type in ["multiclass", "softclass"]:
                  # Renormalize
                  fant_ensemble_prediction[:] = (
                      fant_ensemble_prediction
                      / fant_ensemble_prediction.sum(axis=1)[
                          :, np.newaxis
                      ]
                  )
              scores[j] = self._calculate_regret(
                  y_true=labels,
                  y_pred_proba=fant_ensemble_prediction,
                  metric=self.metric,
                  sample_weight=sample_weight,
              )
              if round_scores:
                  scores[j] = scores[j].round(round_decimals)
  
          all_best = np.argwhere(scores == np.nanmin(scores)).flatten()
  
          if (len(all_best) > 1) and used_models:
              # If tie, prioritize models already in ensemble to avoid unnecessarily large ensemble
              new_all_best = []
              for m in all_best:
                  if m in used_models:
                      new_all_best.append(m)
              if new_all_best:
                  all_best = new_all_best
  
          if len(all_best) > 1:
              if self.tie_breaker == "second_metric":
                  if self.problem_type in ["binary", "multiclass"]:
                      # Tiebreak with log_loss
                      scores_tiebreak = np.zeros((len(all_best)))
                      secondary_metric = log_loss
                      fant_ensemble_prediction = np.zeros(
                          weighted_ensemble_prediction.shape
                      )
                      index_map = {}
                      for k, j in enumerate(all_best):
                          index_map[k] = j
                          pred = predictions[j]
                          fant_ensemble_prediction[:] = (
                              weighted_ensemble_prediction
                              + (1.0 / float(s + 1)) * pred
                          )
                          scores_tiebreak[k] = self._calculate_regret(
                              y_true=labels,
                              y_pred_proba=fant_ensemble_prediction,
                              metric=secondary_metric,
                          )
                      all_best_tiebreak = np.argwhere(
                          scores_tiebreak == np.nanmin(scores_tiebreak)
                      ).flatten()
                      all_best = [
                          index_map[index]
                          for index in all_best_tiebreak
                      ]
  
          best = self.random_state.choice(all_best)
          best_score = scores[best]
  
          # If first iteration
          if i == 0:
              # If abs value of min score is large enough, round to 6 decimal places to avoid floating point error deciding the best index.
              # This avoids 2 models with the same pred proba both being used in the ensemble due to floating point error
              if np.abs(best_score) > epsilon:
                  round_scores = True
                  best_score = best_score.round(round_decimals)
  
          ensemble.append(predictions[best])
          trajectory.append(best_score)
          order.append(best)
          used_models.add(best)
  
          # Handle special case
          if len(predictions) == 1:
              break
  
          if time_limit is not None:
              time_elapsed = time.time() - time_start
              time_left = time_limit - time_elapsed
              if time_left <= 0:
                  logger.warning(
                      "Warning: Ensemble Selection ran out of time, early stopping at iteration %s. This may mean that the time_limit specified is very small for this problem."
                      % (i + 1)
                  )
                  break
  
      min_score = np.min(trajectory)
      first_index_of_best = trajectory.index(min_score)
  
      if self.use_best:
          self.indices_ = order[: first_index_of_best + 1]
          self.trajectory_ = trajectory[: first_index_of_best + 1]
          self.train_score_ = trajectory[first_index_of_best]
          self.ensemble_size = first_index_of_best + 1
          logger.log(15, "Ensemble size: %s" % self.ensemble_size)
      else:
          self.indices_ = order
          self.trajectory_ = trajectory
          self.train_score_ = trajectory[-1]
  
      logger.debug("Ensemble indices: " + str(self.indices_))
  
imports: import logging
  import time
  from collections import Counter
  import numpy
  from ...constants import PROBLEM_TYPES
  from ...metrics import log_loss
  from ...utils import compute_weighted_metric, get_pred_from_proba
label_desc: Use image node_id 3 for calling the EnsembleSelection obj's underlying member method code with example usage: obj._fit(predictions, labels, time_limit, sample_weight) without return types