code: def importances(
      model,
      X_valid,
      y_valid,
      features=None,
      n_samples=5000,
      sort=True,
      metric=None,
      sample_weights=None,
  ):
      """
      Compute permutation feature importances for scikit-learn models using
      a validation set.
  
      Given a Classifier or Regressor in model
      and validation X and y data, return a data frame with columns
      Feature and Importance sorted in reverse order by importance.
      The validation data is needed to compute model performance
      measures (accuracy or R^2). The model is not retrained.
  
      You can pass in a list with a subset of features interesting to you.
      All unmentioned features will be grouped together into a single meta-feature
      on the graph. You can also pass in a list that has sublists like:
      [['latitude', 'longitude'], 'price', 'bedrooms']. Each string or sublist
      will be permuted together as a feature or meta-feature; the drop in
      overall accuracy of the model is the relative importance.
  
      The model.score() method is called to measure accuracy drops.
  
      This version that computes accuracy drops with the validation set
      is much faster than the OOB, cross validation, or drop column
      versions. The OOB version is a less vectorized because it needs to dig
      into the trees to get out of examples. The cross validation and drop column
      versions need to do retraining and are necessarily much slower.
  
      This function used OOB not validation sets in 1.0.5; switched to faster
      test set version for 1.0.6. (breaking API change)
  
      :param model: The scikit model fit to training data
      :param X_valid: Data frame with feature vectors of the validation set
      :param y_valid: Series with target variable of validation set
      :param features: The list of features to show in importance graph.
                       These can be strings (column names) or lists of column
                       names. E.g., features = ['bathrooms', ['latitude', 'longitude']].
                       Feature groups can overlap, with features appearing in multiple.
      :param n_samples: How many records of the validation set to use
                        to compute permutation importance. The default is
                        5000, which we arrived at by experiment over a few data sets.
                        As we cannot be sure how all data sets will react,
                        you can pass in whatever sample size you want. Pass in -1
                        to mean entire validation set. Our experiments show that
                        not too many records are needed to get an accurate picture of
                        feature importance.
      :param sort: Whether to sort the resulting importances
      :param metric: Metric in the form of callable(model, X_valid, y_valid, sample_weights) to evaluate for,
                      if not set default's to model.score()
      :param sample_weights: set if a different weighting is required for the validation samples
  
  
      return: A data frame with Feature, Importance columns
  
      SAMPLE CODE
  
      rf = RandomForestRegressor(n_estimators=100, n_jobs=-1)
      X_train, y_train = ..., ...
      X_valid, y_valid = ..., ...
      rf.fit(X_train, y_train)
      imp = importances(rf, X_valid, y_valid)
      """
  
      def flatten(features):
          all_features = set()
          for sublist in features:
              if isinstance(sublist, str):
                  all_features.add(sublist)
              else:
                  for item in sublist:
                      all_features.add(item)
          return all_features
  
      if features is None:
          # each feature in its own group
          features = X_valid.columns.values
      else:
          req_feature_set = flatten(features)
          model_feature_set = set(X_valid.columns.values)
          # any features left over?
          other_feature_set = model_feature_set.difference(
              req_feature_set
          )
          if len(other_feature_set) > 0:
              # if leftovers, we need group together as single new feature
              features.append(list(other_feature_set))
  
      X_valid, y_valid, sample_weights = sample(
          X_valid, y_valid, n_samples, sample_weights=sample_weights
      )
      X_valid = X_valid.copy(deep=False)  # we're modifying columns
  
      if callable(metric):
          baseline = metric(model, X_valid, y_valid, sample_weights)
      else:
          baseline = model.score(X_valid, y_valid, sample_weights)
  
      imp = []
      for group in features:
          if isinstance(group, str):
              save = X_valid[group].copy()
              X_valid[group] = np.random.permutation(X_valid[group])
              if callable(metric):
                  m = metric(model, X_valid, y_valid, sample_weights)
              else:
                  m = model.score(X_valid, y_valid, sample_weights)
              X_valid[group] = save
          else:
              save = {}
              for col in group:
                  save[col] = X_valid[col].copy()
              for col in group:
                  X_valid[col] = np.random.permutation(X_valid[col])
  
              if callable(metric):
                  m = metric(model, X_valid, y_valid, sample_weights)
              else:
                  m = model.score(X_valid, y_valid, sample_weights)
              for col in group:
                  X_valid[col] = save[col]
          imp.append(baseline - m)
  
      # Convert and groups/lists into string column names
      labels = []
      for col in features:
          if isinstance(col, list):
              labels.append("\n".join(col))
          else:
              labels.append(col)
  
      I = pd.DataFrame(
          data={"Feature": labels, "Importance": np.array(imp)}
      )
      I = I.set_index("Feature")
      if sort:
          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 1 for calling a global function with example usage: importances(model, X_valid, y_valid, features, n_samples, sort, metric, sample_weights) and returns: I, all_features