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. |
rss |
float
|
Residual sum of squares at this node. |
Source code in imote/nodes/base_node.py
8 9 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 | |
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, rss: float)
¶
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 |
rss
|
float
|
Residual sum of squares at this node. |
required |
Source code in imote/nodes/base_node.py
22 23 24 25 26 27 28 29 30 31 | |
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
33 34 35 36 37 38 39 | |
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
41 42 43 44 45 46 47 48 | |
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
50 51 52 53 54 55 56 57 58 59 60 61 | |
get_label() -> str
abstractmethod
¶
Returns the full display label for this node.
Returns:
| Type | Description |
|---|---|
str
|
String of label, shown in a tree visualization. |
Source code in imote/nodes/base_node.py
63 64 65 66 67 68 69 70 | |
get_minimal_label() -> str
abstractmethod
¶
Returns a short display label for this node.
Returns:
| Type | Description |
|---|---|
str
|
String of minimal label, used when space is limited. |
Source code in imote/nodes/base_node.py
72 73 74 75 76 77 78 79 | |
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, rss, indices, and y_res. |
Source code in imote/nodes/base_node.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
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
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 | |
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
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |