-
Notifications
You must be signed in to change notification settings - Fork 4
/
defs.py
82 lines (63 loc) · 2.3 KB
/
defs.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from __future__ import print_function
from __future__ import division
import importlib
from lime import LIME
import numpy as np
class Model(object):
def test_auc(self):
"""Returns the area under ROC curve for the test data."""
raise NotImplementedError()
def train_auc(self):
"""Returns the area under ROC curve for the training data."""
raise NotImplementedError()
def shape(self):
"""Returns the shape of the test data."""
raise NotImplementedError()
def features(self):
"""Returns the feature names as list.
Features that contain a '=' are interpreted as categorical
features where the left side is the name and the right side is
the value of the feature.
"""
raise NotImplementedError()
def threshold(self):
"""The threshold for prediction scores."""
raise NotImplementedError()
def get_label(self, rix):
"""Returns the binary (True or False) label of the test data row with the given index."""
raise NotImplementedError()
def get_row(self, rix):
"""Returns the given row of the test data."""
raise NotImplementedError()
def predict_proba(self, X):
"""Returns the prediction scores for X. For each row one prediction
score must be returned (output shape is (X.shape[0],)).
Parameters:
-----------
X : np.matrix or np.array
The data to predict.
"""
raise NotImplementedError()
def predict_label(self, X):
return self.predict_score(self.predict_proba(X))
def predict_score(self, scores):
return scores >= self.threshold()
def total_pos(self):
total = 0
for rix in range(self.shape()[0]):
if self.get_label(rix):
total += 1
return total
def use_csr(self):
"""Whether to use CSR instead of CSV to store the matrix."""
return True
def create_explainer(self):
return LIME()
def load(module, name):
"""Loads the given module and expects a class name derived from Model.
The class is created with the standard constructor.
"""
mod = importlib.import_module(module, __name__)
return getattr(mod, name)()