code: def batch_keep_retrain(
      nkeep_train,
      nkeep_test,
      X_train,
      y_train,
      X_test,
      y_test,
      attr_train,
      attr_test,
      model_generator,
      metric,
  ):
      """An approximation of keep that only retraines the model once.
  
      This is also called KAR (Keep And Retrain) in work by Google. It is much more computationally
      efficient that the keep method because it masks the unimportant features in every sample
      and then retrains the model once, instead of retraining the model for every test sample like
      the keep metric.
      """
  
      warnings.warn(
          "The retrain based measures can incorrectly evaluate models in some cases!"
      )
  
      X_train, X_test = to_array(X_train, X_test)
  
      # how many features to mask
      assert X_train.shape[1] == X_test.shape[1]
  
      # mask nkeep top features for each explanation
      X_train_tmp = X_train.copy()
      X_train_mean = X_train.mean(0)
      tie_breaking_noise = const_rand(X_train.shape[1]) * 1e-6
      for i in range(len(y_train)):
          if nkeep_train[i] < X_train.shape[1]:
              ordering = np.argsort(
                  -attr_train[i, :] + tie_breaking_noise
              )
              X_train_tmp[i, ordering[nkeep_train[i] :]] = X_train_mean[
                  ordering[nkeep_train[i] :]
              ]
      X_test_tmp = X_test.copy()
      for i in range(len(y_test)):
          if nkeep_test[i] < X_test.shape[1]:
              ordering = np.argsort(
                  -attr_test[i, :] + tie_breaking_noise
              )
              X_test_tmp[i, ordering[nkeep_test[i] :]] = X_train_mean[
                  ordering[nkeep_test[i] :]
              ]
  
      # train the model with all the features not given masked
      model_masked = model_generator()
      model_masked.fit(X_train_tmp, y_train)
      yp_test_masked = model_masked.predict(X_test_tmp)
  
      return metric(y_test, yp_test_masked)
  
file_path: shap/shap/benchmark/measures.py
imports: import warnings
  import numpy
  import pandas
  import sklearn.utils
  from tqdm.auto import tqdm
label_desc: Use image node_id 10 for calling a global function with example usage: batch_keep_retrain(nkeep_train, nkeep_test, X_train, y_train, X_test, y_test, attr_train, attr_test, model_generator, metric) and returns: metric