code: def oob_importances(rf, X_train, y_train, n_samples=5000):
      """
      Compute permutation feature importances for scikit-learn
      RandomForestClassifier or RandomForestRegressor in arg rf.
  
      Given training X and y data, return a data frame with columns
      Feature and Importance sorted in reverse order by importance.
      The training data is needed to compute out of bag (OOB)
      model performance measures (accuracy or R^2). The model
      is not retrained.
  
      By default, sample up to 5000 observations to compute feature importances.
  
      return: A data frame with Feature, Importance columns
  
      SAMPLE CODE
  
      rf = RandomForestRegressor(n_estimators=100, n_jobs=-1, oob_score=True)
      X_train, y_train = ..., ...
      rf.fit(X_train, y_train)
      imp = oob_importances(rf, X_train, y_train)
      """
      if isinstance(rf, RandomForestClassifier):
          return permutation_importances(
              rf, X_train, y_train, oob_classifier_accuracy, n_samples
          )
      elif isinstance(rf, RandomForestRegressor):
          return permutation_importances(
              rf, X_train, y_train, oob_regression_r2_score, n_samples
          )
      return None
  
imports: import pandas
  import numpy
  import matplotlib.pyplot
  import sklearn
  from sklearn.ensemble import RandomForestClassifier
  from sklearn.ensemble import RandomForestRegressor
  from distutils.version import LooseVersion
  from sklearn.model_selection import cross_val_score
  from sklearn.base import clone
  from sklearn.metrics import r2_score
  from sklearn.metrics import f1_score
  from sklearn.preprocessing import LabelEncoder
  from scipy import stats
  from pandas.api.types import is_numeric_dtype
  from matplotlib.colors import ListedColormap
  from matplotlib.ticker import FormatStrFormatter
  from copy import copy
  import warnings
  import tempfile
  from os import getpid, makedirs
label_desc: Use image node_id 4 for calling a global function with example usage: oob_importances(rf, X_train, y_train, n_samples) and returns: None, permutation_importances, permutation_importances