-
Notifications
You must be signed in to change notification settings - Fork 20
/
base_classifier.py
executable file
·59 lines (44 loc) · 1.21 KB
/
base_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
#!/usr/bin/env python2.7
# -*- coding: utf-8 -*-
#@author Bin Hong
import os
import sys
import pickle
import keras
from abc import ABCMeta, abstractmethod
class BaseClassifier:
__metaclass__ = ABCMeta
@abstractmethod
def get_name(self):
pass
@abstractmethod
def fit(self, X, y):
pass
@abstractmethod
def predict_proba(self, X):
pass
@abstractmethod
def get_feature_importances(self, feat_names):
pass
@abstractmethod
def save(self, save_path):
pass
@abstractmethod
def load(self, save_path):
pass
class SklearnClassifier(BaseClassifier):
__metaclass__ = ABCMeta
def fit(self, X, y):
return self.classifier.fit(X, y)
def save(self, save_path):
with open(save_path, 'wb') as fout:
pickle.dump(self.classifier, fout, protocol=-1)
def load(self, save_path):
with open(save_path, 'rb') as fin:
self.classifier = pickle.load(fin)
class KerasClassifier(BaseClassifier):
__metaclass__ = ABCMeta
def save(self, save_path):
self.classifier.save(save_path)
def load(self, save_path):
self.classifier = keras.models.load_model(save_path)