-
Notifications
You must be signed in to change notification settings - Fork 0
/
LearningAlgorithm.py
31 lines (24 loc) · 1001 Bytes
/
LearningAlgorithm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
Abstract Class for Learning Algorithm
"""
import abc
from utils import get_full_examples_from_csv, Examples
""""""""""""""""""""""""""""""""""""""""""" LearningAlgorithm """""""""""""""""""""""""""""""""""""""""""
class LearningAlgorithm(abc.ABC):
def __init__(self, train_path: str):
self._train_examples = get_full_examples_from_csv(train_path)
@abc.abstractmethod
def classify(self, test_path: str) -> float:
...
def _get_accuracy(self, test_examples: Examples) -> float:
classify_correct, test_examples_amount = 0, 0
for example in test_examples:
example_result = self.classify_one(example)
if example_result == example[0]:
classify_correct += 1
test_examples_amount += 1
return classify_correct / test_examples_amount
######### Private Functions for LearningAlgorithm #########
@abc.abstractmethod
def classify_one(self, test_example: Examples) -> int:
...