code: def _extract_labels(
      data: Dataset, label_name: str, map_to_int: bool
  ) -> Tuple[np.ndarray, Mapping]:
      """
      Picks out labels from the dataset and formats them to be [0, 1, ..., K-1]
      where K is the number of classes. Also returns a mapping from the formatted
      labels to the original labels in the dataset.
  
      Note: This function is not meant to be used directly. It is used by
      ``cleanlab.data.Data`` to extract the formatted labels from the dataset
      and stores them as attributes.
  
      Parameters
      ----------
      data : datasets.Dataset
          A Hugging Face Dataset object.
  
      label_name : str
          Name of the column in the dataset that contains the labels.
  
      map_to_int : bool
          Whether to map the labels to integers, e.g. [0, 1, ..., K-1] where K is the number of classes.
          If False, the labels are not mapped to integers, e.g. for regression tasks.
      Returns
      -------
      formatted_labels : np.ndarray
          Labels in the format [0, 1, ..., K-1] where K is the number of classes.
  
      inverse_map : dict
          Mapping from the formatted labels to the original labels in the dataset.
      """
  
      labels = labels_to_array(data[label_name])  # type: ignore[assignment]
      if labels.ndim != 1:
          raise ValueError("labels must be 1D numpy array.")
  
      if not map_to_int:
          # Don't map labels to integers, e.g. for regression tasks
          return labels, {}
      label_name_feature = data.features[label_name]
      if isinstance(label_name_feature, ClassLabel):
          label_map = {
              label: label_name_feature.str2int(label)
              for label in label_name_feature.names
          }
          formatted_labels = labels
      else:
          label_map = {
              label: i for i, label in enumerate(np.unique(labels))
          }
          formatted_labels = np.vectorize(label_map.get)(labels)
      inverse_map = {i: label for label, i in label_map.items()}
  
      return formatted_labels, inverse_map
  
imports: import os
  from typing import Any, Callable, Dict, List, Mapping, Optional, Tuple, Union, cast, TYPE_CHECKING
  import numpy
  import pandas
  from datasets.arrow_dataset import Dataset
  from datasets import ClassLabel
  from cleanlab.internal.validation import labels_to_array
label_desc: Use image node_id 1 for calling a global function with example usage: _extract_labels(data, label_name, map_to_int) and returns: formatted_labels,  inverse_map, labels,  dict