docstr: Fits self.model and partitions R^n into cells.
  
  More accurately, it creates two dictionaries: self.preds_to_ids whose
  keys are the predictions of the training dataset and whose values are
  the ids of their associated bins, and self.ids_to_bins whose keys are
  the ids of the bins and whose values are associated lists of true
  values.
docstr_tok: ["Fits","self.model","and","partitions","R^n","into","cells",".","More","accurately",",","it","creates","two","dictionaries",":","self.preds_to_ids","whose","keys","are","the","predictions","of","the","training","dataset","and","whose","values","are","the","ids","of","their","associated","bins",",","and","self.ids_to_bins","whose","keys","are","the","ids","of","the","bins","and","whose","values","are","associated","lists","of","true","values","."]
code: def fit(
      self,
      x_train: Union[pd.DataFrame, List],
      y_train: Union[pd.Series, List],
      max_sample_size: Optional[
          int
      ] = None,  # If not None, choose without replacement
      # replacement min(max_sample_size, len(x_train)) many datapoints
      # to train on.
      seed: int = 1,
      x_train_is_dataframe: bool = False,  # This should be False for
      # XGBoost, but True if one uses lightgbm.
      model_is_already_trained: bool = False,  # True if there is no need to
      # train self.model
      **kwargs,
  ):
      """
      Fits self.model and partitions R^n into cells.
  
      More accurately, it creates two dictionaries: self.preds_to_ids whose
      keys are the predictions of the training dataset and whose values are
      the ids of their associated bins, and self.ids_to_bins whose keys are
      the ids of the bins and whose values are associated lists of true
      values.
      """
      self.x_train_is_dataframe = x_train_is_dataframe
      self.quantile_dicts = defaultdict(dict)
      if not x_train_is_dataframe:
          x_train, y_train = np.array(x_train), np.array(
              y_train
          )  # xgboost
      # doens't like lists
      if max_sample_size and x_train_is_dataframe:
          assert max_sample_size > 0
          assert isinstance(x_train, pd.DataFrame)
          sample_size = min(max_sample_size, len(x_train))
          x_train = x_train.sample(
              n=min(sample_size, len(x_train)),
              replace=False,
              random_state=seed,
          )
          y_train = y_train[x_train.index]
      elif max_sample_size:
          assert max_sample_size > 0
          sample_size = min(max_sample_size, len(x_train))
          np.random.seed(seed)
          idx = np.random.choice(
              np.arange(len(x_train)), sample_size, replace=False
          )
          x_train = x_train[idx]
          y_train = y_train[idx]
      if not model_is_already_trained:
          self.model.fit(x_train, y_train, **kwargs)
      y_train_pred = self.model.predict(x_train)
      df = pd.DataFrame(
          {
              "y_true": y_train,
              "y_pred": y_train_pred,
          }
      ).reset_index(drop=True)
      self.sorted_train_preds = sorted(df["y_pred"].unique())
      cell_values_dict = self.preprocess_df(
          df, min_bin_size=self.min_bin_size
      )
      del df
      gc.collect()
      cell_values_dict_df = pd.DataFrame(
          cell_values_dict.items(), columns=["keys", "values"]
      )
      cell_values_dict_df["id"] = cell_values_dict_df["values"].apply(
          id
      )
      self.id_to_bins = (
          cell_values_dict_df.groupby("id")["values"].first().to_dict()
      )
      self.preds_to_id = (
          cell_values_dict_df.groupby("keys")["id"].first().to_dict()
      )
      del cell_values_dict_df
      del cell_values_dict
      gc.collect()
  
      df = pd.DataFrame({"preds": self.sorted_train_preds})
      df["bin_ids"] = df["preds"].apply(lambda x: self.preds_to_id[x])
      bin_ids = df["bin_ids"].drop_duplicates().values
      if len(bin_ids) > 1:
          final_id, penultimate_id = bin_ids[-1], bin_ids[-2]
          if len(self.id_to_bins[final_id]) < self.min_bin_size:
              self.id_to_bins[final_id] += self.id_to_bins[
                  penultimate_id
              ]
  
imports: from typing import Any, Dict, List, Optional, Union
  import copy
  import numpy
  import pandas
  import xgboost
  import gc
  from collections import defaultdict
  from gluonts.core.component import equals, validated
label_desc: Use image node_id 4 for calling the QRX obj's underlying member method code with example usage: obj.fit(x_train, y_train, max_sample_size, seed, x_train_is_dataframe, model_is_already_trained) without return types