suggestionDecisionTree implementation

This commit is contained in:
Marcin Dobrowolski 2020-05-27 09:13:23 +02:00
parent 2e114bc0ee
commit a7006c1424
35 changed files with 782 additions and 3 deletions

56
main.py
View File

@ -1,11 +1,15 @@
import secrets import secrets
import sys import sys
from random import randrange
from random import randrange
from src.decisionTree import * from src.decisionTree import *
from src.SubprojectMaksymilianKierski.PlateRecognition import use_model_to_predict, text_speech from src.SubprojectMaksymilianKierski.PlateRecognition import use_model_to_predict, text_speech
# Marcin Dobrowolski
from src.SubprojectMarcinDobrowolski.suggestionDecisionTree import *
from src.SubprojectMarcinDobrowolski.utility import generateTestData, generateTestExample
if __name__ == "__main__": if __name__ == "__main__":
# SETUP # SETUP
pygame.init() pygame.init()
@ -14,13 +18,17 @@ if __name__ == "__main__":
waiter = Waiter(graphics) waiter = Waiter(graphics)
tree = DecisionTree() tree = DecisionTree()
# init functions # Init functions
graphics.drawBackground(waiter.matrix) graphics.drawBackground(waiter.matrix)
graphics.update(waiter) graphics.update(waiter)
# AStar
goal = None goal = None
path = '' path = ''
# Marcin Dobrowolski
suggestionTreeRoot = SuggestionTree.buildTree(trainingData)
# Maksymilian # Maksymilian
go = 0 go = 0
rand = 0 rand = 0
@ -50,7 +58,8 @@ if __name__ == "__main__":
if event.key == pygame.K_m: if event.key == pygame.K_m:
tabPos = [[1, 2], [1, 5], [1, 8], [5, 4], [5, 8], [8, 2], [8, 5], [8, 8], [12, 3], [12, 7]] tabPos = [[1, 2], [1, 5], [1, 8], [5, 4], [5, 8],
[8, 2], [8, 5], [8, 8], [12, 3], [12, 7]]
if not [waiter.X, waiter.Y] in tabPos or go == 0: if not [waiter.X, waiter.Y] in tabPos or go == 0:
if [waiter.X, waiter.Y] in tabPos: if [waiter.X, waiter.Y] in tabPos:
@ -77,6 +86,46 @@ if __name__ == "__main__":
(waiter.X * 50 + 25), (waiter.Y * 50 - 25), False, False, screen=graphics.screen) (waiter.X * 50 + 25), (waiter.Y * 50 - 25), False, False, screen=graphics.screen)
pygame.display.flip() pygame.display.flip()
go = 0 go = 0
# Marcin Dobrowolski
if event.key == pygame.K_0:
generateTestData('testData.csv', 100)
testData = []
with open('src/SubprojectMarcinDobrowolski/Data/testData.csv') as csv_file:
csvReader = csv.reader(csv_file, delimiter=',')
lineCount = 0
for row in csvReader:
example = []
for column in row:
if column.isdigit():
example.append(int(column))
else:
example.append(column)
if lineCount > 0:
testData.append(example)
lineCount += 1
print('Processed lines: ', lineCount)
print('Test examples predictions:')
for example in testData:
print('{} - {}'.format(example, SuggestionTree.classify(
example, suggestionTreeRoot).printLeaf()))
# Marcin Dobrowolski
if event.key == pygame.K_1:
example = generateTestExample()
print('Test example prediction: ')
print('{} - {}'.format(example, SuggestionTree.classify(
example, suggestionTreeRoot).printLeaf()))
# Marcin Dobrowolski
if event.key == pygame.K_2:
SuggestionTree.printTree(suggestionTreeRoot)
# AStar
if event.key == pygame.K_r: if event.key == pygame.K_r:
temp = False temp = False
while not temp: while not temp:
@ -91,6 +140,7 @@ if __name__ == "__main__":
path = waiter.findPath(goal) path = waiter.findPath(goal)
path = waiter.translatePath(path) path = waiter.translatePath(path)
# AStar
if path != '': if path != '':
nextStep = path[0] nextStep = path[0]
path = path[1:] path = path[1:]

101
path Normal file
View File

