-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecision Tree.py
More file actions
152 lines (109 loc) · 4.4 KB
/
Copy pathDecision Tree.py
File metadata and controls
152 lines (109 loc) · 4.4 KB
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
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
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
class Node:
def __init__(self, feature=None, threshold=None, left=None, right=None, value=None):
self.feature = feature
self.threshold = threshold
self.left = left
self.right = right
self.value = value
def is_leaf_node(self):
return self.value is not None
class DecisionTree:
def __init__(self, max_depth=10, min_samples_split=2):
self.max_depth = max_depth
self.min_samples_split = min_samples_split
self.root = None
def fit(self, X, y):
self.root = self._build_tree(X, y)
def _build_tree(self, X, y, depth=0):
n_samples, n_features = X.shape
n_labels = len(np.unique(y))
if (depth >= self.max_depth or
n_labels == 1 or
n_samples < self.min_samples_split):
most_common_label = np.bincount(y).argmax()
return Node(value=most_common_label)
best_feat, best_thresh = self._best_split(X, y, n_features)
left_idx = X[:, best_feat] < best_thresh
right_idx = ~left_idx
left_child = self._build_tree(X[left_idx], y[left_idx], depth + 1)
right_child = self._build_tree(X[right_idx], y[right_idx], depth + 1)
return Node(feature=best_feat, threshold=best_thresh, left=left_child, right=right_child)
def _best_split(self, X, y, n_features):
best_gain = -1
split_idx, split_thresh = None, None
for feat_idx in range(n_features):
X_column = X[:, feat_idx]
thresholds = np.unique(X_column)
for threshold in thresholds:
gain = self._information_gain(y, X_column, threshold)
if gain > best_gain:
best_gain = gain
split_idx = feat_idx
split_thresh = threshold
return split_idx, split_thresh
def _information_gain(self, y, X_column, threshold):
parent_entropy = self._entropy(y)
left_idx = X_column < threshold
right_idx = ~left_idx
if len(y[left_idx]) == 0 or len(y[right_idx]) == 0:
return 0
n = len(y)
n_l, n_r = len(y[left_idx]), len(y[right_idx])
e_l, e_r = self._entropy(y[left_idx]), self._entropy(y[right_idx])
child_entropy = (n_l / n) * e_l + (n_r / n) * e_r
return parent_entropy - child_entropy
def _entropy(self, y):
hist = np.bincount(y)
ps = hist / len(y)
return -np.sum([p * np.log2(p) for p in ps if p > 0])
def predict(self, X):
return np.array([self._traverse_tree(x, self.root) for x in X])
def _traverse_tree(self, x, node):
if node.is_leaf_node():
return node.value
if x[node.feature] < node.threshold:
return self._traverse_tree(x, node.left)
return self._traverse_tree(x, node.right)
iris = load_iris()
X = iris.data[:, [2, 3]]
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = DecisionTree(max_depth=3)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
accuracy = np.sum(y_pred == y_test) / len(y_test)
print(f"Custom Tree Accuracy (2 Features): {accuracy * 100:.2f}%")
plt.figure(figsize=(10, 7))
x_min, x_max = X[:, 0].min() - 0.5, X[:, 0].max() + 0.5
y_min, y_max = X[:, 1].min() - 0.5, X[:, 1].max() + 0.5
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02),
np.arange(y_min, y_max, 0.02))
Z = model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, alpha=0.2, colors=['#FF5733', '#33FF57', '#3357FF'])
colors = ['#FF5733', '#33FF57', '#3357FF']
markers = ['o', 's', '^']
target_names = iris.target_names
feature_names = iris.feature_names
for i, class_name in enumerate(target_names):
mask = (y == i)
plt.scatter(
X[mask, 0],
X[mask, 1],
c=colors[i],
marker=markers[i],
label=class_name,
edgecolor='k',
s=60,
alpha=0.8
)
plt.xlabel(feature_names[2].title(), fontsize=12)
plt.ylabel(feature_names[3].title(), fontsize=12)
plt.title("Iris Decision Tree Classifier Boundaries", fontsize=14, fontweight='bold')
plt.legend(title="Species", loc="upper left")
plt.grid(True, linestyle='--', alpha=0.4)
plt.show()