code: def get_min_nb_features(selection, contributions, mode, distance):
      """
      Determine the minimum number of features needed for the prediction \
      of the interpretability method to be *close enough* \
      to the one obtained with all features.
  
      The closeness is defined via the following distances:
  
      * For regression: 
  
          .. math::
          
              distance = \\frac{|output_{allFeatures} - output_{currentFeatures}|}{|output_{allFeatures}|}
  
      * For classification: 
  
          .. math::
  
              distance = |output_{allFeatures} - output_{currentFeatures}|
  
      Parameters
      ----------
      selection : list
          Indices of rows to be displayed on the stability plot
      contributions : DataFrame
          Calculated contribution values for the dataset
      mode : str
          "classification" or "regression"
      distance : float, optional
          How close we want to be from model with all features, by default 0.1 (10%)
  
      Returns
      -------
      features_needed : list
          List of minimum number of required features (for each instance) to be close enough to the prediction (ex: [4, 7, 8...])
      """
      assert 0 <= distance <= 1
  
      if mode == "classification" and len(contributions) == 2:
          contributions = contributions[1]
      contributions = contributions.loc[selection].values
      features_needed = []
      # For each instance, add features one by one (ordered by SHAP) until we get close enough
      for i in range(contributions.shape[0]):
          ids = np.flip(np.argsort(np.abs(contributions[i, :])))
          output_value = np.sum(contributions[i, :])
  
          score = 0
          for j, idx in enumerate(ids):
              # j : number of features needed
              # idx : positions of the j top shap values
              score += contributions[i, idx]
              # CLOSE_ENOUGH
              if mode == "regression":
                  if abs(score - output_value) < distance * abs(
                      output_value
                  ):
                      break
              elif mode == "classification":
                  if abs(score - output_value) < distance:
                      break
          features_needed.append(j + 1)
      return features_needed
  
file_path: shapash/shapash/utils/explanation_metrics.py
imports: import numpy
  import pandas
  from sklearn.preprocessing import normalize
label_desc: Use image node_id 7 for calling a global function with example usage: get_min_nb_features(selection, contributions, mode, distance) and returns: features_needed