@ -0,0 +1,101 @@
type,size,money,appetite
meat,regular,40,46
meat,regular,38,15
meat,regular,35,50
meat,little,42,25
meat,little,47,40
meat,little,56,48
meat,little,53,11
meat,regular,45,25
meat,little,17,34
meat,regular,39,41
meat,little,31,30
meat,little,14,14
meat,regular,38,38
meat,regular,51,28
meat,little,58,22
meat,little,35,17
meat,regular,47,36
meat,little,55,20
meat,regular,36,10
meat,regular,53,22
meat,regular,56,17
meat,regular,31,45
meat,regular,57,42
meat,regular,44,30
meat,regular,33,45
meat,little,52,25
meat,little,48,15
meat,regular,44,53
meat,little,30,28
meat,little,39,23
meat,little,52,28
meat,little,16,17
meat,little,22,54
meat,little,51,50
meat,regular,51,25
meat,little,14,28
meat,little,51,25
meat,regular,26,27
meat,regular,26,35
meat,little,41,24
meat,regular,22,38
meat,regular,37,25
meat,regular,41,28
meat,little,54,12
meat,regular,31,22
meat,regular,38,17
meat,little,51,32
meat,little,20,54
meat,little,36,30
meat,regular,33,46
meat,little,43,16
meat,regular,30,10
meat,little,14,36
meat,little,51,24
meat,little,11,13
meat,little,53,54
meat,regular,27,12
meat,regular,41,30
meat,little,13,25
meat,little,34,43
meat,regular,51,45
meat,regular,39,37
meat,little,44,54
meat,regular,37,12
meat,regular,46,13
meat,regular,25,21
meat,regular,37,13
meat,little,45,32
meat,little,28,46
meat,little,11,22
meat,little,37,32
meat,little,26,20
meat,regular,21,35
meat,regular,40,18
meat,regular,59,42
meat,regular,36,42
meat,regular,57,16
meat,little,20,47
meat,little,18,34
meat,regular,23,30
meat,regular,44,23
meat,little,41,28
meat,little,12,49
meat,regular,33,10
meat,regular,44,21
meat,little,57,51
meat,regular,49,45
meat,regular,36,40
meat,little,24,11
meat,little,45,45
meat,little,59,30
meat,little,16,23
meat,little,57,13
meat,little,58,36
meat,little,35,16
meat,little,44,54
meat,regular,21,21
meat,little,57,31
meat,little,50,34
meat,regular,25,40

View File

