generowanie drzewa decyzyjnego

This commit is contained in:
Tomasz Adamczyk 2021-05-15 22:41:07 +02:00
parent 424794e53e
commit 7309c28361
7 changed files with 147 additions and 113 deletions

View File

@ -3,7 +3,8 @@
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="56453584-72bd-49f4-a39c-fccf91ab20c6" name="Default Changelist" comment=""> <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$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/tractor.py" beforeDir="false" afterPath="$PROJECT_DIR$/tractor.py" afterDir="false" /> <change beforePath="$PROJECT_DIR$/py.py" beforeDir="false" afterPath="$PROJECT_DIR$/py.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/resources/data.csv" beforeDir="false" afterPath="$PROJECT_DIR$/resources/data.csv" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -123,6 +124,13 @@
<workItem from="1619363880685" duration="158000" /> <workItem from="1619363880685" duration="158000" />
<workItem from="1621104529134" duration="1090000" /> <workItem from="1621104529134" duration="1090000" />
<workItem from="1621107567483" duration="160000" /> <workItem from="1621107567483" duration="160000" />
<workItem from="1621109381657" duration="180000" />
<workItem from="1621109567484" duration="55000" />
<workItem from="1621109625200" duration="178000" />
<workItem from="1621109806483" duration="833000" />
<workItem from="1621110649573" duration="32000" />
<workItem from="1621110688412" duration="148000" />
<workItem from="1621110850717" duration="377000" />
</task> </task>
<servers /> <servers />
</component> </component>
@ -130,26 +138,30 @@
<option name="version" value="1" /> <option name="version" value="1" />
</component> </component>
<component name="WindowStateProjectService"> <component name="WindowStateProjectService">
<state x="628" y="343" key="#com.intellij.fileTypes.FileTypeChooser" timestamp="1621109431051">
<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="1617994393023">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state x="690" y="287" key="#com.intellij.refactoring.safeDelete.UnsafeUsagesDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1617994393023" /> <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="1621107728689"> <state width="1879" height="295" key="GridCell.Tab.0.bottom" timestamp="1621111231927">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="295" key="GridCell.Tab.0.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1621107728689" /> <state width="1879" height="295" key="GridCell.Tab.0.bottom/0.0.1920.1080@0.0.1920.1080" timestamp="1621111231927" />
<state width="1879" height="295" key="GridCell.Tab.0.center" timestamp="1621107728689"> <state width="1879" height="295" key="GridCell.Tab.0.center" timestamp="1621111231927">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="295" key="GridCell.Tab.0.center/0.0.1920.1080@0.0.1920.1080" timestamp="1621107728689" /> <state width="1879" height="295" key="GridCell.Tab.0.center/0.0.1920.1080@0.0.1920.1080" timestamp="1621111231927" />
<state width="1879" height="295" key="GridCell.Tab.0.left" timestamp="1621107728689"> <state width="1879" height="295" key="GridCell.Tab.0.left" timestamp="1621111231927">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="295" key="GridCell.Tab.0.left/0.0.1920.1080@0.0.1920.1080" timestamp="1621107728689" /> <state width="1879" height="295" key="GridCell.Tab.0.left/0.0.1920.1080@0.0.1920.1080" timestamp="1621111231927" />
<state width="1879" height="295" key="GridCell.Tab.0.right" timestamp="1621107728689"> <state width="1879" height="295" key="GridCell.Tab.0.right" timestamp="1621111231927">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="295" key="GridCell.Tab.0.right/0.0.1920.1080@0.0.1920.1080" timestamp="1621107728689" /> <state width="1879" height="295" key="GridCell.Tab.0.right/0.0.1920.1080@0.0.1920.1080" timestamp="1621111231927" />
<state width="1879" height="364" key="GridCell.Tab.1.bottom" timestamp="1619289752045"> <state width="1879" height="364" key="GridCell.Tab.1.bottom" timestamp="1619289752045">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
@ -166,10 +178,10 @@
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state width="1879" height="364" key="GridCell.Tab.1.right/0.0.1920.1080@0.0.1920.1080" timestamp="1619289752045" /> <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="1621107567058"> <state x="0" y="0" key="com.intellij.ide.util.TipDialog" timestamp="1621110850962">
<screen x="0" y="0" width="1920" height="1080" /> <screen x="0" y="0" width="1920" height="1080" />
</state> </state>
<state x="0" y="0" key="com.intellij.ide.util.TipDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1621107567058" /> <state x="0" y="0" key="com.intellij.ide.util.TipDialog/0.0.1920.1080@0.0.1920.1080" timestamp="1621110850962" />
</component> </component>
<component name="XDebuggerManager"> <component name="XDebuggerManager">
<breakpoint-manager> <breakpoint-manager>
@ -183,6 +195,6 @@
</breakpoint-manager> </breakpoint-manager>
</component> </component>
<component name="com.intellij.coverage.CoverageDataManagerImpl"> <component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/SmartTractor$py.coverage" NAME="py Coverage Results" MODIFIED="1621107650440" 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="1621111175660" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="true" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$" />
</component> </component>
</project> </project>

Binary file not shown.

BIN
mydecisiontree.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

2
py.py
View File

@ -5,6 +5,7 @@ import map
import plant import plant
import pygame import pygame
import station import station
import treelearn
import tractor import tractor
pygame.display.set_caption("Smart Tractor") pygame.display.set_caption("Smart Tractor")
def main(): def main():
@ -19,6 +20,7 @@ def main():
tractor1 = tractor.Tractor(amount_of_seeds_dict, collected_plants_dict, definitions.TRACTOR_DIRECTION_NORTH, fertilizer_dict, definitions.TRACTOR_FUEL, definitions.TRACTOR_WATER_LEVEL, 0, 0) tractor1 = tractor.Tractor(amount_of_seeds_dict, collected_plants_dict, definitions.TRACTOR_DIRECTION_NORTH, fertilizer_dict, definitions.TRACTOR_FUEL, definitions.TRACTOR_WATER_LEVEL, 0, 0)
tractor1_rect = pygame.Rect(tractor1.get_x(), tractor1.get_y(), definitions.BLOCK_SIZE, definitions.BLOCK_SIZE) tractor1_rect = pygame.Rect(tractor1.get_x(), tractor1.get_y(), definitions.BLOCK_SIZE, definitions.BLOCK_SIZE)
clock = pygame.time.Clock() clock = pygame.time.Clock()
treelearn.treelearn()
run = True run = True
while run: #pętla główna programu while run: #pętla główna programu
clock.tick(definitions.FPS) clock.tick(definitions.FPS)

View File

@ -1,101 +1,101 @@
#;amount of seeds;collected plants;fertilizer;fuel;water level;back to station amount of seeds,collected plants,fertilizer,fuel,water level,back to station
1;10;79;8;129;12;1 10,79,8,129,12,1
2;39;195;8;125;1;1 39,195,8,125,1,1
3;68;5;5;2;3;1 68,5,5,2,3,1
4;30;70;4;162;24;0 30,70,4,162,24,0
5;49;121;0;158;37;0 49,121,0,158,37,0
6;62;139;4;112;9;0 62,139,4,112,9,0
7;24;154;4;138;14;1 24,154,4,138,14,1
8;45;151;3;68;4;1 45,151,3,68,4,1
9;44;96;0;2;30;1 44,96,0,2,30,1
10;78;17;6;146;24;0 78,17,6,146,24,0
11;38;170;3;91;9;1 38,170,3,91,9,1
12;40;85;3;40;4;1 40,85,3,40,4,1
13;47;38;0;54;32;0 47,38,0,54,32,0
14;36;179;6;77;31;1 36,179,6,77,31,1
15;3;22;6;73;23;1 3,22,6,73,23,1
16;4;46;3;160;17;1 4,46,3,160,17,1
17;48;27;8;43;0;1 48,27,8,43,0,1
18;9;16;1;164;12;1 9,16,1,164,12,1
19;9;14;0;19;3;1 9,14,0,19,3,1
20;48;126;0;68;21;0 48,126,0,68,21,0
21;35;185;7;81;3;1 35,185,7,81,3,1
22;75;21;8;153;6;1 75,21,8,153,6,1
23;32;136;5;91;23;0 32,136,5,91,23,0
24;47;167;7;123;17;0 47,167,7,123,17,0
25;76;32;3;114;35;0 76,32,3,114,35,0
26;80;60;6;5;0;1 80,60,6,5,0,1
27;40;15;1;50;24;0 40,15,1,50,24,0
28;62;3;6;6;21;1 62,3,6,6,21,1
29;10;159;8;199;24;1 10,159,8,199,24,1
30;8;149;2;49;1;1 8,149,2,49,1,1
31;76;55;1;45;3;1 76,55,1,45,3,1
32;3;29;5;111;8;1 3,29,5,111,8,1
33;15;14;7;118;7;1 15,14,7,118,7,1
34;56;112;4;87;30;0 56,112,4,87,30,0
35;1;168;4;40;27;1 1,168,4,40,27,1
36;65;149;4;40;34;0 65,149,4,40,34,0
37;21;190;4;82;17;1 21,190,4,82,17,1
38;66;83;1;155;6;1 66,83,1,155,6,1
39;19;165;7;3;2;1 19,165,7,3,2,1
40;38;92;3;2;16;1 38,92,3,2,16,1
41;18;141;0;6;11;1 18,141,0,6,11,1
42;30;192;6;85;20;1 30,192,6,85,20,1
43;54;30;0;172;30;0 54,30,0,172,30,0
44;66;25;0;79;4;1 66,25,0,79,4,1
45;43;142;1;29;9;1 43,142,1,29,9,1
46;0;48;0;58;32;1 0,48,0,58,32,1
47;6;105;1;83;13;1 6,105,1,83,13,1
48;22;175;7;117;24;1 22,175,7,117,24,1
49;31;95;3;146;30;0 31,95,3,146,30,0
50;72;64;0;33;23;0 72,64,0,33,23,0
51;41;113;87;31;31;0 41,113,87,31,31,0
52;45;54;4;87;9;1 45,54,4,87,9,1
53;39;107;5;200;1;1 39,107,5,200,1,1
54;38;167;6;112;32;0 38,167,6,112,32,0
55;63;103;0;56;18;0 63,103,0,56,18,0
56;73;66;8;36;10;1 73,66,8,36,10,1
57;3;67;1;174;10;1 3,67,1,174,10,1
58;59;182;8;4;12;1 59,182,8,4,12,1
59;72;24;4;60;17;0 72,24,4,60,17,0
60;26;3;4;67;35;0 26,3,4,67,35,0
61;66;153;5;19;9;1 66,153,5,19,9,1
62;74;155;5;86;13;1 74,155,5,86,13,1
63;75;71;8;179;9;1 75,71,8,179,9,1
64;67;101;3;65;38;0 67,101,3,65,38,0
65;46;192;6;39;4;1 46,192,6,39,4,1
66;58;109;7;11;3;1 58,109,7,11,3,1
67;25;30;0;156;37;0 25,30,0,156,37,0
68;25;115;1;150;7;1 25,115,1,150,7,1
69;38;195;3;185;12;1 38,195,3,185,12,1
70;39;41;5;168;32;0 39,41,5,168,32,0
71;45;163;1;98;32;1 45,163,1,98,32,1
72;18;107;0;150;39;0 18,107,0,150,39,0
73;72;131;1;188;6;1 72,131,1,188,6,1
74;64;134;3;121;28;0 64,134,3,121,28,0
75;76;193;5;181;35;1 76,193,5,181,35,1
76;42;151;4;8;40;1 42,151,4,8,40,1
77;32;95;1;185;40;0 32,95,1,185,40,0
78;31;106;4;30;14;0 31,106,4,30,14,0
79;75;37;7;175;0;1 75,37,7,175,0,1
80;58;70;0;163;36;0 58,70,0,163,36,0
81;74;112;6;133;7;1 74,112,6,133,7,1
82;46;72;0;175;35;0 46,72,0,175,35,0
83;42;68;6;188;3;1 42,68,6,188,3,1
84;1;127;5;16;13;1 1,127,5,16,13,1
85;16;38;8;92;15;0 16,38,8,92,15,0
86;22;82;3;69;29;0 22,82,3,69,29,0
87;17;68;4;0;5;1 17,68,4,0,5,1
88;22;172;4;11;17;1 22,172,4,11,17,1
89;14;115;5;67;20;0 14,115,5,67,20,0
90;18;29;7;135;20;0 18,29,7,135,20,0
91;52;140;7;20;39;1 52,140,7,20,39,1
92;0;139;5;49;35;1 0,139,5,49,35,1
93;31;170;6;93;26;1 31,170,6,93,26,1
94;56;78;4;47;10;1 56,78,4,47,10,1
95;71;6;1;116;3;1 71,6,1,116,3,1
96;11;61;3;39;27;0 11,61,3,39,27,0
97;73;152;8;126;1;1 73,152,8,126,1,1
98;21;57;4;0;29;1 21,57,4,0,29,1
99;68;6;3;170;38;0 68,6,3,170,38,0
100;13;38;0;50;34;0 13,38,0,50,34,0
1 # amount of seeds collected plants fertilizer fuel water level back to station
2 1 10 79 8 129 12 1
3 2 39 195 8 125 1 1
4 3 68 5 5 2 3 1
5 4 30 70 4 162 24 0
6 5 49 121 0 158 37 0
7 6 62 139 4 112 9 0
8 7 24 154 4 138 14 1
9 8 45 151 3 68 4 1
10 9 44 96 0 2 30 1
11 10 78 17 6 146 24 0
12 11 38 170 3 91 9 1
13 12 40 85 3 40 4 1
14 13 47 38 0 54 32 0
15 14 36 179 6 77 31 1
16 15 3 22 6 73 23 1
17 16 4 46 3 160 17 1
18 17 48 27 8 43 0 1
19 18 9 16 1 164 12 1
20 19 9 14 0 19 3 1
21 20 48 126 0 68 21 0
22 21 35 185 7 81 3 1
23 22 75 21 8 153 6 1
24 23 32 136 5 91 23 0
25 24 47 167 7 123 17 0
26 25 76 32 3 114 35 0
27 26 80 60 6 5 0 1
28 27 40 15 1 50 24 0
29 28 62 3 6 6 21 1
30 29 10 159 8 199 24 1
31 30 8 149 2 49 1 1
32 31 76 55 1 45 3 1
33 32 3 29 5 111 8 1
34 33 15 14 7 118 7 1
35 34 56 112 4 87 30 0
36 35 1 168 4 40 27 1
37 36 65 149 4 40 34 0
38 37 21 190 4 82 17 1
39 38 66 83 1 155 6 1
40 39 19 165 7 3 2 1
41 40 38 92 3 2 16 1
42 41 18 141 0 6 11 1
43 42 30 192 6 85 20 1
44 43 54 30 0 172 30 0
45 44 66 25 0 79 4 1
46 45 43 142 1 29 9 1
47 46 0 48 0 58 32 1
48 47 6 105 1 83 13 1
49 48 22 175 7 117 24 1
50 49 31 95 3 146 30 0
51 50 72 64 0 33 23 0
52 51 41 113 87 31 31 0
53 52 45 54 4 87 9 1
54 53 39 107 5 200 1 1
55 54 38 167 6 112 32 0
56 55 63 103 0 56 18 0
57 56 73 66 8 36 10 1
58 57 3 67 1 174 10 1
59 58 59 182 8 4 12 1
60 59 72 24 4 60 17 0
61 60 26 3 4 67 35 0
62 61 66 153 5 19 9 1
63 62 74 155 5 86 13 1
64 63 75 71 8 179 9 1
65 64 67 101 3 65 38 0
66 65 46 192 6 39 4 1
67 66 58 109 7 11 3 1
68 67 25 30 0 156 37 0
69 68 25 115 1 150 7 1
70 69 38 195 3 185 12 1
71 70 39 41 5 168 32 0
72 71 45 163 1 98 32 1
73 72 18 107 0 150 39 0
74 73 72 131 1 188 6 1
75 74 64 134 3 121 28 0
76 75 76 193 5 181 35 1
77 76 42 151 4 8 40 1
78 77 32 95 1 185 40 0
79 78 31 106 4 30 14 0
80 79 75 37 7 175 0 1
81 80 58 70 0 163 36 0
82 81 74 112 6 133 7 1
83 82 46 72 0 175 35 0
84 83 42 68 6 188 3 1
85 84 1 127 5 16 13 1
86 85 16 38 8 92 15 0
87 86 22 82 3 69 29 0
88 87 17 68 4 0 5 1
89 88 22 172 4 11 17 1
90 89 14 115 5 67 20 0
91 90 18 29 7 135 20 0
92 91 52 140 7 20 39 1
93 92 0 139 5 49 35 1
94 93 31 170 6 93 26 1
95 94 56 78 4 47 10 1
96 95 71 6 1 116 3 1
97 96 11 61 3 39 27 0
98 97 73 152 8 126 1 1
99 98 21 57 4 0 29 1
100 99 68 6 3 170 38 0
101 100 13 38 0 50 34 0

Binary file not shown.

After

Width:  |  Height:  |  Size: 67 KiB

20
treelearn.py Normal file
View File

@ -0,0 +1,20 @@
import matplotlib.image as pltimg
import matplotlib.pyplot as plt
import os
import pandas
import pydotplus
from sklearn import tree
from sklearn.tree import DecisionTreeClassifier
def treelearn():
df = pandas.read_csv(os.path.join('resources', 'data.csv'))
features = ['amount of seeds', 'collected plants', 'fertilizer', 'fuel', 'water level']
x = df[features]
y = df['back to station']
dtree = DecisionTreeClassifier()
dtree = dtree.fit(x, y)
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('mydecisiontree.png')
imgplot = plt.imshow(img)
plt.show()