-
Notifications
You must be signed in to change notification settings - Fork 1
/
train_decision_tree.py
51 lines (40 loc) · 1.17 KB
/
train_decision_tree.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
import pickle
import ipdb
import numpy as np
import torch
from hummingbird.ml import convert
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier as De
from sklearn.tree import _tree
from data import HeartFailureDataset
# custom collate_fn
def collate_fn(data):
feats = []
labels = []
for e in data:
feats.append(e["feat"])
labels.append(e["label"])
return torch.tensor(feats), torch.tensor(labels)
if __name__ == "__main__":
"""Get dataset"""
batch_size = 16
x_train, x_test, y_train, y_test = HeartFailureDataset(split="train").get_data()
"""Get Train Configurations"""
# todo: fix scikit-learn seed
# torch.manual_seed(17)
clr = De(max_depth=3)
clr.fit(x_train, y_train)
circuit = convert(clr, "torch", x_test[:1]).model
"""Evaluation"""
total = len(y_test)
correct = 0
pred = clr.predict(x_test)
correct += np.sum(pred == y_test).item()
print("\n")
print(
f"Total: {total}, Correct: {correct}, Accuracy: {round(correct/total*100, 2)}"
)
"""Checkpointing"""
PATH = f"./data/decision_tree.pkl"
with open(PATH, "wb") as f:
pickle.dump(clr, f)