@ -0,0 +1,101 @@
type,size,money,appetite
meat,regular,35,53
meat,little,43,44
meat,little,21,50
meat,regular,39,17
meat,little,37,14
meat,regular,57,44
meat,regular,59,43
meat,regular,57,16
meat,little,33,19
meat,little,28,49
meat,little,40,53
meat,little,12,10
meat,regular,54,49
meat,regular,56,37
meat,little,19,13
meat,regular,38,49
meat,regular,48,15
meat,regular,57,49
meat,regular,20,40
meat,regular,52,47
meat,little,11,19
meat,regular,44,51
meat,little,39,43
meat,little,58,51
meat,regular,39,14
meat,little,59,46
meat,regular,20,34
meat,regular,37,22
meat,regular,56,17
meat,little,14,32
meat,regular,56,51
meat,regular,43,52
meat,little,30,25
meat,little,35,20
meat,little,25,11
meat,regular,37,32
meat,little,26,39
meat,little,58,26
meat,regular,42,28
meat,regular,21,24
meat,little,31,25
meat,regular,48,25
meat,little,50,10
meat,regular,51,34
meat,little,34,33
meat,little,41,19
meat,regular,25,25
meat,regular,21,19
meat,little,23,36
meat,little,37,41
meat,little,10,18
meat,little,38,41
meat,regular,37,54
meat,little,38,37
meat,regular,23,23
meat,regular,55,21
meat,little,30,48
meat,regular,48,40
meat,regular,25,34
meat,little,59,37
meat,little,50,12
meat,regular,51,13
meat,regular,57,29
meat,regular,28,25
meat,little,34,50
meat,little,20,39
meat,little,28,31
meat,little,56,13
meat,little,56,35
meat,little,21,33
meat,regular,33,33
meat,regular,37,34
meat,little,43,34
meat,regular,32,47
meat,regular,47,37
meat,regular,48,16
meat,little,19,53
meat,little,22,47
meat,little,18,34
meat,little,16,25
meat,regular,45,22
meat,little,55,13
meat,little,44,53
meat,little,31,29
meat,little,47,10
meat,regular,56,24
meat,little,25,40
meat,little,58,33
meat,little,15,40
meat,little,53,24
meat,little,54,18
meat,little,38,31
meat,regular,44,36
meat,little,57,24
meat,regular,59,47
meat,regular,45,52
meat,regular,36,42
meat,regular,45,24
meat,regular,23,12
meat,little,13,40
1 type size money appetite
2 meat regular 35 53
3 meat little 43 44
4 meat little 21 50
5 meat regular 39 17
6 meat little 37 14
7 meat regular 57 44
8 meat regular 59 43
9 meat regular 57 16
10 meat little 33 19
11 meat little 28 49
12 meat little 40 53
13 meat little 12 10
14 meat regular 54 49
15 meat regular 56 37
16 meat little 19 13
17 meat regular 38 49
18 meat regular 48 15
19 meat regular 57 49
20 meat regular 20 40
21 meat regular 52 47
22 meat little 11 19
23 meat regular 44 51
24 meat little 39 43
25 meat little 58 51
26 meat regular 39 14
27 meat little 59 46
28 meat regular 20 34
29 meat regular 37 22
30 meat regular 56 17
31 meat little 14 32
32 meat regular 56 51
33 meat regular 43 52
34 meat little 30 25
35 meat little 35 20
36 meat little 25 11
37 meat regular 37 32
38 meat little 26 39
39 meat little 58 26
40 meat regular 42 28
41 meat regular 21 24
42 meat little 31 25
43 meat regular 48 25
44 meat little 50 10
45 meat regular 51 34
46 meat little 34 33
47 meat little 41 19
48 meat regular 25 25
49 meat regular 21 19
50 meat little 23 36
51 meat little 37 41
52 meat little 10 18
53 meat little 38 41
54 meat regular 37 54
55 meat little 38 37
56 meat regular 23 23
57 meat regular 55 21
58 meat little 30 48
59 meat regular 48 40
60 meat regular 25 34
61 meat little 59 37
62 meat little 50 12
63 meat regular 51 13
64 meat regular 57 29
65 meat regular 28 25
66 meat little 34 50
67 meat little 20 39
68 meat little 28 31
69 meat little 56 13
70 meat little 56 35
71 meat little 21 33
72 meat regular 33 33
73 meat regular 37 34
74 meat little 43 34
75 meat regular 32 47
76 meat regular 47 37
77 meat regular 48 16
78 meat little 19 53
79 meat little 22 47
80 meat little 18 34
81 meat little 16 25
82 meat regular 45 22
83 meat little 55 13
84 meat little 44 53
85 meat little 31 29
86 meat little 47 10
87 meat regular 56 24
88 meat little 25 40
89 meat little 58 33
90 meat little 15 40
91 meat little 53 24
92 meat little 54 18
93 meat little 38 31
94 meat regular 44 36
95 meat little 57 24
96 meat regular 59 47
97 meat regular 45 52
98 meat regular 36 42
99 meat regular 45 24
100 meat regular 23 12
101 meat little 13 40

View File

