Skip to content

Instantly share code, notes, and snippets.

@Eligijus112
Created June 2, 2022 03:53
Show Gist options
  • Save Eligijus112/699487fe286daafd7a86a8f8ae291ae8 to your computer and use it in GitHub Desktop.
Save Eligijus112/699487fe286daafd7a86a8f8ae291ae8 to your computer and use it in GitHub Desktop.
Calculates the node importance in a decision tree
def node_importance(
node_main: str,
node_left: str,
node_right: str,
n_entries_weight: dict,
i_sq: dict
) -> float:
"""
Calculated the importance of the node_main
Arguments
---------
node_main: str
The main parent node
node_left: str
The left child node
node_right: str
The right child node
n_entries_weight: dict
The weight of each node, calculated by the samples in node divided by the total number of samples
i_sq: dict
The squared error of each node
Returns
-------
float
The importance of the node_main
"""
# Calculating the main nodes part of the equation
_main_part = n_entries_weight[node_main] * i_sq[node_main]
# Calculating the left child nodes part of the equation
_left_part = n_entries_weight[node_left] * i_sq[node_left]
# Calculating the right child nodes part of the equation
_right_part = n_entries_weight[node_right] * i_sq[node_right]
# Returning the gain of the node_main; This gain is the importance of the node_main
return _main_part - _left_part - _right_part
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment