code: def xgboost_distances_r2(
      X,
      y,
      learning_rate=0.6,
      early_stopping_rounds=2,
      subsample=1,
      max_estimators=10000,
      random_state=0,
  ):
      """Compute reducancy distances scaled from 0-1 among all the feature in X relative to the label y.
  
      Distances are measured by training univariate XGBoost models of y for all the features, and then
      predicting the output of these models using univariate XGBoost models of other features. If one
      feature can effectively predict the output of another feature's univariate XGBoost model of y,
      then the second feature is redundant with the first with respect to y. A distance of 1 corresponds
      to no redundancy while a distance of 0 corresponds to perfect redundancy (measured using the
      proportion of variance explained). Note these distances are not symmetric.
      """
  
      import xgboost
  
      # pick our train/text split
      (
          X_train,
          X_test,
          y_train,
          y_test,
      ) = sklearn.model_selection.train_test_split(
          X, y, random_state=random_state
      )
  
      # fit an XGBoost model on each of the features
      test_preds = []
      train_preds = []
      for i in range(X.shape[1]):
          model = xgboost.XGBRegressor(
              subsample=subsample,
              n_estimators=max_estimators,
              learning_rate=learning_rate,
              max_depth=1,
              early_stopping_rounds=early_stopping_rounds,
          )
          model.fit(
              X_train[:, i : i + 1],
              y_train,
              eval_set=[(X_test[:, i : i + 1], y_test)],
              verbose=False,
          )
          train_preds.append(model.predict(X_train[:, i : i + 1]))
          test_preds.append(model.predict(X_test[:, i : i + 1]))
      train_preds = np.vstack(train_preds).T
      test_preds = np.vstack(test_preds).T
  
      # fit XGBoost models to predict the outputs of other XGBoost models to see how redundant features are
      dist = np.zeros((X.shape[1], X.shape[1]))
      for i in show_progress(range(X.shape[1]), total=X.shape[1]):
          for j in range(X.shape[1]):
              if i == j:
                  dist[i, j] = 0
                  continue
  
              # skip features that have not variance in their predictions (likely because the feature is a constant)
              preds_var = np.var(test_preds[:, i])
              if preds_var < 1e-4:
                  warnings.warn(
                      f"No/low signal found from feature {i} (this is typically caused by constant or near-constant features)! Cluster distances can't be computed for it (so setting all distances to 1)."
                  )
                  r2 = 0
  
              # fit the model
              else:
                  model = xgboost.XGBRegressor(
                      subsample=subsample,
                      n_estimators=max_estimators,
                      learning_rate=learning_rate,
                      max_depth=1,
                      early_stopping_rounds=early_stopping_rounds,
                  )
                  model.fit(
                      X_train[:, j : j + 1],
                      train_preds[:, i],
                      eval_set=[
                          (X_test[:, j : j + 1], test_preds[:, i])
                      ],
                      verbose=False,
                  )
                  r2 = max(
                      0,
                      1
                      - np.mean(
                          (
                              test_preds[:, i]
                              - model.predict(X_test[:, j : j + 1])
                          )
                          ** 2
                      )
                      / preds_var,
                  )
              dist[i, j] = 1 - r2
  
      return dist
  
file_path: shap/shap/utils/_clustering.py
imports: import warnings
  import numpy
  import pandas
  import scipy.cluster
  import scipy.spatial
  import sklearn
  from numba import njit
  from ._show_progress import show_progress
label_desc: Use image node_id 9 for calling a global function with example usage: xgboost_distances_r2(X, y, learning_rate, early_stopping_rounds, subsample, max_estimators, random_state) and returns: dist