mirror of
https://github.com/3x4byte/StreetsignRecognition.git
synced 2025-12-22 01:45:52 +00:00
added proper implementation for learn in decisiontree
This commit is contained in:
@@ -17,40 +17,49 @@ class DecisionTree:
|
||||
# TODO
|
||||
# IMPLEMENTATION IS NOT WORKING
|
||||
|
||||
def classify(self, feature_vector: FeatureVector) -> tuple[Concept, int]:
|
||||
def classify(self, feature_vector: FeatureVector) -> Concept:
|
||||
if len(self.children) == 0:
|
||||
return self.concept, self.depth
|
||||
return self.concept
|
||||
|
||||
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 Concept.UNKNOWN
|
||||
|
||||
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):
|
||||
while not self._finished(i, len(training_set)):
|
||||
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
|
||||
|
||||
concept = self.classify(vector)
|
||||
#print(f"{i}\t: actual: {vector.concept} \t classified as: {concept}")
|
||||
if 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={})})
|
||||
|
||||
|
||||
self._insert(vector)
|
||||
#i += 1
|
||||
|
||||
|
||||
def _insert(self, vector: FeatureVector) -> None:
|
||||
if self.concept == Concept.UNKNOWN:
|
||||
self.concept = vector.concept
|
||||
|
||||
else:
|
||||
feature_value = vector.features_list[self.depth]
|
||||
|
||||
try:
|
||||
sub_decision_tree: Self = self.children[feature_value] # get sub decition tree for feature
|
||||
sub_decision_tree._insert(vector)
|
||||
except KeyError:
|
||||
self.children.update({vector.features_list[self.depth]: DecisionTree(concept=vector.concept, parent=self, depth=self.depth+1, children={})})
|
||||
|
||||
|
||||
def _finished(self, i: int, num_training_data: int):
|
||||
# TODO implement proper finished condition
|
||||
if i < num_training_data:
|
||||
if i < num_training_data*3:
|
||||
return False
|
||||
return True
|
||||
Reference in New Issue
Block a user