-
Notifications
You must be signed in to change notification settings - Fork 17
/
explanation.py
127 lines (108 loc) · 4.26 KB
/
explanation.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import string
import matplotlib.pyplot as plt
import numpy as np
from utils import InvalidExplanationMode
def id_generator(size=15, random_state=None):
chars = list(string.ascii_uppercase + string.digits)
return ''.join(random_state.choice(chars, size, replace=True))
class DomainMapper(object):
def __init__(self):
pass
def map_exp_ids(self, exp, **kwargs):
return exp
class Explanation(object):
def __init__(self,
domain_mapper,
mode='classification',
class_names=None,
random_state=None):
self.random_state = random_state
self.mode = mode
self.domain_mapper = domain_mapper
self.local_exp = {}
self.intercept = {}
self.score = {}
self.local_pred = {}
self.scaled_data = None
if mode == 'classification':
self.class_names = class_names
self.top_labels = None
self.predict_proba = None
elif mode == 'regression':
self.class_names = ['negative', 'positive']
self.predicted_value = None
self.min_value = 0.0
self.max_value = 1.0
self.dummy_label = 1
else:
raise InvalidExplanationMode('Invalid explanation mode "{}". '
'Should be either "classification" '
'or "regression".'.format(mode))
def available_labels(self):
try:
assert self.mode == "classification"
except AssertionError:
raise NotImplementedError('Not supported for regression explanations.')
else:
ans = self.top_labels if self.top_labels else self.local_exp.keys()
return list(ans)
def as_list_one(self, label=1, **kwargs):
label_to_use = label if self.mode == "classification" else self.dummy_label
ans = self.domain_mapper.map_exp_ids(self.local_exp[label_to_use], **kwargs)
return ans
def as_list_zero(self, label=0, **kwargs):
label_to_use = label if self.mode == "classification" else self.dummy_label
ans = self.domain_mapper.map_exp_ids(self.local_exp[label_to_use], **kwargs)
return ans
def as_map(self):
return self.local_exp
def as_pyplot_figure(self, label=0, type='h', **kwargs):
exp = self.as_list(label=label, **kwargs)
fig = plt.figure()
vals = [x[1] for x in exp]
names = [x[0] for x in exp]
vals.reverse()
names.reverse()
colors = ['green' if x > 0 else 'red' for x in vals]
pos = np.arange(len(exp)) + .5
if type == 'h':
plt.barh(pos, vals, align='center', color=colors)
plt.yticks(pos, names)
else:
plt.bar(pos, vals, align='center', color=colors)
plt.xticks(pos, names, rotation=90)
if self.mode == "classification":
title = 'Local explanation for class %s' % self.class_names[label]
else:
title = 'Local explanation'
plt.title(title)
return fig, names
def as_pyplot_to_figure(self, type='h', name = None, label='0', lp=None, **kwargs):
if label == '0':
exp = self.as_list_zero(label=0, **kwargs)
else:
exp = self.as_list_one(label=1, **kwargs)
vals = [x[1] for x in exp]
names = [x[0] for x in exp]
vals.reverse()
names.reverse()
colors = ['green' if x > 0 else 'red' for x in vals]
#pos = np.arange(len(exp)) + .2
pos = np.arange(len(exp)) / 3
fig = plt.figure(figsize=(4, 2))
if type == 'h':
plt.barh(pos, vals, align='center', color=colors, height=0.2)
plt.yticks(pos, names)
else:
plt.bar(pos, vals, align='center', color=colors)
plt.xticks(pos, names, rotation=90)
if self.mode == "classification":
title = 'Local explanation for class: %s' % self.class_names[int(label)]
else:
title = 'Local explanation'
plt.title(title)
#plt.suptitle('Sup title', y=1.05, fontsize=18)
#plt.savefig(str(name) + ".png")
filename= 'results/' + str(name)+".pdf"
plt.savefig(filename, bbox_inches='tight')
return fig, names