Repository navigation
Expand file tree
/
Copy pathusage.py
More file actions
76 lines (63 loc) · 2.4 KB
/
Copy pathusage.py
File metadata and controls
76 lines (63 loc) · 2.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
"""
Usage script for DecisionTree.
Covers all 4 cases:
> real input, real output
> real input, discrete output
> discrete input, discrete output
> discrete input, real output
"""
import numpy as np
import pandas as pd
from tree.base import DecisionTree
from metrics import accuracy, precision, recall, rmse, mae
np.random.seed(42)
# Case 1: Real input, Real output (Regression)
print("\n Case 1: Real Input, Real Output ")
X = pd.DataFrame(np.random.randn(30, 5))
y = pd.Series(np.random.randn(30))
for crit in ["information_gain", "gini_index"]:
tree = DecisionTree(criterion=crit)
tree.fit(X, y)
y_hat = tree.predict(X)
tree.plot()
print(f"Criteria: {crit}")
print(f"RMSE: {rmse(y_hat, y):.3f}, MAE: {mae(y_hat, y):.3f}\n")
# Case 2: Real input, Discrete output (Classification)
print("\n Case 2: Real Input, Discrete Output")
X = pd.DataFrame(np.random.randn(30, 5))
y = pd.Series(np.random.randint(5, size=30), dtype="category")
for crit in ["information_gain", "gini_index"]:
tree = DecisionTree(criterion=crit)
tree.fit(X, y)
y_hat = tree.predict(X)
tree.plot()
print(f"Criteria: {crit}")
print("Accuracy:", accuracy(y_hat, y))
for cls in y.unique():
print(f"Class {cls}: Precision={precision(y_hat, y, cls):.3f}, Recall={recall(y_hat, y, cls):.3f}")
print()
# Case 3: Discrete input, Discrete output
print("\n Case 3: Discrete Input, Discrete Output")
X = pd.DataFrame({i: pd.Series(np.random.randint(5, size=30), dtype="category") for i in range(5)})
y = pd.Series(np.random.randint(5, size=30), dtype="category")
for crit in ["information_gain", "gini_index"]:
tree = DecisionTree(criterion=crit)
tree.fit(X, y)
y_hat = tree.predict(X)
tree.plot()
print(f"Criteria: {crit}")
print("Accuracy:", accuracy(y_hat, y))
for cls in y.unique():
print(f"Class {cls}: Precision={precision(y_hat, y, cls):.3f}, Recall={recall(y_hat, y, cls):.3f}")
print()
# Case 4: Discrete input, Real output (Regression)
print("\n Case 4: Discrete Input, Real Output")
X = pd.DataFrame({i: pd.Series(np.random.randint(5, size=30), dtype="category") for i in range(5)})
y = pd.Series(np.random.randn(30))
for crit in ["information_gain", "gini_index"]:
tree = DecisionTree(criterion=crit)
tree.fit(X, y)
y_hat = tree.predict(X)
tree.plot()
print(f"Criteria: {crit}")
print(f"RMSE: {rmse(y_hat, y):.3f}, MAE: {mae(y_hat, y):.3f}\n")