@ -0,0 +1,213 @@
type,size,money,appetite,name
meat,small,31,55,schabowy
meat,small,40,41,schabowy
meat,small,40,55,schabowy
meat,small,31,41,schabowy
meat,regular,31,40,schabowy
meat,regular,40,26,schabowy
meat,regular,31,26,schabowy
meat,regular,40,40,schabowy
meat,large,31,25,schabowy
meat,large,40,10,schabowy
meat,large,31,10,schabowy
meat,large,40,25,schabowy
vege,small,20,41,placki ziemniaczane
vege,small,30,55,placki ziemniaczane
vege,small,20,55,placki ziemniaczane
vege,small,30,41,placki ziemniaczane
vege,regular,20,40,placki ziemniaczane
vege,regular,30,26,placki ziemniaczane
vege,regular,20,40,placki ziemniaczane
vege,regular,30,26,placki ziemniaczane
vege,regular,20,25,placki ziemniaczane
vege,regular,30,10,placki ziemniaczane
vege,regular,20,25,placki ziemniaczane
vege,regular,30,10,placki ziemniaczane
meat,regular,51,41,golonka
meat,regular,51,55,golonka
meat,regular,60,41,golonka
meat,regular,60,55,golonka
meat,large,51,26,golonka
meat,large,51,40,golonka
meat,large,60,26,golonka
meat,large,60,40,golonka
vege,small,10,41,sajgonki
vege,small,10,55,sajgonki
vege,small,20,41,sajgonki
vege,small,20,55,sajgonki
vege,small,10,10,sajgonki
vege,small,10,25,sajgonki
vege,small,20,10,sajgonki
vege,small,20,25,sajgonki
vege,small,10,26,sajgonki
vege,small,10,40,sajgonki
vege,small,20,26,sajgonki
vege,small,20,40,sajgonki
vege,small,31,41,salatka grecka
vege,small,31,55,salatka grecka
vege,small,40,41,salatka grecka
vege,small,40,55,salatka grecka
vege,regular,31,26,salatka grecka
vege,regular,31,40,salatka grecka
vege,regular,40,26,salatka grecka
vege,regular,40,40,salatka grecka
vege,regular,31,10,salatka grecka
vege,regular,31,25,salatka grecka
vege,regular,40,10,salatka grecka
vege,regular,40,25,salatka grecka
vege,regular,41,41,pieczony kalafior
vege,regular,41,55,pieczony kalafior
vege,regular,50,41,pieczony kalafior
vege,regular,50,55,pieczony kalafior
vege,large,41,26,pieczony kalafior
vege,large,41,40,pieczony kalafior
vege,large,50,26,pieczony kalafior
vege,large,50,40,pieczony kalafior
vege,large,41,10,pieczony kalafior
vege,large,41,25,pieczony kalafior
vege,large,50,10,pieczony kalafior
vege,large,50,25,pieczony kalafior
meat,small,10,41,paszteciki
meat,small,10,55,paszteciki
meat,small,20,41,paszteciki
meat,small,20,55,paszteciki
meat,small,10,26,paszteciki
meat,small,10,40,paszteciki
meat,small,20,26,paszteciki
meat,small,20,40,paszteciki
meat,small,10,10,paszteciki
meat,small,10,25,paszteciki
meat,small,20,10,paszteciki
meat,small,20,25,paszteciki
meat,small,21,41,burger
meat,small,21,55,burger
meat,small,30,41,burger
meat,small,30,55,burger
meat,regular,21,26,burger
meat,regular,21,40,burger
meat,regular,30,26,burger
meat,regular,30,40,burger
meat,large,21,10,burger
meat,large,21,25,burger
meat,large,30,10,burger
meat,large,30,25,burger
vege,regular,20,55,pancake
vege,regular,20,41,pancake
vege,regular,30,55,pancake
vege,regular,30,41,pancake
vege,large,20,26,pancake
vege,large,20,40,pancake
vege,large,30,26,pancake
vege,large,30,40,pancake
vege,large,20,10,pancake
vege,large,20,25,pancake
vege,large,30,10,pancake
vege,large,30,25,pancake
meat,small,50,41,sushi
meat,small,50,55,sushi
meat,small,60,41,sushi
meat,small,60,55,sushi
meat,regular,50,26,sushi
meat,regular,50,40,sushi
meat,regular,60,26,sushi
meat,regular,60,40,sushi
meat,large,50,10,sushi
meat,large,50,25,sushi
meat,large,60,10,sushi
meat,large,60,25,sushi
vege,small,50,41,vege sushi
vege,small,50,55,vege sushi
vege,small,60,41,vege sushi
vege,small,60,55,vege sushi
vege,regular,50,26,vege sushi
vege,regular,50,40,vege sushi
vege,regular,60,26,vege sushi
vege,regular,60,40,vege sushi
vege,large,50,10,vege sushi
vege,large,50,25,vege sushi
vege,large,60,10,vege sushi
vege,large,60,25,vege sushi
vege,small,21,31,kimczi
vege,small,21,40,kimczi
vege,small,30,31,kimczi
vege,small,30,40,kimczi
vege,small,21,10,kimczi
vege,small,21,25,kimczi
vege,small,30,10,kimczi
vege,small,30,25,kimczi
meat,small,21,41,dorsz z frytkami
meat,small,21,55,dorsz z frytkami
meat,small,30,41,dorsz z frytkami
meat,small,30,55,dorsz z frytkami
meat,regular,21,31,dorsz z frytkami
meat,regular,21,40,dorsz z frytkami
meat,regular,30,31,dorsz z frytkami
meat,regular,30,40,dorsz z frytkami
meat,regular,21,10,dorsz z frytkami
meat,regular,21,26,dorsz z frytkami
meat,regular,30,10,dorsz z frytkami
meat,regular,30,26,dorsz z frytkami
vege,small,50,26,kawior
vege,small,50,40,kawior
vege,small,60,26,kawior
vege,small,60,40,kawior
vege,small,50,10,kawior
vege,small,50,25,kawior
vege,small,60,10,kawior
vege,small,60,25,kawior
meat,regular,40,41,stek z dzika
meat,regular,40,55,stek z dzika
meat,regular,50,41,stek z dzika
meat,regular,50,55,stek z dzika
meat,large,40,31,stek z dzika
meat,large,40,40,stek z dzika
meat,large,50,31,stek z dzika
meat,large,50,40,stek z dzika
meat,large,40,10,stek z dzika
meat,large,40,25,stek z dzika
meat,large,50,10,stek z dzika
meat,large,50,25,stek z dzika
meat,large,50,41,kaczka po pekinsku
meat,large,50,55,kaczka po pekinsku
meat,large,60,41,kaczka po pekinsku
meat,large,60,55,kaczka po pekinsku
meat,large,50,31,kaczka po pekinsku
meat,large,50,40,kaczka po pekinsku
meat,large,60,31,kaczka po pekinsku
meat,large,60,40,kaczka po pekinsku
meat,small,31,26,tatar
meat,small,31,40,tatar
meat,small,40,26,tatar
meat,small,40,40,tatar
meat,small,31,10,tatar
meat,small,31,25,tatar
meat,small,40,10,tatar
meat,small,40,25,tatar
meat,regular,31,41,english breakfast
meat,regular,31,55,english breakfast
meat,regular,40,41,english breakfast
meat,regular,40,55,english breakfast
meat,large,31,26,english breakfast
meat,large,31,40,english breakfast
meat,large,40,26,english breakfast
meat,large,40,40,english breakfast
meat,small,41,41,losos
meat,small,41,50,losos
meat,small,50,41,losos
meat,small,50,50,losos
meat,regular,41,26,losos
meat,regular,41,40,losos
meat,regular,50,26,losos
meat,regular,50,40,losos
meat,regular,41,10,losos
meat,regular,41,25,losos
meat,regular,50,10,losos
meat,regular,50,25,losos
meat,small,21,26,chicken wings
meat,small,21,40,chicken wings
meat,small,30,26,chicken wings
meat,small,30,40,chicken wings
meat,small,21,10,chicken wings
meat,small,21,25,chicken wings
meat,small,30,10,chicken wings
meat,small,30,25,chicken wings
1 type size money appetite name
2 meat small 31 55 schabowy
3 meat small 40 41 schabowy
4 meat small 40 55 schabowy
5 meat small 31 41 schabowy
6 meat regular 31 40 schabowy
7 meat regular 40 26 schabowy
8 meat regular 31 26 schabowy
9 meat regular 40 40 schabowy
10 meat large 31 25 schabowy
11 meat large 40 10 schabowy
12 meat large 31 10 schabowy
13 meat large 40 25 schabowy
14 vege small 20 41 placki ziemniaczane
15 vege small 30 55 placki ziemniaczane
16 vege small 20 55 placki ziemniaczane
17 vege small 30 41 placki ziemniaczane
18 vege regular 20 40 placki ziemniaczane
19 vege regular 30 26 placki ziemniaczane
20 vege regular 20 40 placki ziemniaczane
21 vege regular 30 26 placki ziemniaczane
22 vege regular 20 25 placki ziemniaczane
23 vege regular 30 10 placki ziemniaczane
24 vege regular 20 25 placki ziemniaczane
25 vege regular 30 10 placki ziemniaczane
26 meat regular 51 41 golonka
27 meat regular 51 55 golonka
28 meat regular 60 41 golonka
29 meat regular 60 55 golonka
30 meat large 51 26 golonka
31 meat large 51 40 golonka
32 meat large 60 26 golonka
33 meat large 60 40 golonka
34 vege small 10 41 sajgonki
35 vege small 10 55 sajgonki
36 vege small 20 41 sajgonki
37 vege small 20 55 sajgonki
38 vege small 10 10 sajgonki
39 vege small 10 25 sajgonki
40 vege small 20 10 sajgonki
41 vege small 20 25 sajgonki
42 vege small 10 26 sajgonki
43 vege small 10 40 sajgonki
44 vege small 20 26 sajgonki
45 vege small 20 40 sajgonki
46 vege small 31 41 salatka grecka
47 vege small 31 55 salatka grecka
48 vege small 40 41 salatka grecka
49 vege small 40 55 salatka grecka
50 vege regular 31 26 salatka grecka
51 vege regular 31 40 salatka grecka
52 vege regular 40 26 salatka grecka
53 vege regular 40 40 salatka grecka
54 vege regular 31 10 salatka grecka
55 vege regular 31 25 salatka grecka
56 vege regular 40 10 salatka grecka
57 vege regular 40 25 salatka grecka
58 vege regular 41 41 pieczony kalafior
59 vege regular 41 55 pieczony kalafior
60 vege regular 50 41 pieczony kalafior
61 vege regular 50 55 pieczony kalafior
62 vege large 41 26 pieczony kalafior
63 vege large 41 40 pieczony kalafior
64 vege large 50 26 pieczony kalafior
65 vege large 50 40 pieczony kalafior
66 vege large 41 10 pieczony kalafior
67 vege large 41 25 pieczony kalafior
68 vege large 50 10 pieczony kalafior
69 vege large 50 25 pieczony kalafior
70 meat small 10 41 paszteciki
71 meat small 10 55 paszteciki
72 meat small 20 41 paszteciki
73 meat small 20 55 paszteciki
74 meat small 10 26 paszteciki
75 meat small 10 40 paszteciki
76 meat small 20 26 paszteciki
77 meat small 20 40 paszteciki
78 meat small 10 10 paszteciki
79 meat small 10 25 paszteciki
80 meat small 20 10 paszteciki
81 meat small 20 25 paszteciki
82 meat small 21 41 burger
83 meat small 21 55 burger
84 meat small 30 41 burger
85 meat small 30 55 burger
86 meat regular 21 26 burger
87 meat regular 21 40 burger
88 meat regular 30 26 burger
89 meat regular 30 40 burger
90 meat large 21 10 burger
91 meat large 21 25 burger
92 meat large 30 10 burger
93 meat large 30 25 burger
94 vege regular 20 55 pancake
95 vege regular 20 41 pancake
96 vege regular 30 55 pancake
97 vege regular 30 41 pancake
98 vege large 20 26 pancake
99 vege large 20 40 pancake
100 vege large 30 26 pancake
101 vege large 30 40 pancake
102 vege large 20 10 pancake
103 vege large 20 25 pancake
104 vege large 30 10 pancake
105 vege large 30 25 pancake
106 meat small 50 41 sushi
107 meat small 50 55 sushi
108 meat small 60 41 sushi
109 meat small 60 55 sushi
110 meat regular 50 26 sushi
111 meat regular 50 40 sushi
112 meat regular 60 26 sushi
113 meat regular 60 40 sushi
114 meat large 50 10 sushi
115 meat large 50 25 sushi
116 meat large 60 10 sushi
117 meat large 60 25 sushi
118 vege small 50 41 vege sushi
119 vege small 50 55 vege sushi
120 vege small 60 41 vege sushi
121 vege small 60 55 vege sushi
122 vege regular 50 26 vege sushi
123 vege regular 50 40 vege sushi
124 vege regular 60 26 vege sushi
125 vege regular 60 40 vege sushi
126 vege large 50 10 vege sushi
127 vege large 50 25 vege sushi
128 vege large 60 10 vege sushi
129 vege large 60 25 vege sushi
130 vege small 21 31 kimczi
131 vege small 21 40 kimczi
132 vege small 30 31 kimczi
133 vege small 30 40 kimczi
134 vege small 21 10 kimczi
135 vege small 21 25 kimczi
136 vege small 30 10 kimczi
137 vege small 30 25 kimczi
138 meat small 21 41 dorsz z frytkami
139 meat small 21 55 dorsz z frytkami
140 meat small 30 41 dorsz z frytkami
141 meat small 30 55 dorsz z frytkami
142 meat regular 21 31 dorsz z frytkami
143 meat regular 21 40 dorsz z frytkami
144 meat regular 30 31 dorsz z frytkami
145 meat regular 30 40 dorsz z frytkami
146 meat regular 21 10 dorsz z frytkami
147 meat regular 21 26 dorsz z frytkami
148 meat regular 30 10 dorsz z frytkami
149 meat regular 30 26 dorsz z frytkami
150 vege small 50 26 kawior
151 vege small 50 40 kawior
152 vege small 60 26 kawior
153 vege small 60 40 kawior
154 vege small 50 10 kawior
155 vege small 50 25 kawior
156 vege small 60 10 kawior
157 vege small 60 25 kawior
158 meat regular 40 41 stek z dzika
159 meat regular 40 55 stek z dzika
160 meat regular 50 41 stek z dzika
161 meat regular 50 55 stek z dzika
162 meat large 40 31 stek z dzika
163 meat large 40 40 stek z dzika
164 meat large 50 31 stek z dzika
165 meat large 50 40 stek z dzika
166 meat large 40 10 stek z dzika
167 meat large 40 25 stek z dzika
168 meat large 50 10 stek z dzika
169 meat large 50 25 stek z dzika
170 meat large 50 41 kaczka po pekinsku
171 meat large 50 55 kaczka po pekinsku
172 meat large 60 41 kaczka po pekinsku
173 meat large 60 55 kaczka po pekinsku
174 meat large 50 31 kaczka po pekinsku
175 meat large 50 40 kaczka po pekinsku
176 meat large 60 31 kaczka po pekinsku
177 meat large 60 40 kaczka po pekinsku
178 meat small 31 26 tatar
179 meat small 31 40 tatar
180 meat small 40 26 tatar
181 meat small 40 40 tatar
182 meat small 31 10 tatar
183 meat small 31 25 tatar
184 meat small 40 10 tatar
185 meat small 40 25 tatar
186 meat regular 31 41 english breakfast
187 meat regular 31 55 english breakfast
188 meat regular 40 41 english breakfast
189 meat regular 40 55 english breakfast
190 meat large 31 26 english breakfast
191 meat large 31 40 english breakfast
192 meat large 40 26 english breakfast
193 meat large 40 40 english breakfast
194 meat small 41 41 losos
195 meat small 41 50 losos
196 meat small 50 41 losos
197 meat small 50 50 losos
198 meat regular 41 26 losos
199 meat regular 41 40 losos
200 meat regular 50 26 losos
201 meat regular 50 40 losos
202 meat regular 41 10 losos
203 meat regular 41 25 losos
204 meat regular 50 10 losos
205 meat regular 50 25 losos
206 meat small 21 26 chicken wings
207 meat small 21 40 chicken wings
208 meat small 30 26 chicken wings
209 meat small 30 40 chicken wings
210 meat small 21 10 chicken wings
211 meat small 21 25 chicken wings
212 meat small 30 10 chicken wings
213 meat small 30 25 chicken wings

