code: def get_precision_df(
      pred_probas,
      y_true,
      bin_size=None,
      quantiles=None,
      round=3,
      pos_label=1,
  ):
      """
      returns a pd.DataFrame with the predicted probabilities and
      the observed frequency per bin_size or quantile.
  
      If pred_probas has one dimension (i.e. only probabilities of positive class)
      only returns a single precision. If pred_probas containts probabilities for
      every class (typically a multiclass classifier), also returns precision
      for every class in every bin.
  
      Args:
          pred_probas (np.ndarray): result of model.predict_proba(X). Can either
              be probabilities of a single class or multiple classes.
          y_true (np.ndarray): array of true class labels.
          bin_size (float): bin sizes to bin by. E.g. 0.1 to bin all prediction
              between 0 and 0.1, between 0.1 and 0.2, etc. If setting bin_size
              you cannot set quantiles.
          quantiles (int): number of quantiles to divide pred_probas in.
              e.g. if quantiles=4, set bins such that the lowest 25% of pred_probas
              go into first bin, next 25% go in second bin, etc. Each bin will
              have (approximatly the same amount of observations). If setting
              quantiles you cannot set bin_size.
          round (int): the number of figures to round the output by. Defaults to 3.
          pos_label (int): the label of the positive class. Defaults to 1.
  
      Returns:
          pd.DataFrame with columns ['p_min', 'p_max', 'p_avg', 'bin_width',
          'precision', 'count']
      """
      if bin_size is None and quantiles is None:
          bin_size = 0.1
  
      assert (bin_size is not None and quantiles is None) or (
          bin_size is None and quantiles is not None
      ), "either only pass bin_size or only pass quantiles!"
  
      if len(pred_probas.shape) == 2:
          # in case the full binary classifier pred_proba is passed,
          # we only select the probability of the positive class
          predictions_df = pd.DataFrame(
              {
                  "pred_proba": pred_probas[:, pos_label],
                  "target": y_true,
              }
          )
          n_classes = pred_probas.shape[1]
      else:
          predictions_df = pd.DataFrame(
              {"pred_proba": pred_probas, "target": y_true}
          )
          n_classes = 1
  
      predictions_df = predictions_df.sort_values("pred_proba")
  
      # define a placeholder df:
      columns = [
          "p_min",
          "p_max",
          "p_avg",
          "bin_width",
          "precision",
          "count",
      ]
      if n_classes > 1:
          for i in range(n_classes):
              columns.append("precision_" + str(i))
  
      precision_df = pd.DataFrame(columns=columns)
  
      if bin_size:
          thresholds = np.arange(0.0, 1.0, bin_size).tolist()
          # loop through prediction intervals, and compute
          for bin_min, bin_max in zip(
              thresholds, thresholds[1:] + [1.0]
          ):
              if bin_min != bin_max:
                  new_row_dict = {
                      "p_min": [bin_min],
                      "p_max": [bin_max],
                      "p_avg": [bin_min + (bin_max - bin_min) / 2.0],
                      "bin_width": [bin_max - bin_min],
                  }
  
                  if bin_min == 0.0:
                      new_row_dict["p_avg"] = predictions_df[
                          (predictions_df.pred_proba >= bin_min)
                          & (predictions_df.pred_proba <= bin_max)
                      ]["pred_proba"].mean()
                      new_row_dict["precision"] = (
                          predictions_df[
                              (predictions_df.pred_proba >= bin_min)
                              & (predictions_df.pred_proba <= bin_max)
                          ].target
                          == pos_label
                      ).mean()
                      new_row_dict["count"] = predictions_df[
                          (predictions_df.pred_proba >= bin_min)
                          & (predictions_df.pred_proba <= bin_max)
                      ].target.count()
                      if n_classes > 1:
                          for i in range(n_classes):
                              new_row_dict["precision_" + str(i)] = (
                                  predictions_df[
                                      (
                                          predictions_df.pred_proba
                                          >= bin_min
                                      )
                                      & (
                                          predictions_df.pred_proba
                                          <= bin_max
                                      )
                                  ].target
                                  == i
                              ).mean()
                  else:
                      new_row_dict["p_avg"] = predictions_df[
                          (predictions_df.pred_proba > bin_min)
                          & (predictions_df.pred_proba <= bin_max)
                      ]["pred_proba"].mean()
                      new_row_dict["precision"] = (
                          predictions_df[
                              (predictions_df.pred_proba > bin_min)
                              & (predictions_df.pred_proba <= bin_max)
                          ].target
                          == pos_label
                      ).mean()
                      new_row_dict["count"] = (
                          predictions_df[
                              (predictions_df.pred_proba > bin_min)
                              & (predictions_df.pred_proba <= bin_max)
                          ].target
                          == pos_label
                      ).count()
                      if n_classes > 1:
                          for i in range(n_classes):
                              new_row_dict["precision_" + str(i)] = (
                                  predictions_df[
                                      (
                                          predictions_df.pred_proba
                                          > bin_min
                                      )
                                      & (
                                          predictions_df.pred_proba
                                          <= bin_max
                                      )
                                  ].target
                                  == i
                              ).mean()
                  new_row_df = pd.DataFrame(
                      new_row_dict, columns=precision_df.columns
                  )
                  precision_df = pd.concat([precision_df, new_row_df])
  
      elif quantiles:
          preds_quantiles = np.array_split(
              predictions_df.pred_proba.values, quantiles
          )
          target_quantiles = np.array_split(
              predictions_df.target.values, quantiles
          )
  
          last_p_max = 0.0
          for preds, targets in zip(preds_quantiles, target_quantiles):
              new_row_dict = {
                  "p_min": [last_p_max],
                  "p_max": [preds.max()],
                  "p_avg": [preds.mean()],
                  "bin_width": [preds.max() - last_p_max],
                  "precision": [np.mean(targets == pos_label)],
                  "count": [len(preds)],
              }
              if n_classes > 1:
                  for i in range(n_classes):
                      new_row_dict["precision_" + str(i)] = np.mean(
                          targets == i
                      )
  
              new_row_df = pd.DataFrame(
                  new_row_dict, columns=precision_df.columns
              )
              precision_df = pd.concat([precision_df, new_row_df])
              last_p_max = preds.max()
  
      precision_df[["p_avg", "precision"]] = (
          precision_df[["p_avg", "precision"]]
          .astype(float)
          .apply(partial(np.round, decimals=round))
      )
      if n_classes > 1:
          precision_cols = [
              "precision_" + str(i) for i in range(n_classes)
          ]
          precision_df[precision_cols] = (
              precision_df[precision_cols]
              .astype(float)
              .apply(partial(np.round, decimals=round))
          )
      return precision_df
  
imports: from functools import partial
  import re
  from collections import Counter
  from typing import List, Union
  import warnings
  import numpy
  import pandas
  from pandas.api.types import is_numeric_dtype, is_categorical_dtype
  from dtreeviz.trees import ShadowDecTree
  from sklearn.metrics import make_scorer
  from sklearn.base import clone
  from sklearn.pipeline import Pipeline
  from sklearn.model_selection import KFold, StratifiedKFold
  from joblib import Parallel, delayed
label_desc: Use image node_id 22 for calling a global function with example usage: get_precision_df(pred_probas, y_true, bin_size, quantiles, round, pos_label) and returns: precision_df