code: def feature_importance(
      rules: Union[str, list], silent: bool = False
  ) -> pd.DataFrame:
      """
      Show the feature importance values of a built model
      Args:
          rules (str or list): e.g. decision_rules = "outputs/rules/rules.py"
              or this could be retrieved from built model as shown below.
  
              ```python
              decision_rules = []
              for tree in model["trees"]:
                 rule = .__dict__["__spec__"].origin
                 decision_rules.append(rule)
              ```
          silent (bool): set this to True if you do want to see
              any informative logs.
      Returns:
              feature importance (pd.DataFrame)
      """
  
      if not isinstance(rules, list):
          rules = [rules]
  
      if silent is False:
          logger.info(f"rules: {rules}")
  
      # -----------------------------
  
      dfs = []
  
      for rule in rules:
          if silent is False:
              logger.info(f"Decision rule: {rule}")
  
          with open(rule, "r", encoding="UTF-8") as file:
              lines = file.readlines()
  
          pivot = {}
          rules = []
  
          # initialize feature importances
          line_idx = 0
          for line in lines:
              if line_idx == 0:
                  feature_explainer_list = line.split("#")[1].split(
                      ", "
                  )
                  for feature_explainer in feature_explainer_list:
                      feature = feature_explainer.split(": ")[
                          1
                      ].replace("\n", "")
                      pivot[feature] = 0
              else:
                  if "# " in line:
                      rule = line.strip().split("# ")[1]
                      rules.append(json.loads(rule))
  
              line_idx = line_idx + 1
  
          feature_names = list(pivot.keys())
  
          for feature in feature_names:
              for rule in rules:
                  if rule["feature"] == feature:
                      score = rule["metric_value"] * rule["instances"]
                      current_depth = rule["depth"]
  
                      child_scores = 0
                      # find child node importances
                      for child_rule in rules:
                          if child_rule["depth"] == current_depth + 1:
                              child_score = (
                                  child_rule["metric_value"]
                                  * child_rule["instances"]
                              )
  
                              child_scores = child_scores + child_score
  
                      score = score - child_scores
  
                      pivot[feature] = pivot[feature] + score
  
          # normalize feature importance
  
          total_score = 0
          for feature, score in pivot.items():
              total_score = total_score + score
  
          for feature, score in pivot.items():
              pivot[feature] = round(pivot[feature] / total_score, 4)
  
          instances = []
          for feature, score in pivot.items():
              instance = []
              instance.append(feature)
              instance.append(score)
              instances.append(instance)
  
          df = pd.DataFrame(
              instances, columns=["feature", "final_importance"]
          )
          df = df.sort_values(by=["final_importance"], ascending=False)
  
          if len(rules) == 1:
              return df
          else:
              dfs.append(df)
  
      if len(rules) > 1:
          hf = pd.DataFrame(feature_names, columns=["feature"])
          hf["importance"] = 0
  
          for df in dfs:
              hf = hf.merge(df, on=["feature"], how="left")
              hf["importance"] = (
                  hf["importance"] + hf["final_importance"]
              )
              hf = hf.drop(columns=["final_importance"])
  
          # ------------------------
          # normalize
          hf["importance"] = hf["importance"] / hf["importance"].sum()
          hf = hf.sort_values(by=["importance"], ascending=False)
  
          return hf
  
file_path: chefboost/chefboost/Chefboost.py
imports: import time
  import pickle
  import os
  import json
  from typing import Optional, Dict, Any, Union
  import numpy
  import pandas
  from chefboost.commons import functions, evaluate
  from chefboost.training import Training
  from chefboost.tuning import gbm, adaboost, randomforest
  from chefboost.commons.logger import Logger
label_desc: Use image node_id 6 for calling a global function with example usage: feature_importance(rules, silent) and returns: hf, df