-
Notifications
You must be signed in to change notification settings - Fork 1
/
usage.py
115 lines (99 loc) · 3.17 KB
/
usage.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
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
"""
The current code given is for the Assignment 1.
You will be expected to use this to make trees for:
> discrete input, discrete output
> real input, real output
> real input, discrete output
> discrete input, real output
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tree.base import DecisionTree
from metrics import *
np.random.seed(4)
# Test case 1
# Real Input and Real Output
print("-----------------Test case 1-----------------")
print("Real Input and Real Output")
N = 30
P = 5
X = pd.DataFrame(np.random.randn(N, P))
# print(X)
# X.columns = ["A", "B", "C","D","E"]
y = pd.Series(np.random.randn(N))
# X = pd.DataFrame({
# 'A': [1, 1, 3, 0],
# 'B': [1, 2, 3, 4]
# })
# y = pd.Series([1, 1, 0, 0])
# features = pd.Series(['A', 'B'])
for criteria in ["information_gain", "gini_index"]:
tree = DecisionTree(criterion=criteria) # Split based on Inf. Gain
tree.fit(X, y)
y_hat = tree.predict(X)
y_hat = pd.Series(y_hat)
tree.plot()
tree.plotGraph()
print(f"Criteria :, {criteria}")
print(f"RMSE: {rmse(y_hat, y)}")
print(f"MAE: {mae(y_hat, y)}")
print('Done')
# Test case 2
# Real Input and Discrete Output
print("-----------------Test case 2-----------------")
print("Real Input and Discrete Output")
N = 30
P = 5
X = pd.DataFrame(np.random.randn(N, P))
y = pd.Series(np.random.randint(P, size=N), dtype="category")
for criteria in ["information_gain", "gini_index"]:
tree = DecisionTree(criterion=criteria) # Split based on Inf. Gain
tree.fit(X, y)
y_hat = tree.predict(X)
tree.plot()
tree.plotGraph()
print(f"Criteria : {criteria}")
print(f"Accuracy: {accuracy(y_hat, y)}")
for cls in y.unique():
print(f"Precision:{precision(y_hat, y, cls)}")
print(f"Recall: {recall(y_hat, y, cls)}")
print('Done')
# Test case 3
# Discrete Input and Discrete .Output
print("-----------------Test case 3-----------------")
print("Discrete Input and Discrete Output")
N = 30
P = 5
X = pd.DataFrame({i: pd.Series(np.random.randint(P, size=N), dtype="category") for i in range(5)})
y = pd.Series(np.random.randint(P, size=N), dtype="category")
for criteria in ["information_gain", "gini_index"]:
tree = DecisionTree(criterion=criteria) # Split based on Inf. Gain
tree.fit(X, y)
y_hat = tree.predict(X)
tree.plot()
tree.plotGraph()
print(f"Criteria :, {criteria}")
print(f"Accuracy: , {accuracy(y_hat, y)}")
for cls in y.unique():
print(f"Precision:, {precision(y_hat, y, cls)}")
print(f"Recall: , {recall(y_hat, y, cls)}")
print('Done')
# Test case 4
# Discrete Input and Real Output
print("-----------------Test case 4-----------------")
print("Discrete Input and Real Output")
N = 30
P = 5
X = pd.DataFrame({i: pd.Series(np.random.randint(P, size=N), dtype="category") for i in range(5)})
y = pd.Series(np.random.randn(N))
for criteria in ["information_gain", "gini_index"]:
tree = DecisionTree(criterion=criteria) # Split based on Inf. Gain
tree.fit(X, y)
y_hat = tree.predict(X)
tree.plot()
tree.plotGraph()
print(f"Criteria : {criteria}")
print(f"RMSE: {rmse(y_hat, y)}")
print(f"MAE: {mae(y_hat, y)}")
print('Done')