code: def null_rmse_distribution(
      model: SklearnModel,
      X: Union[pd.DataFrame, np.ndarray],
      y: np.ndarray,
      variable: int,
      n_k_fold_splits: int,
      n_permutations: int = 10,
  ) -> List[float]:
      """
      Calculate a null distribution on the RMSEs after scrambling a variable
  
      Works by randomly permuting y to remove any true dependence of y on X and calculating feature importance
  
      RMSEs are calculated on out of sample data
  
      Parameters
      ----------
      model: SklearnModel
          Model specification to work with
      X: np.ndarray
          Covariate matrix
      y: np.ndarray
          Target data
      variable: int
          Which column of the covariate matrix to permute
      n_k_fold_splits: int
          How many K-fold splits to make of the data
      n_permutations: int
          How many permutations to run
          The higher the number of permutations, the more accurate the null distribution, but the longer it will take to run
      Returns
      -------
      List[float]
          A list of predict set RMSEs - one entry for each fold of each permutation
      """
  
      kf = KFold(n_k_fold_splits, shuffle=True)
  
      permuted_train_X_s = []
      permuted_test_X_s = []
      train_y_s = []
      test_y_s = []
  
      for train_index, test_index in kf.split(X):
          for _ in range(n_permutations):
              permuted_X = deepcopy(X)
              permuted_X[:, variable] = np.random.permutation(
                  permuted_X[:, variable]
              )
              permuted_train_X_s.append(permuted_X[train_index])
              permuted_test_X_s.append(permuted_X[train_index])
              train_y_s.append(y[train_index])
              test_y_s.append(y[test_index])
  
      fit_models = run_models(model, permuted_train_X_s, train_y_s)
  
      rmses = []
      for i, m in enumerate(fit_models):
          rmses.append(m.rmse(permuted_test_X_s[i], test_y_s[i]))
      return rmses
  
file_path: imodels/imodels/experimental/bartpy/features/featureimportance.py
imports: from copy import deepcopy
  from typing import List, Tuple, Union
  import numpy
  import pandas
  from matplotlib import pyplot
  from sklearn.model_selection import KFold
  from ..runner import run_models
  from ..sklearnmodel import SklearnModel
label_desc: Use image node_id 2 for calling a global function with example usage: null_rmse_distribution(model, X, y, variable, n_k_fold_splits, n_permutations) and returns: rmses