code: def _prediction_feature_weights(
      booster, dmatrix, n_targets, feature_names, xgb_feature_names
  ):
      """For each target, return score and numpy array with feature weights
      on this prediction, following an idea from
      http://blog.datadive.net/interpreting-random-forests/
      """
      # XGBClassifier does not have pred_leaf argument, so use booster
      (leaf_ids,) = booster.predict(dmatrix, pred_leaf=True)
      xgb_feature_names = {
          f: i for i, f in enumerate(xgb_feature_names)
      }
      tree_dumps = booster.get_dump(with_stats=True)
      assert len(tree_dumps) == len(leaf_ids)
  
      target_feature_weights = partial(
          _target_feature_weights,
          feature_names=feature_names,
          xgb_feature_names=xgb_feature_names,
      )
      if n_targets > 1:
          # For multiclass, XGBoost stores dumps and leaf_ids in a 1d array,
          # so we need to split them.
          scores_weights = [
              target_feature_weights(
                  leaf_ids[target_idx::n_targets],
                  tree_dumps[target_idx::n_targets],
              )
              for target_idx in range(n_targets)
          ]
      else:
          scores_weights = [
              target_feature_weights(leaf_ids, tree_dumps)
          ]
      return scores_weights
  
imports: from __future__ import absolute_import
  from functools import partial
  import re
  from typing import Any, Dict, List, Tuple, Optional, Pattern
  import numpy
  import scipy.sparse
  from xgboost import XGBClassifier, XGBRegressor, Booster, DMatrix
  from eli5.explain import explain_weights, explain_prediction
  from eli5.sklearn.utils import add_intercept, get_X, get_X0, handle_vec, predict_proba
  from eli5.utils import is_sparse_vector
  from eli5._decision_path import get_decision_path_explanation
  from eli5._feature_importances import get_feature_importance_explanation
label_desc: Use image node_id 4 for calling a global function with example usage: _prediction_feature_weights(booster, dmatrix, n_targets, feature_names, xgb_feature_names) and returns: scores_weights