code: def gini_impurity(
      thresholds: np.ndarray,
      data: np.ndarray,
      labels: np.ndarray,
      classes: Optional[List[int]] = None,
      reduction: Optional[str] = "sum",
      padded: bool = True,
  ) -> float:
      """
      Calculate the Gini impurity of a dataset split on a set of thresholds.
  
      Args:
          thresholds (np.ndarray): The thresholds to split the data on.
          data (np.ndarray): The data to split.
          labels (np.ndarray): The labels for the data.
          classes (Optional[List[int]]): The classes to consider. If None, all classes are used.
          reduction (Optional[str]): The reduction to apply to the impurity. One of "none", "sum", or "mean".
          padded (bool): Whether to pad the thresholds with `[-0.5, data.max() + 0.5]`.
  
      Returns:
          float: The Gini impurity.
      """
      G = []
      if not padded:
          thresholds = np.insert(
              thresholds, [0, len(thresholds)], [-0.5, data.max() + 0.5]
          )
      if classes is None:
          classes = np.arange(len(thresholds) - 1)
      else:
          classes = np.asarray(classes)
      if data.ndim == 1:
          data = np.expand_dims(data, 0)
      masks = np.logical_and(
          data > thresholds[classes, None],
          data <= thresholds[classes + 1, None],
      )
      for i, c in enumerate(classes):
          G.append(
              compute_binary_gini(np.where(labels[masks[i]] == c, 1, 0))
          )
  
      if reduction is None or reduction == "none":
          return G
      elif reduction == "sum":
          return sum(G)
      elif reduction == "mean":
          return sum(G) / len(G)
      else:
          raise NotImplementedError
  
imports: from collections import deque
  import operator
  import itertools
  from typing import Optional, List, Tuple
  import numpy
  import warnings
label_desc: Use image node_id 4 for calling a global function with example usage: gini_impurity(thresholds, data, labels, classes, reduction, padded) and returns: G, sum, unknown