Skip to content

LeafNode

LeafNode

Bases: BaseNode

A terminal tree node holding the model used for its predictions.

Attributes:

Name Type Description
node_model LinearNodeModel

The prediction model for samples reaching this leaf.

Source code in imote/nodes/leaf_node.py
 6
 7
 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
@BaseNode.register
class LeafNode(BaseNode):
    """A terminal tree node holding the model used for its predictions.

    Attributes:
        node_model: The prediction model for samples reaching this leaf.
    """

    node_model: LinearNodeModel = None

    def __init__(self, indices: np.ndarray, y_res: np.ndarray, rss: float,
                 node_model: LinearNodeModel):
        """
        Args:
            indices: Row indices of the samples belonging to this node.
            y_res: Residuals of the target variable at this node.
            rss: Residual sum of squares at this node.
            node_model: The prediction model for samples reaching this leaf.
        """
        super().__init__(indices, y_res, rss)
        self.node_model = node_model

    def get_children(self) -> List[BaseNode]:
        """Returns this node's children.

        Returns:
            Empty list, a leaf node has no children.
        """
        return []

    def get_label(self) -> str:
        """Returns the full display label for this node.

        Returns:
            Label of the form "Leaf\\n<node_model label>".
        """
        return "Leaf\n" + self.node_model.get_label()

    def get_minimal_label(self) -> str:
        """Returns a short display label for this node.

        Returns:
            The string "L".
        """
        return "L"

    def to_dict(self) -> dict:
        """Serializes the node to a dictionary.

        Returns:
            Dictionary with base attributes plus the serialized node_model.
        """
        node_dict = super().to_dict()
        node_dict["node_model"] = self.node_model.to_dict()
        return node_dict

    @classmethod
    def from_dict(cls, dic: dict) -> "LeafNode":
        """Reconstructs a LeafNode instance, including its model.

        Args:
            dic: Dictionary previously produced by to_dict().

        Returns:
            A new LeafNode instance with its node_model restored.
        """
        obj = super().from_dict(dic)
        obj.node_model = node_model_from_dict(dic["node_model"])
        return obj

__init__(indices: np.ndarray, y_res: np.ndarray, rss: float, node_model: LinearNodeModel)

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
node_model LinearNodeModel

The prediction model for samples reaching this leaf.

required
Source code in imote/nodes/leaf_node.py
16
17
18
19
20
21
22
23
24
25
26
def __init__(self, indices: np.ndarray, y_res: np.ndarray, rss: float,
             node_model: LinearNodeModel):
    """
    Args:
        indices: Row indices of the samples belonging to this node.
        y_res: Residuals of the target variable at this node.
        rss: Residual sum of squares at this node.
        node_model: The prediction model for samples reaching this leaf.
    """
    super().__init__(indices, y_res, rss)
    self.node_model = node_model

get_children() -> List[BaseNode]

Returns this node's children.

Returns:

Type Description
List[BaseNode]

Empty list, a leaf node has no children.

Source code in imote/nodes/leaf_node.py
28
29
30
31
32
33
34
def get_children(self) -> List[BaseNode]:
    """Returns this node's children.

    Returns:
        Empty list, a leaf node has no children.
    """
    return []

get_label() -> str

Returns the full display label for this node.

Returns:

Type Description
str

Label of the form "Leaf\n".

Source code in imote/nodes/leaf_node.py
36
37
38
39
40
41
42
def get_label(self) -> str:
    """Returns the full display label for this node.

    Returns:
        Label of the form "Leaf\\n<node_model label>".
    """
    return "Leaf\n" + self.node_model.get_label()

get_minimal_label() -> str

Returns a short display label for this node.

Returns:

Type Description
str

The string "L".

Source code in imote/nodes/leaf_node.py
44
45
46
47
48
49
50
def get_minimal_label(self) -> str:
    """Returns a short display label for this node.

    Returns:
        The string "L".
    """
    return "L"

to_dict() -> dict

Serializes the node to a dictionary.

Returns:

Type Description
dict

Dictionary with base attributes plus the serialized node_model.

Source code in imote/nodes/leaf_node.py
52
53
54
55
56
57
58
59
60
def to_dict(self) -> dict:
    """Serializes the node to a dictionary.

    Returns:
        Dictionary with base attributes plus the serialized node_model.
    """
    node_dict = super().to_dict()
    node_dict["node_model"] = self.node_model.to_dict()
    return node_dict

from_dict(dic: dict) -> LeafNode classmethod

Reconstructs a LeafNode instance, including its model.

Parameters:

Name Type Description Default
dic dict

Dictionary previously produced by to_dict().

required

Returns:

Type Description
LeafNode

A new LeafNode instance with its node_model restored.

Source code in imote/nodes/leaf_node.py
62
63
64
65
66
67
68
69
70
71
72
73
74
@classmethod
def from_dict(cls, dic: dict) -> "LeafNode":
    """Reconstructs a LeafNode instance, including its model.

    Args:
        dic: Dictionary previously produced by to_dict().

    Returns:
        A new LeafNode instance with its node_model restored.
    """
    obj = super().from_dict(dic)
    obj.node_model = node_model_from_dict(dic["node_model"])
    return obj