code: def feature_dependence_matrix(
      X_train,
      rfrmodel=RandomForestRegressor(n_estimators=50, oob_score=True),
      rfcmodel=RandomForestClassifier(n_estimators=50, oob_score=True),
      cat_count=20,
      zero=0.001,
      sort_by_dependence=False,
      n_samples=5000,
  ):
      """
      Given training observation independent variables in X_train (a dataframe),
      compute the feature importance using each var as a dependent variable using
      a RandomForestRegressor or RandomForestClassifier. A RandomForestClassifer is
      used when the number of the unique values for the dependent variable is less or
      equal to the cat_count arg. We retrain a random forest for each var as target
      using the others as independent vars. Only numeric columns are considered.
  
      By default, sample up to 5000 observations to compute feature dependencies.
  
      If feature importance is less than zero arg, force to 0. Force all negatives to 0.0.
      Clip to 1.0 max. (Some importances could come back > 1.0 because removing that
      feature sends R^2 very negative.)
  
      :return: a non-symmetric data frame with the dependence matrix where each row is the importance of each var to the row's var used as a model target.
      """
      numeric_cols = [
          col for col in X_train if is_numeric_dtype(X_train[col])
      ]
  
      cat_cols = [
          col
          for col in numeric_cols
          if X_train[col].value_counts().count() <= cat_count
      ]
      cat_cols_le = [
          col for col in cat_cols if X_train[col].dtypes == "float"
      ]
      for col in cat_cols_le:
          le = LabelEncoder()
          X_train[col] = le.fit_transform(X_train[col])
  
      X_train = sample_rows(X_train, n_samples)
  
      df_dep = pd.DataFrame(
          index=X_train.columns,
          columns=["Dependence"] + X_train.columns.tolist(),
      )
      for i, col in enumerate(numeric_cols):
          X, y = X_train.drop(col, axis=1), X_train[col]
          if col in cat_cols:
              rf = clone(rfcmodel)
              rf.fit(X, y)
              imp = permutation_importances_raw(
                  rf, X, y, oob_classifier_f1_score, n_samples
              )
          else:
              rf = clone(rfrmodel)
              rf.fit(X, y)
              imp = permutation_importances_raw(
                  rf, X, y, oob_regression_r2_score, n_samples
              )
          """
          Some RandomForestRegressor importances could come back > 1.0 because removing
          that feature sends R^2 very negative. Clip them at 1.0.  Also, features with 
          negative importance means that taking them out helps predict but we don't care
          about that here. We want to know which features are collinear/predictive. Clip
          at 0.0.
          """
          imp = np.clip(imp, a_min=0.0, a_max=1.0)
          imp[imp < zero] = 0.0
          imp = np.insert(imp, i, 1.0)
          df_dep.iloc[i] = np.insert(
              imp, 0, rf.oob_score_
          )  # add overall dependence
  
      if sort_by_dependence:
          return df_dep.sort_values("Dependence", ascending=False)
      return df_dep
  
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 18 for calling a global function with example usage: feature_dependence_matrix(X_train, rfrmodel, rfcmodel, cat_count, zero, sort_by_dependence, n_samples) and returns: df_dep, df_dep