-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm_l_fitter.py
47 lines (38 loc) · 1.5 KB
/
m_l_fitter.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
class MLFitter:
vectorizer = None
data_manager = None
model_preceptor = None
data_to_train = None
data_to_fit = None
def __init__(self, *args, **kwargs):
self.vectorizer = self.__get_param(kwargs, 'vectorizer')
self.data_manager = self.__get_param(kwargs, 'data_manager')
self.model_preceptor = self.__get_param(kwargs, 'model_preceptor')
@staticmethod
def __get_param(kwargs, name):
param = kwargs.get(name, False)
if not param:
raise Exception('No %s provided'.format(name))
return param
def train(self, data, labels):
if self.data_to_fit is None:
data = self.__preprocess_data(data)
data = self.__vectorize(data)
self.data_to_fit = data
self.__fit_model(self.data_to_fit, labels)
def predict(self, data):
if self.data_to_train is None:
data = self.__preprocess_data(data)
data = self.__vectorize_predictions(data)
self.data_to_train = data
return self.__predict(self.data_to_train)
def __vectorize(self, data):
return self.vectorizer.fit_transform(data)
def __vectorize_predictions(self, data):
return self.vectorizer.transform(data)
def __preprocess_data(self, data):
return self.data_manager.process_data(data)
def __fit_model(self, data, labels):
self.model_preceptor.fit(data, labels)
def __predict(self, data):
return self.model_preceptor.predict(data)