code: def get_rules(tree, feature_names, class_names):
      tree_ = tree.tree_
      feature_name = [
          feature_names[i]
          if i != _tree.TREE_UNDEFINED
          else "undefined!"
          for i in tree_.feature
      ]
  
      paths = []
      path = []
  
      def recurse(node, path, paths):
          if tree_.feature[node] != _tree.TREE_UNDEFINED:
              name = feature_name[node]
              threshold = tree_.threshold[node]
              p1, p2 = list(path), list(path)
              p1 += [f"({name} <= {np.round(threshold, 3)})"]
              recurse(tree_.children_left[node], p1, paths)
              p2 += [f"({name} > {np.round(threshold, 3)})"]
              recurse(tree_.children_right[node], p2, paths)
          else:
              path += [(tree_.value[node], tree_.n_node_samples[node])]
              paths += [path]
  
      recurse(0, path, paths)
  
      # sort by samples count
      samples_count = [p[-1][1] for p in paths]
      ii = list(np.argsort(samples_count))
      paths = [paths[i] for i in reversed(ii)]
  
      rules = []
      for path in paths:
          rule = "if "
  
          for p in path[:-1]:
              if rule != "if ":
                  rule += " and "
              rule += str(p)
          rule += " then "
          if class_names is None:
              rule += "response: " + str(np.round(path[-1][0][0][0], 3))
          else:
              classes = path[-1][0][0]
              l = np.argmax(classes)
              rule += f"class: {class_names[l]} (proba: {np.round(100.0*classes[l]/np.sum(classes),2)}%)"
          rule += f" | based on {path[-1][1]:,} samples"
          rules += [rule]
  
      return rules
  
imports: import logging
  import os
  import warnings
  import numpy
  import sklearn
  from sklearn.base import BaseEstimator, ClassifierMixin, RegressorMixin
  from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
  from supervised.algorithms.algorithm import BaseAlgorithm
  from supervised.algorithms.registry import BINARY_CLASSIFICATION, MULTICLASS_CLASSIFICATION, REGRESSION, AlgorithmsRegistry
  from supervised.algorithms.sklearn import SklearnAlgorithm
  from supervised.utils.config import LOG_LEVEL
  import dtreeviz
  from sklearn.tree import _tree
  from supervised.utils.subsample import subsample
label_desc: Use image node_id 1 for calling a global function with example usage: get_rules(tree, feature_names, class_names) and returns: rules