czytanie/odczytywanie drzewa z/do pliku za pomocą pickle

This commit is contained in:
Tomasz Adamczyk 2021-05-19 15:04:17 +02:00
parent 4b73064f4e
commit 7cabeb6cbe
7 changed files with 35 additions and 26 deletions

View File

@ -3,7 +3,7 @@
<component name="ChangeListManager">
<list default="true" id="56453584-72bd-49f4-a39c-fccf91ab20c6" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/py.py" beforeDir="false" afterPath="$PROJECT_DIR$/py.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/resources/mydecisiontree.png" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/treelearn.py" beforeDir="false" afterPath="$PROJECT_DIR$/treelearn.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
@ -133,6 +133,10 @@
<workItem from="1621110850717" duration="377000" />
<workItem from="1621111315400" duration="72000" />
<workItem from="1621171225695" duration="880000" />
<workItem from="1621195639702" duration="147000" />
<workItem from="1621247003154" duration="155000" />
<workItem from="1621259807468" duration="619000" />
<workItem from="1621428139271" duration="1235000" />
</task>
<servers />
</component>
@ -144,26 +148,26 @@
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state x="628" y="343" key="#com.intellij.fileTypes.FileTypeChooser/0.0.1920.1080@0.0.1920.1080" timestamp="1621109431051" />
<state x="690" y="287" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog" timestamp="1617994393023">
<state x="690" y="287" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog" timestamp="1621429221570">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state x="690" y="287" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1617994393023" />
<state width="1879" height="295" key="GridCell.Tab.0.bottom" timestamp="1621172112876">
<state x="690" y="287" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1621429221570" />
<state width="1879" height="295" key="GridCell.Tab.0.bottom" timestamp="1621429389327">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="295" key="GridCell.Tab.0.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1621172112876" />
<state width="1879" height="295" key="GridCell.Tab.0.center" timestamp="1621172112876">
<state width="1879" height="295" key="GridCell.Tab.0.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1621429389327" />
<state width="1879" height="295" key="GridCell.Tab.0.center" timestamp="1621429389327">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="295" key="GridCell.Tab.0.center/0.0.1920.1080@0.0.1920.1080" timestamp="1621172112876" />
<state width="1879" height="295" key="GridCell.Tab.0.left" timestamp="1621172112876">
<state width="1879" height="295" key="GridCell.Tab.0.center/0.0.1920.1080@0.0.1920.1080" timestamp="1621429389327" />
<state width="1879" height="295" key="GridCell.Tab.0.left" timestamp="1621429389326">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="295" key="GridCell.Tab.0.left/0.0.1920.1080@0.0.1920.1080" timestamp="1621172112876" />
<state width="1879" height="295" key="GridCell.Tab.0.right" timestamp="1621172112876">
<state width="1879" height="295" key="GridCell.Tab.0.left/0.0.1920.1080@0.0.1920.1080" timestamp="1621429389326" />
<state width="1879" height="295" key="GridCell.Tab.0.right" timestamp="1621429389327">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="295" key="GridCell.Tab.0.right/0.0.1920.1080@0.0.1920.1080" timestamp="1621172112876" />
<state width="1879" height="295" key="GridCell.Tab.0.right/0.0.1920.1080@0.0.1920.1080" timestamp="1621429389327" />
<state width="1879" height="364" key="GridCell.Tab.1.bottom" timestamp="1619289752045">
<screen x="0" y="0" width="1920" height="1080" />
</state>
@ -180,10 +184,10 @@
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state width="1879" height="364" key="GridCell.Tab.1.right/0.0.1920.1080@0.0.1920.1080" timestamp="1619289752045" />
<state x="0" y="0" key="com.intellij.ide.util.TipDialog" timestamp="1621171227060">
<state x="0" y="0" key="com.intellij.ide.util.TipDialog" timestamp="1621428140628">
<screen x="0" y="0" width="1920" height="1080" />
</state>
<state x="0" y="0" key="com.intellij.ide.util.TipDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1621171227060" />
<state x="0" y="0" key="com.intellij.ide.util.TipDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1621428140628" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
@ -197,6 +201,6 @@
</breakpoint-manager>
</component>
<component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/SmartTractor$py.coverage" NAME="py Coverage Results" MODIFIED="1621172058000" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
<SUITE FILE_PATH="coverage/SmartTractor$py.coverage" NAME="py Coverage Results" MODIFIED="1621429250145" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
</component>
</project>

Binary file not shown.

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 67 KiB

After

Width:  |  Height:  |  Size: 67 KiB

BIN
resources/tree.pkl Normal file

Binary file not shown.

View File

@ -2,22 +2,27 @@ import matplotlib.image as pltimg
import matplotlib.pyplot as plt
import os
import pandas
import pickle
import pydotplus
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
def treelearn(): #zwraca utworzone drzewo decyzyjne
df = pandas.read_csv(os.path.join('resources', 'data.csv')) #czytanie danych do nauki drzewa z pliku .csv
features = ['amount of seeds', 'collected plants', 'fertilizer', 'fuel', 'water level']
x = df[features] #wczytanie atrybutów, z których ma się uczyć drzewo
y = df['back to station'] #podjęte decyzje
dtree = DecisionTreeClassifier() #tworzy obiekt drzewa
dtree = dtree.fit(x, y) #uczy drzewo
data = tree.export_graphviz(dtree, out_file=None, feature_names=features)
graph = pydotplus.graph_from_dot_data(data)
graph.write_png(os.path.join('resources', 'mydecisiontree.png'))
img = pltimg.imread(os.path.join('resources', 'mydecisiontree.png'))
imgplot = plt.imshow(img)
plt.show() #wyświetl drzewo decyzyjne
if os.path.exists("resources/tree.pkl"): #jeżeli drzewo jest zapisane w pliku to odczytaj
dtree = pickle.load(open(os.path.join('resources', "tree.pkl"), "rb"))
else: #w przeciwnym razie utwórz drzewo od początku i zapisz do pliku
df = pandas.read_csv(os.path.join('resources', 'data.csv')) #czytanie danych do nauki drzewa z pliku .csv
features = ['amount of seeds', 'collected plants', 'fertilizer', 'fuel', 'water level']
x = df[features] #wczytanie atrybutów, z których ma się uczyć drzewo
y = df['back to station'] #podjęte decyzje
dtree = DecisionTreeClassifier() #klasyfikuje drzewo
dtree = dtree.fit(x, y) #uczy drzewo
pickle.dump(dtree, open(os.path.join('resources', "tree.pkl"), "wb"))
data = tree.export_graphviz(dtree, out_file=None, feature_names=features)
graph = pydotplus.graph_from_dot_data(data)
graph.write_png(os.path.join('resources', 'mytree.png'))
img = pltimg.imread(os.path.join('resources', 'mytree.png'))
imgplot = plt.imshow(img)
plt.show() #wyświetl drzewo decyzyjne
return dtree
def make_decision(tree, amount_of_seeds, collected_plants, fertilizer, fuel, water_level): #zwraca decyzję o powrocie do stacji (0 : NIE, 1 : TAK)
decision = tree.predict([[amount_of_seeds, collected_plants, fertilizer, fuel, water_level]]) #podejmij decyzję na podstawie aktualnych parametrów traktora o powrocie do stacji lub nie