code: def _sklearn_avg_precision_multilabel(
      preds, target, average="macro", ignore_index=None
  ):
      if average == "micro":
          return _sklearn_avg_precision_binary(
              preds.flatten(), target.flatten(), ignore_index
          )
      res = [
          _sklearn_avg_precision_binary(
              preds[:, i], target[:, i], ignore_index
          )
          for i in range(NUM_CLASSES)
      ]
      if average == "macro":
          return np.array(res)[~np.isnan(res)].mean()
      if average == "weighted":
          weights = (
              (target == 1).sum([0, 2])
              if target.ndim == 3
              else (target == 1).sum(0)
          ).numpy()
          weights = weights / sum(weights)
          return (np.array(res) * weights)[~np.isnan(res)].sum()
      return res
  
imports: from functools import partial
  import numpy
  import pytest
  import torch
  from scipy.special import expit
  from scipy.special import softmax
  from sklearn.metrics import average_precision_score
  from torchmetrics.classification.average_precision import AveragePrecision, BinaryAveragePrecision, MulticlassAveragePrecision, MultilabelAveragePrecision
  from torchmetrics.functional.classification.average_precision import binary_average_precision, multiclass_average_precision, multilabel_average_precision
  from torchmetrics.functional.classification.precision_recall_curve import binary_precision_recall_curve
  from torchmetrics.metric import Metric
  from unittests import NUM_CLASSES
  from unittests.classification.inputs import _binary_cases, _multiclass_cases, _multilabel_cases
  from unittests.helpers import seed_all
  from unittests.helpers.testers import MetricTester, inject_ignore_index, remove_ignore_index
label_desc: Use image node_id 3 for calling a global function with example usage: _sklearn_avg_precision_multilabel(preds, target, average, ignore_index) and returns: res, _sklearn_avg_precision_binary, np, unknown