forked from microsoft/0xDeCA10B
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassifier.py
88 lines (72 loc) · 2.68 KB
/
classifier.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import logging
from abc import ABC, abstractmethod
from typing import List
from decai.simulation.contract.objects import SmartContract
from decai.simulation.data.featuremapping.feature_index_mapper import FeatureIndexMapping
class Classifier(ABC, SmartContract):
"""
A classifier that can take a data sample as input and return a predict classification/label for the data.
"""
@abstractmethod
def evaluate(self, data, labels) -> float:
"""
Evaluate the model.
:param data: Data samples.
:param labels: The ground truth labels for `data`.
:return: The accuracy for the given test set.
"""
pass
@abstractmethod
def log_evaluation_details(self, data, labels, level=logging.INFO) -> float:
"""
Log some evaluation details.
:param data: Data samples.
:param labels: The ground truth labels for `data`.
:param level: The level at which to log.
:return: The accuracy for the given test set.
"""
pass
@abstractmethod
def init_model(self, training_data, labels, save_model=False):
"""
Fit the model to a specific dataset.
:param training_data: The data to use to train the model.
:param labels: The ground truth labels for `data`.
:param save_model: `True` if the model should be saved, `False` otherwise.
"""
pass
@abstractmethod
def predict(self, data):
"""
:param data: The data or features for one sample.
:return: The predicted classification or label for `data`.
"""
pass
@abstractmethod
def update(self, data, classification):
"""
Update the classifier with one data sample.
:param data: The training data or features for one sample.
:param classification: The label for `data`.
"""
pass
@abstractmethod
def reset_model(self):
"""
Re-initialize the model to the same state it was in after `init_model` was called.
"""
pass
@abstractmethod
def export(self,
path: str,
classifications: List[str] = None,
model_type: str = None,
feature_index_mapping: FeatureIndexMapping = None):
"""
Export the model in a format for the demo Node.js code to load.
:param path: The path to save the exported model to.
:param classifications: The classifications output by the model.
:param model_type: The type of the model.
:param feature_index_mapping: Mapping of the feature indices. Mainly for sparse models that were converted to dense ones.
"""
pass