From 01dae9185b19d4822e843ffe6273a8fb06f60f54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz?= Date: Mon, 31 May 2021 21:05:42 +0200 Subject: [PATCH] DecisionTree save and load from file --- createDecisionTree.py | 7 +++++-- Testfile.py => createTreeData.py | 2 +- main.py | 5 ++++- treedata/DecisionTree.pkl | Bin 0 -> 4110 bytes .../mydecisiontree.png | Bin 5 files changed, 10 insertions(+), 4 deletions(-) rename Testfile.py => createTreeData.py (99%) create mode 100644 treedata/DecisionTree.pkl rename mydecisiontree.png => treedata/mydecisiontree.png (100%) diff --git a/createDecisionTree.py b/createDecisionTree.py index 0cfddd3..537c75b 100644 --- a/createDecisionTree.py +++ b/createDecisionTree.py @@ -4,6 +4,7 @@ import pydotplus from sklearn.tree import DecisionTreeClassifier import matplotlib.pyplot as plt import matplotlib.image as pltimg +import joblib df = pandas.read_csv("treedata\\data.csv") @@ -38,12 +39,14 @@ y = df['Decision'] dtree = DecisionTreeClassifier() dtree = dtree.fit(X, y) +joblib.dump(dtree, 'treedata\\DecisionTree.pkl') + data = tree.export_graphviz(dtree, out_file=None, feature_names=features) graph = pydotplus.graph_from_dot_data(data) -graph.write_png('mydecisiontree.png') +graph.write_png('treedata\\mydecisiontree.png') -img = pltimg.imread('mydecisiontree.png') +img = pltimg.imread('treedata\\mydecisiontree.png') imgplot = plt.imshow(img) plt.show() diff --git a/Testfile.py b/createTreeData.py similarity index 99% rename from Testfile.py rename to createTreeData.py index bc9ed54..4b7d4a5 100644 --- a/Testfile.py +++ b/createTreeData.py @@ -119,4 +119,4 @@ filename = "treedata\\data.csv" with open(filename, 'w') as csvfile: writer = csv.DictWriter(csvfile, fieldnames=fields) writer.writeheader() - writer.writerows(dict) + writer.writerows(dict) \ No newline at end of file diff --git a/main.py b/main.py index bb67d7d..5125206 100644 --- a/main.py +++ b/main.py @@ -7,11 +7,11 @@ import WeatherConditions import convertToPrediction import drawUI import Graphsearch as graph -from decisiontree import dtree from Tractor import Tractor from TractorLoad import TillageUnit from constants import * from manualSteering import manualSteeringDriver +import joblib pygame.init() @@ -58,6 +58,9 @@ while working: weather, day_time, temperature, wind, humidy = WeatherConditions.checkConditions() question = [convertToPrediction.convert(field.state, weather, day_time, temperature, wind, humidy)] + + dtree = joblib.load('treedata\\DecisionTree.pkl') + decision = dtree.predict(question) if tractor.autodrive: diff --git a/treedata/DecisionTree.pkl b/treedata/DecisionTree.pkl new file mode 100644 index 0000000000000000000000000000000000000000..c48082d4a2fde99c9022517272a0a26a0c885ab8 GIT binary patch literal 4110 zcmdT{-D@0G6ratub2Wk1jxJG4d=bXk-PzpH+1YVE zT2mAvlC;8o5WGR~kMPNdKKiDJJ_vm%_|f>R4?amiQ18r~v)MZ#u=JterSvzy^L5WX z=iJHccHiD&j}k97?!RlBhUb(6&os+=%{F}B^f^1dX4Wj)W&(i?f{X+EyThLQ*yTfV07-@xER+-GK568*N&S?i)?lHhn!d zqGS~bxV&PFQ{ zebMubtv5Zxbvvt{^TTTF&sl%$#o2&UHzHfnBG&Iy!!<>Dcf@nct4A%#tcvG)uqEVl zip~nF9&3eva`z5@otLy;mbt;+kYU@cTY9r;xcqKd#p-9^Vebog zh)#ESpz%1Ta;@c=S=AOrFt5 zDkdx%Oc6-S(sRj3((|-PFKC$^PA}3KI!ov2f=C&6*(*WPS|&QOm|;4o4T!wOwCJ@~ z=mcDw7uAf1ClzzP9dnNLZanjBXW2gBl@c4W%|@WtsAbnV8}{PC1vUUPDsDqS#i-I& zWRt@=)Nsi<&&pe7^ERg}=IY6vaLJ!*K z!}y7Ofy=(=-N-tL+RQto#!wlg6Edw9;ja2z#moAC+4ND_rtm+ zenoNu_8S3|p^qedUwzzlkdS1HCQ$DvsPDA`N(mqHI)a2>^)A#gz zo`a)2iARz!Pu7!0s9$x;w;NsGqfa~ilq#77ejX6tiwWI_dOlERB)^xs-Vf(DabF_% z9)1eqrQh?oZ`!Zhd6D1C(_QD8_PYT2m5#~t_Ngzg{&xGmx(@mTZjfo6$5QM<-XbyXTM2Go1HPG{vIe*exB$4A;xoVtfW;J7 z(p*U`(4%w$IL1YPjEir2!@TjF7zXW7kMY_Z4IvKdk09D1f_*y9 zbRvJ-1ib>Q3Va&yGN1x$3HWphL9HAoUkCd|z?EZo5#-0Al3HkoaZ_pq^*CROvWIW4 cu*wQL{D9JLnqKsqp0n&?d1<-4#KUU&KPPf8&;S4c literal 0 HcmV?d00001 diff --git a/mydecisiontree.png b/treedata/mydecisiontree.png similarity index 100% rename from mydecisiontree.png rename to treedata/mydecisiontree.png