mirror of
https://github.com/3x4byte/StreetsignRecognition.git
synced 2025-12-21 09:25:52 +00:00
added DecisionTree and not working implementaion
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,5 +1,6 @@
|
|||||||
|
|
||||||
.vsc
|
.vsc
|
||||||
|
.vscode
|
||||||
.venv
|
.venv
|
||||||
|
|
||||||
data
|
data
|
||||||
|
|||||||
56
src/classes/decision_tree.py
Normal file
56
src/classes/decision_tree.py
Normal file
@@ -0,0 +1,56 @@
|
|||||||
|
from typing import Self
|
||||||
|
import csv
|
||||||
|
import ast
|
||||||
|
|
||||||
|
from classes.concept import Concept
|
||||||
|
from classes.feature_vector import FeatureVector
|
||||||
|
|
||||||
|
class DecisionTree:
|
||||||
|
|
||||||
|
def __init__(self, children: dict = {}, parent: Self = None, concept: Concept = Concept.UNKNOWN, depth: int = 0) -> None:
|
||||||
|
self.children: dict = children # (number, DecisionTree)
|
||||||
|
self.parent: Self = parent
|
||||||
|
self.concept: Concept = concept
|
||||||
|
self.depth: int = depth
|
||||||
|
|
||||||
|
|
||||||
|
# TODO
|
||||||
|
# IMPLEMENTATION IS NOT WORKING
|
||||||
|
|
||||||
|
def classify(self, feature_vector: FeatureVector) -> tuple[Concept, int]:
|
||||||
|
if len(self.children) == 0:
|
||||||
|
return self.concept, self.depth
|
||||||
|
|
||||||
|
feature_value = feature_vector.features_list[self.depth] # get feature value of vector based on current depth
|
||||||
|
|
||||||
|
try:
|
||||||
|
new_decision_tree: Self = self.children[feature_value] # get sub decition tree for feature
|
||||||
|
except KeyError:
|
||||||
|
return Concept.UNKNOWN, self.depth
|
||||||
|
|
||||||
|
return new_decision_tree.classify(feature_vector) # classify new desicion tree
|
||||||
|
|
||||||
|
|
||||||
|
def learn(self, training_set: list[FeatureVector]) -> None:
|
||||||
|
i = 0
|
||||||
|
while not self._finished(i, len(training_set)*2):
|
||||||
|
vector: FeatureVector = training_set[i % len(training_set)]
|
||||||
|
concept, depth = self.classify(vector)
|
||||||
|
print(f"concept is {vector.concept}, was classified as {concept} in depth: {depth}")
|
||||||
|
if concept == Concept.UNKNOWN:
|
||||||
|
self.concept = vector.concept
|
||||||
|
i+=1
|
||||||
|
|
||||||
|
elif concept == vector.concept:
|
||||||
|
i+=1
|
||||||
|
|
||||||
|
else:
|
||||||
|
self.concept = Concept.UNKNOWN
|
||||||
|
self.children.update({vector.features_list[depth]: DecisionTree(concept=vector.concept, parent=self, depth=depth+1, children={})})
|
||||||
|
|
||||||
|
|
||||||
|
def _finished(self, i: int, num_training_data: int):
|
||||||
|
# TODO implement proper finished condition
|
||||||
|
if i < num_training_data:
|
||||||
|
return False
|
||||||
|
return True
|
||||||
27
src/main.py
27
src/main.py
@@ -2,8 +2,12 @@ import os
|
|||||||
import ast
|
import ast
|
||||||
import csv
|
import csv
|
||||||
|
|
||||||
from classes.learner import Learner
|
|
||||||
from classes.concept import Concept
|
from classes.concept import Concept
|
||||||
|
from classes.feature_vector import FeatureVector
|
||||||
|
|
||||||
|
|
||||||
|
def knn():
|
||||||
|
from classes.learner import Learner
|
||||||
|
|
||||||
training = os.path.abspath(os.path.join(__file__, "..", "training.csv"))
|
training = os.path.abspath(os.path.join(__file__, "..", "training.csv"))
|
||||||
testing = os.path.abspath(os.path.join(__file__, "..", "testing.csv"))
|
testing = os.path.abspath(os.path.join(__file__, "..", "testing.csv"))
|
||||||
@@ -24,4 +28,23 @@ for test_feature_vector in testing_data:
|
|||||||
distances = learner.classify(ast.literal_eval(test_feature_vector[1]))
|
distances = learner.classify(ast.literal_eval(test_feature_vector[1]))
|
||||||
cnt += learner.analyse(distances, Concept.identify_by_str(test_feature_vector[0].split('.')[1]))
|
cnt += learner.analyse(distances, Concept.identify_by_str(test_feature_vector[0].split('.')[1]))
|
||||||
|
|
||||||
print(f"Correctnes: {(cnt / len(testing_data)) * 100}%")
|
print(f"Correctness: {(cnt / len(testing_data)) * 100}%")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
from classes.decision_tree import DecisionTree
|
||||||
|
|
||||||
|
training = os.path.abspath(os.path.join(__file__, "..", "training.csv"))
|
||||||
|
testing = os.path.abspath(os.path.join(__file__, "..", "testing.csv"))
|
||||||
|
|
||||||
|
testing_data = []
|
||||||
|
with open(testing, 'r') as csv_file:
|
||||||
|
reader = csv.reader(csv_file, delimiter=";")
|
||||||
|
next(reader)
|
||||||
|
for row in reader:
|
||||||
|
fv = FeatureVector(concept = Concept.identify_by_str(row[1].split(".")[1]), features_list = ast.literal_eval(row[2]), loaded = True)
|
||||||
|
testing_data.append(fv)
|
||||||
|
|
||||||
|
tree = DecisionTree()
|
||||||
|
tree.learn(testing_data)
|
||||||
Reference in New Issue
Block a user