View File

@ -0,0 +1,8 @@
class DecisionNode:
def __init__(self,
question,
trueBranch,
falseBranch):
self.question = question
self.trueBranch = trueBranch
self.falseBranch = falseBranch

View File

@ -0,0 +1,14 @@
from src.SubprojectMarcinDobrowolski.utility import classCount
class Leaf:
def __init__(self, rows):
self.predictions = classCount(rows)
def printLeaf(self):
total = sum(self.predictions.values()) * 1.0
probs = {}
for label in self.predictions.keys():
probs[label] = str(
int(self.predictions[label] / total * 100)) + '%'
return probs

View File

@ -0,0 +1,21 @@
from src.SubprojectMarcinDobrowolski.utility import is_numeric
class Question:
def __init__(self, columnLabel, column, value):
self.columnLabel = columnLabel
self.column = column
self.value = value
def match(self, example):
val = example[self.column]
if is_numeric(val):
return val <= self.value
else:
return val == self.value
def __repr__(self):
condition = '=='
if is_numeric(self.value):
condition = '<='
return 'Is {} {} {}?'.format(self.columnLabel, condition, str(self.value))

View File

@ -0,0 +1,112 @@
import csv
from src.SubprojectMarcinDobrowolski.leaf import Leaf
from src.SubprojectMarcinDobrowolski.decisionNode import DecisionNode
from src.SubprojectMarcinDobrowolski.question import Question
from src.SubprojectMarcinDobrowolski.utility import *
class SuggestionTree:
def readTrainingData(path):
with open(path) as csv_file:
csvReader = csv.reader(csv_file, delimiter=',')
lineCount = 0
trainingData = []
labels = []
for row in csvReader:
example = []
for column in row:
if lineCount == 0:
labels.append(column)
else:
if column.isdigit():
example.append(int(column))
else:
example.append(column)
if lineCount > 0:
trainingData.append(example)
lineCount += 1
print('Processed lines: ', lineCount)
return trainingData, labels
def gini(rows):
counts = classCount(rows)
impurity = 1
for lbl in counts:
prob_of_lbl = counts[lbl] / float(len(rows))
impurity -= prob_of_lbl**2
return impurity
def infoGain(left, right, currentUncertainty):
p = float(len(left)) / (len(left) + len(right))
return currentUncertainty - p * SuggestionTree.gini(left) - (1 - p) * SuggestionTree.gini(right)
def findBestSplit(rows):
bestGain = 0
bestQuestion = None
currentUncertainty = SuggestionTree.gini(rows)
nFeatures = len(labels) - 1
for column in range(nFeatures):
values = set([row[column] for row in rows])
for value in values:
question = Question(labels[column], column, value)
trueRows, falseRows = partition(rows, question)
if len(trueRows) == 0 or len(falseRows) == 0:
continue
gain = SuggestionTree.infoGain(
trueRows, falseRows, currentUncertainty)
if gain > bestGain:
bestGain, bestQuestion = gain, question
return bestGain, bestQuestion
def buildTree(rows):
gain, question = SuggestionTree.findBestSplit(rows)
if gain == 0:
return Leaf(rows)
trueRows, falseRows = partition(rows, question)
trueBranch = SuggestionTree.buildTree(trueRows)
falseBranch = SuggestionTree.buildTree(falseRows)
return DecisionNode(question, trueBranch, falseBranch)
def printTree(node, spacing=' '):
if isinstance(node, Leaf):
print(spacing + 'Predict', node.predictions)
return
print(spacing + str(node.question))
print(spacing + '--> True:')
SuggestionTree.printTree(node.trueBranch, spacing + ' ')
print(spacing + '--> False:')
SuggestionTree.printTree(node.falseBranch, spacing + ' ')
def classify(row, node):
if isinstance(node, Leaf):
return node
if node.question.match(row):
return SuggestionTree.classify(row, node.trueBranch)
else:
return SuggestionTree.classify(row, node.falseBranch)
trainingData, labels = SuggestionTree.readTrainingData(
'src/SubprojectMarcinDobrowolski/Data/trainingData.csv')

