code: def rank_by_importance(
      shap_values: List[np.ndarray],
      feature_names: Union[List[str], Tuple[str], None] = None,
  ) -> Dict:
      """
      Given the shap values estimated for a multi-output model, this function ranks
      features according to their importance. The feature importance is the average
      absolute value for a given feature.
  
      Parameters
      ----------
      shap_values
          Each element corresponds to a `samples x features` array of shap values corresponding
          to each model output.
      feature_names
          Each element is the name of the column with the corresponding index in each of the
          arrays in the `shap_values` list.
  
      Returns
      -------
      importances
          A dictionary of the form::
  
              {
                  '0': {'ranked_effect': array([0.2, 0.5, ...]), 'names': ['feat_3', 'feat_5', ...]},
                  '1': {'ranked_effect': array([0.3, 0.2, ...]), 'names': ['feat_6', 'feat_1', ...]},
                  ...
                  'aggregated': {'ranked_effect': array([0.9, 0.7, ...]), 'names': ['feat_3', 'feat_6', ...]}
              }
  
          The keys of the first level represent the index of the model output. The feature effects in
          `ranked_effect` and the corresponding feature names in `names` are sorted from highest (most
          important) to lowest (least important). The values in the `aggregated` field are obtained by
          summing the shap values for all the model outputs and then computing the effects. Given an
          output, the effects are defined as the average magnitude of the shap values across the instances
          to be explained.
      """
  
      if len(shap_values[0].shape) == 1:
          shap_values = [np.atleast_2d(arr) for arr in shap_values]
  
      if not feature_names:
          feature_names = [
              "feature_{}".format(i)
              for i in range(shap_values[0].shape[1])
          ]
      else:
          if len(feature_names) != shap_values[0].shape[1]:
              msg = (
                  "The feature names provided do not match the number of shap values estimated. "
                  "Received {} feature names but estimated {} shap values!"
              )
              logger.warning(
                  msg.format(
                      len(feature_names), shap_values[0].shape[1]
                  )
              )
              feature_names = [
                  "feature_{}".format(i)
                  for i in range(shap_values[0].shape[1])
              ]
  
      importances: Dict[
          str, Dict[str, Union[np.ndarray, List[str]]]
      ] = {}
      avg_mag: List = []
  
      # rank the features by average shap value for each class in turn
      for class_idx in range(len(shap_values)):
          avg_mag_shap = np.abs(shap_values[class_idx]).mean(axis=0)
          avg_mag.append(avg_mag_shap)
          feature_order = np.argsort(avg_mag_shap)[::-1]
          most_important = avg_mag_shap[feature_order]
          most_important_names = [
              feature_names[i] for i in feature_order
          ]
          importances[str(class_idx)] = {
              "ranked_effect": most_important,
              "names": most_important_names,
          }
  
      # rank feature by average shap value for aggregated classes
      combined_shap = np.sum(avg_mag, axis=0)
      feature_order = np.argsort(combined_shap)[::-1]
      most_important_c = combined_shap[feature_order]
      most_important_c_names = [feature_names[i] for i in feature_order]
      importances["aggregated"] = {
          "ranked_effect": most_important_c,
          "names": most_important_c_names,
      }
  
      return importances
  
imports: import copy
  import logging
  from functools import partial
  from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional, Sequence, Tuple, Union
  import numpy
  import pandas
  import shap
  import shap.utils._legacy
  from scipy import sparse
  from scipy.special import expit
  from shap import KernelExplainer
  from alibi.api.defaults import DEFAULT_DATA_KERNEL_SHAP, DEFAULT_DATA_TREE_SHAP, DEFAULT_META_KERNEL_SHAP, DEFAULT_META_TREE_SHAP
  from alibi.api.interfaces import Explainer, Explanation, FitMixin
  from alibi.utils.wrappers import methdispatch
label_desc: Use image node_id 1 for calling a global function with example usage: rank_by_importance(shap_values, feature_names) and returns: importances