suggestionDecisionTree implementation
This commit is contained in:
parent
2e114bc0ee
commit
a7006c1424
56
main.py
56
main.py
@ -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
101
path
Normal 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
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
101
src/SubprojectMarcinDobrowolski/Data/testData.csv
Normal file
101
src/SubprojectMarcinDobrowolski/Data/testData.csv
Normal 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
|
|
213
src/SubprojectMarcinDobrowolski/Data/trainingData.csv
Normal file
213
src/SubprojectMarcinDobrowolski/Data/trainingData.csv
Normal 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
|
|
8
src/SubprojectMarcinDobrowolski/decisionNode.py
Normal file
8
src/SubprojectMarcinDobrowolski/decisionNode.py
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
class DecisionNode:
|
||||||
|
def __init__(self,
|
||||||
|
question,
|
||||||
|
trueBranch,
|
||||||
|
falseBranch):
|
||||||
|
self.question = question
|
||||||
|
self.trueBranch = trueBranch
|
||||||
|
self.falseBranch = falseBranch
|
14
src/SubprojectMarcinDobrowolski/leaf.py
Normal file
14
src/SubprojectMarcinDobrowolski/leaf.py
Normal 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
|
21
src/SubprojectMarcinDobrowolski/question.py
Normal file
21
src/SubprojectMarcinDobrowolski/question.py
Normal 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))
|
112
src/SubprojectMarcinDobrowolski/suggestionDecisionTree.py
Normal file
112
src/SubprojectMarcinDobrowolski/suggestionDecisionTree.py
Normal 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')
|
159
src/SubprojectMarcinDobrowolski/utility.py
Normal file
159
src/SubprojectMarcinDobrowolski/utility.py
Normal 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.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Loading…
Reference in New Issue
Block a user