View File

@ -0,0 +1,159 @@
import csv
import random
def uniqueValues(rows, column):
return set([row[column] for row in rows])
def classCount(rows):
counts = {}
for row in rows:
label = row[-1]
if label not in counts:
counts[label] = 0
counts[label] += 1
return counts
def is_numeric(value):
return isinstance(value, int) or isinstance(value, float)
def partition(rows, question):
trueRows, falseRows = [], []
for row in rows:
if question.match(row):
trueRows.append(row)
else:
falseRows.append(row)
return trueRows, falseRows
def generateTestData(path, quantity):
with open('path', 'w') as csvFile:
csvWriter = csv.writer(csvFile, delimiter=',')
csvWriter.writerow(['type', 'size', 'money', 'appetite'])
for a in range(quantity):
example = []
category = random.randrange(0, 1)
size = random.randrange(0, 2)
if category == 0:
example.append('meat')
if size == 0:
money = random.randrange(10, 60)
appetite = random.randrange(10, 55)
example.append('little')
example.append(money)
example.append(appetite)
if size == 1:
money = random.randrange(20, 60)
appetite = random.randrange(10, 55)
if appetite > 40:
if money < 30:
money = random.randrange(30, 60)
example.append('regular')
example.append(money)
example.append(appetite)
if size == 2:
money = random.randrange(20, 60)
appetite = random.randrange(10, 55)
if appetite > 25 and appetite <= 40:
money = random.randrange(30, 60)
if appetite > 40:
money = random.randrange(50, 60)
example.append('large')
example.append(money)
example.append(appetite)
elif category == 1:
example.append('vege')
if size == 0:
money = random.randrange(10, 60)
appetite = random.randrange(10, 55)
example.append('little')
example.append(money)
example.append(appetite)
if size == 1:
money = random.randrange(20, 60)
appetite = random.randrange(10, 55)
example.append('regular,')
example.append(money)
example.append(appetite)
if size == 2:
money = random.randrange(20, 60)
appetite = random.randrange(10, 40)
example.append('large,')
example.append(money)
example.append(appetite)
csvWriter.writerow(example)
def generateTestExample():
example = []
category = random.randrange(0, 1)
size = random.randrange(0, 2)
if category == 0:
example.append('meat')
if size == 0:
money = random.randrange(10, 60)
appetite = random.randrange(10, 55)
example.append('little')
example.append(money)
example.append(appetite)
if size == 1:
money = random.randrange(20, 60)
appetite = random.randrange(10, 55)
if appetite > 40:
if money < 30:
money = random.randrange(30, 60)
example.append('regular')
example.append(money)
example.append(appetite)
if size == 2:
money = random.randrange(20, 60)
appetite = random.randrange(10, 55)
if appetite > 25 and appetite <= 40:
money = random.randrange(30, 60)
if appetite > 40:
money = random.randrange(50, 60)
example.append('large')
example.append(money)
example.append(appetite)
elif category == 1:
example.append('vege')
if size == 0:
money = random.randrange(10, 60)
appetite = random.randrange(10, 55)
example.append('little')
example.append(money)
example.append(appetite)
if size == 1:
money = random.randrange(20, 60)
appetite = random.randrange(10, 55)
example.append('regular,')
example.append(money)
example.append(appetite)
if size == 2:
money = random.randrange(20, 60)
appetite = random.randrange(10, 40)
example.append('large,')
example.append(money)
example.append(appetite)
return example

Binary file not shown.