-
Notifications
You must be signed in to change notification settings - Fork 1
/
Node.py
60 lines (50 loc) · 1.94 KB
/
Node.py
1
2
3
4
5
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
from __future__ import annotations
class Node:
"""
A class used to represent a node in a decision tree.
Attributes:
feature (int): The index of the feature used for splitting the data.
threshold (float): The threshold value for the split at this node.
left (Node): The left child node resulting from the split.
right (Node): The right child node resulting from the split.
value (int, optional): The class value assigned to this node if it is a leaf node.
"""
def __init__(
self,
feature: int = None,
threshold: int = None,
left: Node = None,
right: Node = None,
*,
value: int = None,
) -> None:
"""
Initializes the Node with the provided feature, threshold, children, and value.
Parameters:
feature (int): The index of the feature used for splitting the data.
threshold (float): The threshold value for the split at this node.
left (Node): The left child node resulting from the split.
right (Node): The right child node resulting from the split.
value (int, optional): The class value assigned to this node if it is a leaf node.
"""
self.feature = feature
self.threshold = threshold
self.left = left
self.right = right
self.value = value
def is_leaf_node(self) -> bool:
"""
Determines if the node is a leaf node.
Returns:
bool: True if the node is a leaf node, False otherwise.
"""
return self.value is not None
def __repr__(self) -> str:
"""
Represents the Node as a string.
Returns:
str: A string representation of the Node.
"""
if self.is_leaf_node():
return f"Node(value={self.value})"
return f"Node(feature={self.feature}, threshold={self.threshold}, left={self.left}, right={self.right})"