code: def cv_importances(model, X_train, y_train, k=3):
      """
      Compute permutation feature importances for scikit-learn models using
      k-fold cross-validation (default k=3).
  
      Given a Classifier or Regressor in model
      and training X and y data, return a data frame with columns
      Feature and Importance sorted in reverse order by importance.
      Cross-validation observations are taken from X_train, y_train.
  
      The model.score() method is called to measure accuracy drops.
  
      return: A data frame with Feature, Importance columns
  
      SAMPLE CODE
  
      rf = RandomForestRegressor(n_estimators=100, n_jobs=-1)
      X_train, y_train = ..., ...
      rf.fit(X_train, y_train)
      imp = cv_importances(rf, X_train, y_train)
      """
  
      def score(model):
          cvscore = cross_val_score(
              model,  # which model to use
              X_train,
              y_train,  # what training data to split up
              cv=k,
          )  # number of folds/chunks
          return np.mean(cvscore)
  
      X_train = X_train.copy(deep=False)  # shallow copy
      baseline = score(model)
      imp = []
      for col in X_train.columns:
          save = X_train[col].copy()
          X_train[col] = np.random.permutation(X_train[col])
          m = score(model)
          X_train[col] = save
          imp.append(baseline - m)
  
      I = pd.DataFrame(
          data={"Feature": X_train.columns, "Importance": np.array(imp)}
      )
      I = I.set_index("Feature")
      I = I.sort_values("Importance", ascending=False)
      return I
  
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 5 for calling a global function with example usage: cv_importances(model, X_train, y_train, k) and returns: I, np