2021-05-23 13:38:16 +02:00
|
|
|
import os
|
|
|
|
import json
|
2021-05-26 13:47:02 +02:00
|
|
|
from matplotlib import pyplot
|
2021-05-23 13:38:16 +02:00
|
|
|
from joblib import dump, load
|
|
|
|
from sklearn import tree
|
|
|
|
from sklearn.feature_extraction import DictVectorizer
|
|
|
|
|
2021-05-23 20:20:02 +02:00
|
|
|
from objects.mines.disarming.mine_parameters import MineParameters
|
|
|
|
from objects.mines.disarming.parameter_json import generate_data
|
2021-05-23 13:38:16 +02:00
|
|
|
|
|
|
|
|
|
|
|
class DecisionTree:
|
|
|
|
def __init__(self, clf_source: str = None, vec_source: str = None):
|
|
|
|
if clf_source is not None and vec_source is not None:
|
|
|
|
self.load(clf_source, vec_source)
|
|
|
|
else:
|
|
|
|
self.clf = None
|
|
|
|
self.vec = None
|
|
|
|
|
|
|
|
def build(self, training_file: str, depth: int):
|
2021-06-06 19:00:20 +02:00
|
|
|
path = os.path.join("../..", "..", "resources", "data", "decision_tree", training_file)
|
2021-05-23 13:38:16 +02:00
|
|
|
|
|
|
|
samples = list()
|
|
|
|
results = list()
|
|
|
|
|
|
|
|
with open(path, "r") as training_file:
|
|
|
|
for sample in training_file:
|
|
|
|
s, r = self._process_input_line(sample)
|
|
|
|
samples.append(s)
|
|
|
|
results.append(r)
|
|
|
|
|
|
|
|
# vec transforms X (a list of dictionaries of string-string pairs) to binary arrays for tree to work on
|
|
|
|
self.vec = DictVectorizer()
|
|
|
|
|
|
|
|
# create and run Tree Clasifier upon provided data
|
|
|
|
self.clf = tree.DecisionTreeClassifier(max_depth=depth)
|
|
|
|
self.clf = self.clf.fit(self.vec.fit_transform(samples).toarray(), results)
|
|
|
|
|
|
|
|
# print a tree (not necessary)
|
|
|
|
print(tree.export_text(self.clf, feature_names=self.vec.get_feature_names()))
|
|
|
|
|
2021-05-26 13:47:02 +02:00
|
|
|
# plot a tree (not necessary)
|
2021-06-06 18:29:02 +02:00
|
|
|
# fig = pyplot.figure(figsize=(50, 40))
|
|
|
|
# _ = tree.plot_tree(self.clf,
|
|
|
|
# feature_names=self.vec.get_feature_names(),
|
|
|
|
# class_names=self.clf.classes_,
|
|
|
|
# filled=True)
|
|
|
|
# fig.savefig("decistion_tree.png")
|
2021-05-26 13:47:02 +02:00
|
|
|
|
2021-05-23 13:38:16 +02:00
|
|
|
def save(self):
|
|
|
|
dump(self.clf, 'decision_tree.joblib')
|
|
|
|
dump(self.vec, 'dict_vectorizer.joblib')
|
|
|
|
|
|
|
|
def load(self, clf_file, vec_file):
|
|
|
|
self.clf = load(clf_file)
|
|
|
|
self.vec = load(vec_file)
|
|
|
|
|
|
|
|
def get_answer(self, mine_params):
|
|
|
|
return self.clf.predict(self.vec.transform(mine_params).toarray())
|
|
|
|
|
|
|
|
def test(self):
|
|
|
|
mistakes = 0
|
|
|
|
for _ in range(1000):
|
|
|
|
mine_params = MineParameters().jsonifyable_dict()
|
|
|
|
correct = mine_params['wire']
|
|
|
|
del mine_params['wire']
|
|
|
|
|
|
|
|
answer = self.get_answer(mine_params)
|
|
|
|
|
|
|
|
if correct != answer:
|
|
|
|
print(f"Answer: {answer}\nCorrect: {correct}")
|
|
|
|
mistakes += 1
|
|
|
|
|
2021-05-23 20:20:02 +02:00
|
|
|
print(f"Accuracy: {100 - (mistakes / 10)}")
|
2021-05-23 13:38:16 +02:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def _process_input_line(line):
|
|
|
|
data = json.loads(line.strip())
|
|
|
|
result = data['wire']
|
|
|
|
del data['wire']
|
|
|
|
sample = data
|
|
|
|
|
|
|
|
return sample, result
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2021-05-24 01:05:00 +02:00
|
|
|
# generate_data("training_set.txt", 12000)
|
2021-05-23 13:38:16 +02:00
|
|
|
decision_tree = DecisionTree()
|
2021-05-23 20:20:02 +02:00
|
|
|
decision_tree.build("training_set.txt", 15)
|
2021-05-23 13:38:16 +02:00
|
|
|
decision_tree.test()
|
2021-06-05 00:02:14 +02:00
|
|
|
decision_tree.save()
|