code: def geometric_mean_score(
      y_true,
      y_pred,
      *,
      labels=None,
      pos_label=1,
      average="multiclass",
      sample_weight=None,
      correction=0.0,
  ):
      """Compute the geometric mean.
  
      The geometric mean (G-mean) is the root of the product of class-wise
      sensitivity. This measure tries to maximize the accuracy on each of the
      classes while keeping these accuracies balanced. For binary classification
      G-mean is the squared root of the product of the sensitivity
      and specificity. For multi-class problems it is a higher root of the
      product of sensitivity for each class.
  
      For compatibility with other imbalance performance measures, G-mean can be
      calculated for each class separately on a one-vs-rest basis when
      ``average != 'multiclass'``.
  
      The best value is 1 and the worst value is 0. Traditionally if at least one
      class is unrecognized by the classifier, G-mean resolves to zero. To
      alleviate this property, for highly multi-class the sensitivity of
      unrecognized classes can be "corrected" to be a user specified value
      (instead of zero). This option works only if ``average == 'multiclass'``.
  
      Read more in the :ref:`User Guide <imbalanced_metrics>`.
  
      Parameters
      ----------
      y_true : array-like of shape (n_samples,)
          Ground truth (correct) target values.
  
      y_pred : array-like of shape (n_samples,)
          Estimated targets as returned by a classifier.
  
      labels : array-like, default=None
          The set of labels to include when ``average != 'binary'``, and their
          order if ``average is None``. Labels present in the data can be
          excluded, for example to calculate a multiclass average ignoring a
          majority negative class, while labels not present in the data will
          result in 0 components in a macro average.
  
      pos_label : str, int or None, default=1
          The class to report if ``average='binary'`` and the data is binary.
          If ``pos_label is None`` and in binary classification, this function
          returns the average geometric mean if ``average`` is one of
          ``'weighted'``.
          If the data are multiclass, this will be ignored;
          setting ``labels=[pos_label]`` and ``average != 'binary'`` will report
          scores for that label only.
  
      average : str or None, default='multiclass'
          If ``None``, the scores for each class are returned. Otherwise, this
          determines the type of averaging performed on the data:
  
          ``'binary'``:
              Only report results for the class specified by ``pos_label``.
              This is applicable only if targets (``y_{true,pred}``) are binary.
          ``'micro'``:
              Calculate metrics globally by counting the total true positives,
              false negatives and false positives.
          ``'macro'``:
              Calculate metrics for each label, and find their unweighted
              mean.  This does not take label imbalance into account.
          ``'multiclass'``:
              No average is taken.
          ``'weighted'``:
              Calculate metrics for each label, and find their average, weighted
              by support (the number of true instances for each label). This
              alters 'macro' to account for label imbalance; it can result in an
              F-score that is not between precision and recall.
          ``'samples'``:
              Calculate metrics for each instance, and find their average (only
              meaningful for multilabel classification where this differs from
              :func:`accuracy_score`).
  
      sample_weight : array-like of shape (n_samples,), default=None
          Sample weights.
  
      correction : float, default=0.0
          Substitutes sensitivity of unrecognized classes from zero to a given
          value.
  
      Returns
      -------
      geometric_mean : float
          Returns the geometric mean.
  
      Notes
      -----
      See :ref:`sphx_glr_auto_examples_evaluation_plot_metrics.py`.
  
      References
      ----------
      .. [1] Kubat, M. and Matwin, S. "Addressing the curse of
         imbalanced training sets: one-sided selection" ICML (1997)
  
      .. [2] Barandela, R., Sánchez, J. S., Garcıa, V., & Rangel, E. "Strategies
         for learning in class imbalance problems", Pattern Recognition,
         36(3), (2003), pp 849-851.
  
      Examples
      --------
      >>> from imblearn.metrics import geometric_mean_score
      >>> y_true = [0, 1, 2, 0, 1, 2]
      >>> y_pred = [0, 2, 1, 0, 0, 1]
      >>> geometric_mean_score(y_true, y_pred)
      0.0
      >>> geometric_mean_score(y_true, y_pred, correction=0.001)
      0.010...
      >>> geometric_mean_score(y_true, y_pred, average='macro')
      0.471...
      >>> geometric_mean_score(y_true, y_pred, average='micro')
      0.471...
      >>> geometric_mean_score(y_true, y_pred, average='weighted')
      0.471...
      >>> geometric_mean_score(y_true, y_pred, average=None)
      array([0.866...,  0.       ,  0.       ])
      """
      if average is None or average != "multiclass":
          sen, spe, _ = sensitivity_specificity_support(
              y_true,
              y_pred,
              labels=labels,
              pos_label=pos_label,
              average=average,
              warn_for=("specificity", "specificity"),
              sample_weight=sample_weight,
          )
  
          return np.sqrt(sen * spe)
      else:
          present_labels = unique_labels(y_true, y_pred)
  
          if labels is None:
              labels = present_labels
              n_labels = None
          else:
              n_labels = len(labels)
              labels = np.hstack(
                  [
                      labels,
                      np.setdiff1d(
                          present_labels, labels, assume_unique=True
                      ),
                  ]
              )
  
          le = LabelEncoder()
          le.fit(labels)
          y_true = le.transform(y_true)
          y_pred = le.transform(y_pred)
          sorted_labels = le.classes_
  
          # labels are now from 0 to len(labels) - 1 -> use bincount
          tp = y_true == y_pred
          tp_bins = y_true[tp]
  
          if sample_weight is not None:
              tp_bins_weights = np.asarray(sample_weight)[tp]
          else:
              tp_bins_weights = None
  
          if len(tp_bins):
              tp_sum = np.bincount(
                  tp_bins,
                  weights=tp_bins_weights,
                  minlength=len(labels),
              )
          else:
              # Pathological case
              true_sum = tp_sum = np.zeros(len(labels))
          if len(y_true):
              true_sum = np.bincount(
                  y_true, weights=sample_weight, minlength=len(labels)
              )
  
          # Retain only selected labels
          indices = np.searchsorted(sorted_labels, labels[:n_labels])
          tp_sum = tp_sum[indices]
          true_sum = true_sum[indices]
  
          with np.errstate(divide="ignore", invalid="ignore"):
              recall = _prf_divide(
                  tp_sum, true_sum, "recall", "true", None, "recall"
              )
          recall[recall == 0] = correction
  
          with np.errstate(divide="ignore", invalid="ignore"):
              gmean = sp.stats.gmean(recall)
          # old version of scipy return MaskedConstant instead of 0.0
          if isinstance(gmean, np.ma.core.MaskedConstant):
              return 0.0
          return gmean
  
file_path: imbalanced-learn/imblearn/metrics/_classification.py
imports: import functools
  import numbers
  import warnings
  from inspect import signature
  import numpy
  import scipy
  from sklearn.metrics import mean_absolute_error, precision_recall_fscore_support
  from sklearn.metrics._classification import _check_targets, _prf_divide
  from sklearn.preprocessing import LabelEncoder
  from sklearn.utils.multiclass import unique_labels
  from sklearn.utils.validation import check_consistent_length, column_or_1d
  from ..utils._param_validation import Interval, StrOptions, validate_params
label_desc: Use image node_id 4 for calling a global function with example usage: geometric_mean_score(y_true, y_pred) and returns: np, gmean, int