Base Node¶
BaseNode
¶
Bases: ABC
Abstract base class for all node types in a tree.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
int
|
Unique identifier assigned to the node, or None if unset. |
indices |
ndarray
|
Row indices of the samples associated with this node. |
y_res |
ndarray
|
Residuals of the target variable at this node. |
log_odds |
ndarray | None
|
Log-odds of the samples at this node from the model before this node, None for regression trees. |
Source code in imote/nodes/base_node.py
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |
class_registry = {}
class-attribute
instance-attribute
¶
Maps subclass names to subclass objects, for use in from_dict().
__init__(indices: np.ndarray, y_res: np.ndarray)
¶
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
indices
|
ndarray
|
Row indices of the samples belonging to this node. |
required |
y_res
|
ndarray
|
Residuals of the target variable at this node. |
required |
Source code in imote/nodes/base_node.py
24 25 26 27 28 29 30 31 32 | |
set_id(id: int)
¶
Sets the unique identifier of the node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
int
|
Identifier to assign. |
required |
Source code in imote/nodes/base_node.py
34 35 36 37 38 39 40 | |
get_children() -> List[BaseNode]
abstractmethod
¶
Returns the list of child nodes of this node.
Returns:
| Type | Description |
|---|---|
List[BaseNode]
|
List of child BaseNode instances (empty for leaf-like nodes). |
Source code in imote/nodes/base_node.py
42 43 44 45 46 47 48 49 | |
get_all_children() -> List[BaseNode]
¶
Returns all descendant nodes of this node, recursively.
Returns:
| Type | Description |
|---|---|
List[BaseNode]
|
Flat list of all descendant BaseNode instances, in |
List[BaseNode]
|
depth-first order. |
Source code in imote/nodes/base_node.py
51 52 53 54 55 56 57 58 59 60 61 62 | |
get_label(fmt: LabelFormatter) -> str
abstractmethod
¶
Returns the full display label for this node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt
|
LabelFormatter
|
Formats the feature names, numbers and expressions in the label. |
required |
Returns:
| Type | Description |
|---|---|
str
|
String of label, shown in a tree visualization. |
Source code in imote/nodes/base_node.py
64 65 66 67 68 69 70 71 72 73 74 | |
get_minimal_label(fmt: LabelFormatter) -> str
abstractmethod
¶
Returns a short display label for this node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt
|
LabelFormatter
|
Formats the feature names, numbers and expressions in the label. |
required |
Returns:
| Type | Description |
|---|---|
str
|
String of minimal label, used when space is limited. |
Source code in imote/nodes/base_node.py
76 77 78 79 80 81 82 83 84 85 86 | |
get_description(fmt: LabelFormatter) -> str
abstractmethod
¶
Returns a one line description of what this node does, without its type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmt
|
LabelFormatter
|
Formats the feature names, numbers and expressions in the label. |
required |
Returns:
| Type | Description |
|---|---|
str
|
String of description, shown in the node information. |
Source code in imote/nodes/base_node.py
88 89 90 91 92 93 94 95 96 97 98 | |
to_dict() -> dict
¶
Serializes the node's base attributes to a dictionary.
Subclasses should call this and extend the result with their own attributes.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with the node's id, indices, y_res and log_odds. |
Source code in imote/nodes/base_node.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
from_dict(node_dict: dict) -> BaseNode
classmethod
¶
Reconstructs the node's base attributes from a dictionary.
Subclasses should call this and populate their own attributes on the returned object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
node_dict
|
dict
|
Dictionary of node previously produced by to_dict(). |
required |
Returns:
| Type | Description |
|---|---|
BaseNode
|
A new instance of cls with base attributes restored. |
Source code in imote/nodes/base_node.py
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
register(subclass: Type[T]) -> Type[T]
classmethod
¶
Class decorator that registers a node subclass by name.
Registration is required so that from_dict() can look up the correct class when deserializing a node whose type is only known by name (e.g. a child stored as "left_child_class").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
subclass
|
Type[T]
|
The BaseNode subclass to register. |
required |
Returns:
| Type | Description |
|---|---|
Type[T]
|
The subclass, unchanged (so it can be used as a decorator). |
Source code in imote/nodes/base_node.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | |