code: def cv_permutation_importances(
      model,
      X,
      y,
      metric,
      onehot_dict=None,
      greater_is_better=True,
      needs_proba=False,
      pos_label=None,
      cv=None,
      n_repeats=1,
      n_jobs=None,
      pass_nparray=False,
      verbose=0,
  ):
      """
      Returns the permutation importances averages over `cv` cross-validated folds.
  
      Args:
          model: fitted model for which you'd like to calculate importances.
          X (pd.DataFrame): dataframe of features
          y (pd.Series): series of targets
          metric: metric to be evaluated (usually R2 for regression, roc_auc for
              classification)
          onehot_dict (dict): dict of features with lists for onehot-encoded variables,
               e.g. {'Fare': ['Fare'], 'Sex' : ['Sex_male', 'Sex_Female']}
          greater_is_better (bool): indicates whether the higher score on the metric
              indicates a better model.
          needs_proba (bool): does the metric need a classification probability
              or direct prediction?
          pos_label (int): for classification, the label to use a positive label.
              Defaults to 1.
          cv (int): number of cross-validation folds to apply.
          sort (bool): sort the output from highest importances to lowest.
          pass_nparray (bool, optional): instead of the X pass X.values to model.
              This is useful for skorch models that do not accepts dataframes.
          verbose (int): set to 1 to print output for debugging. Defaults to 0.
      """
      if cv is None:
          return permutation_importances(
              model,
              X,
              y,
              metric,
              onehot_dict,
              greater_is_better=greater_is_better,
              needs_proba=needs_proba,
              pos_label=pos_label,
              n_repeats=n_repeats,
              n_jobs=n_jobs,
              sort=False,
              pass_nparray=pass_nparray,
              verbose=verbose,
          )
  
      if needs_proba:
          skf = StratifiedKFold(
              n_splits=cv, random_state=None, shuffle=False
          )
          splitter = skf.split(X, y)
      else:
          kf = KFold(n_splits=cv, random_state=None, shuffle=False)
          splitter = kf.split(X)
  
      model = clone(model)
      for i, (train_index, test_index) in enumerate(splitter):
          X_train, X_test = X.iloc[train_index], X.iloc[test_index]
          y_train, y_test = y.iloc[train_index], y.iloc[test_index]
  
          model.fit(X_train, y_train)
  
          imp = permutation_importances(
              model,
              X_test,
              y_test,
              metric,
              onehot_dict,
              greater_is_better=greater_is_better,
              needs_proba=needs_proba,
              pos_label=pos_label,
              n_repeats=n_repeats,
              n_jobs=n_jobs,
              sort=False,
              pass_nparray=pass_nparray,
              verbose=verbose,
          )
          if i == 0:
              imps = imp[["Feature", "Importance"]]
          else:
              imps = imps.merge(
                  imp[["Feature", "Importance"]],
                  on="Feature",
                  suffixes=("", "_" + str(i)),
              )
  
      return (
          imps.set_index("Feature")
          .mean(axis=1)
          .to_frame()
          .rename(columns={0: "Importance"})
          .sort_values("Importance", ascending=False)
          .reset_index()
      )
  
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 18 for calling a global function with example usage: cv_permutation_importances(model, X, y, metric, onehot_dict, greater_is_better, needs_proba, pos_label, cv, n_repeats, n_jobs, pass_nparray, verbose) and returns: imps, permutation_importances