code: def oob_regression_r2_score(rf, X_train, y_train):
      """
      Compute out-of-bag (OOB) R^2 for a scikit-learn random forest
      regressor. We learned the guts of scikit's RF from the BSD licensed
      code:
  
      https://github.com/scikit-learn/scikit-learn/blob/a24c8b46/sklearn/ensemble/forest.py#L702
      """
      X = (
          X_train.values
          if isinstance(X_train, pd.DataFrame)
          else X_train
      )
      y = y_train.values if isinstance(y_train, pd.Series) else y_train
  
      n_samples = len(X)
      predictions = np.zeros(n_samples)
      n_predictions = np.zeros(n_samples)
      for tree in rf.estimators_:
          unsampled_indices = _get_unsampled_indices(tree, n_samples)
          tree_preds = tree.predict(X[unsampled_indices, :])
          predictions[unsampled_indices] += tree_preds
          n_predictions[unsampled_indices] += 1
  
      if (n_predictions == 0).any():
          warnings.warn(
              "Too few trees; some variables do not have OOB scores."
          )
          n_predictions[n_predictions == 0] = 1
  
      predictions /= n_predictions
  
      oob_score = r2_score(y, predictions)
      return oob_score
  
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 14 for calling a global function with example usage: oob_regression_r2_score(rf, X_train, y_train) and returns: oob_score