-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusage.py
92 lines (80 loc) · 2.74 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
"""
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(42)
# Test case 1
# Real Input and Real Output
print("################### RIRO ###################")
N = 30
P = 5
X = pd.DataFrame(np.random.randn(N, P))
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()
print('Criteria :', criteria)
print('RMSE: ', rmse(y_hat, y))
print('MAE: ', mae(y_hat, y))
# Test case 2
# Real Input and Discrete Output
print("################### RIDO ###################")
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']:
X = pd.DataFrame(np.random.randn(N, P))
y = pd.Series(np.random.randint(P, size = N), dtype="category")
tree = DecisionTree(criterion=criteria) #Split based on Inf. Gain
tree.fit(X, y)
y_hat = tree.predict(X)
tree.plot()
print('Criteria :', criteria)
print('Accuracy: ', accuracy(y_hat, y))
for cls in y.unique():
print('Precision: ', precision(y_hat, y, cls))
print('Recall: ', recall(y_hat, y, cls))
# Test case 3
# Discrete Input and Discrete Output
print("################### DIDO ###################")
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()
print('Criteria :', criteria)
print('Accuracy: ', accuracy(y_hat, y))
for cls in y.unique():
print('Precision: ', precision(y_hat, y, cls))
print('Recall: ', recall(y_hat, y, cls))
# Test case 4
# Discrete Input and Real Output
print("################### DIRO ###################")
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()
print('Criteria :', criteria)
print('RMSE: ', rmse(y_hat, y))
print('MAE: ', mae(y_hat, y))