Trashmaster/decision_tree/decisionTree.py

59 lines
1.4 KiB
Python
Raw Normal View History

2022-05-09 19:18:24 +02:00
import joblib
import matplotlib.pyplot as plt
import pandas
from sklearn.tree import DecisionTreeClassifier, export_text, plot_tree
decisions = ["decision"]
attributes = ["season", "trash_type", "mass", "space", "trash_mass"]
2022-05-09 19:18:24 +02:00
# return tree made from attributes
def tree():
2022-05-13 12:47:08 +02:00
dataset = pandas.read_csv('decision_tree/tree_dataset.csv', sep=";")
2022-05-09 19:18:24 +02:00
x = dataset[attributes]
y = dataset[decisions]
decision_tree = DecisionTreeClassifier()
decision_tree = decision_tree.fit(x.values, y.values)
2022-05-09 19:18:24 +02:00
return decision_tree
# return decision made from tree and attributes
def decision(decision_tree, season, trash_type, mass, space, trash_mass):
2022-05-09 19:18:24 +02:00
decision = decision_tree.predict(
[[season, trash_type , mass, space, trash_mass]])
2022-05-09 19:18:24 +02:00
return decision
'''
we shall save output of our decision tree. It is possible for a few ways:
txt, png or structure
'''
def tree_as_txt(decision_tree):
with open('./decision_tree/tree_as_txt.txt', "w") as file:
file.write(export_text(decision_tree))
def tree_to_png(decision_tree):
plt.figure()
plot_tree(decision_tree, feature_names=attributes, filled=True)
plt.title("Decision tree")
plt.show()
def tree_to_structure(decision_tree):
joblib.dump(decision_tree, './decision_tree/tree_model')
2022-05-12 09:39:25 +02:00
def tree_from_structure(file):
return joblib.load(file)
2022-05-09 19:18:24 +02:00
#drzewo = tree()
#tree_as_txt(drzewo)
#tree_to_png(drzewo)
#tree_to_structure(drzewo)