-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
/
Copy pathrandom_forest_classifier.py
85 lines (64 loc) · 2.18 KB
/
random_forest_classifier.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
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
import numpy as np
from typing import Optional
class DecisionTree:
"""
Decision Tree classifier.
Parameters:
max_depth (Optional[int]): Maximum depth of the tree. If None, the tree grows until pure nodes or min_samples_split is reached.
Attributes:
tree (tuple): The decision tree structure.
"""
def __init__(self, max_depth: Optional[int] = None) -> None:
self.max_depth = max_depth
def fit(self, features, labels) -> None:
"""
Fit the decision tree to the training data.
Parameters:
features: The input features.
labels: The target labels.
Returns:
None
"""
self.tree = self._build_tree(features, labels, depth=0)
def _build_tree(self, features, labels, depth) -> tuple:
"""
Recursively build the decision tree.
Parameters:
features: The input features.
labels: The target labels.
depth: The current depth of the tree.
Returns:
tuple: The decision tree structure.
"""
# Your existing _build_tree implementation
def _calculate_gini(self, labels) -> float:
"""
Calculate the Gini impurity for a given set of labels.
Parameters:
labels: A list of labels.
Returns:
float: The Gini impurity.
"""
# Your existing _calculate_gini implementation
def predict(self, features) -> list:
"""
Make predictions for input features.
Parameters:
features: The input features.
Returns:
list: Predicted labels.
"""
return [self._predict_tree(data_point, self.tree) for data_point in features]
def _predict_tree(self, data_point, tree) -> int:
"""
Recursively traverse the decision tree to make predictions.
Parameters:
data_point: Input features for a single data point.
tree: The decision tree structure.
Returns:
int: Predicted label.
"""
# Your existing _predict_tree implementation
if __name__ == "__main__":
import doctest
doctest.testmod()