-
Notifications
You must be signed in to change notification settings - Fork 2
/
MIRCO.py
444 lines (355 loc) · 15.6 KB
/
MIRCO.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
# -*- coding: utf-8 -*-
import numpy as np
import gurobipy as gp
from gurobipy import GRB
from sklearn.ensemble import RandomForestClassifier
from scipy.sparse import csr_matrix
from joblib import Parallel, delayed, cpu_count
class MIRCOfit:
def __init__(self, CorR = 'C'):
self.rules = dict()
self.CorR = CorR # 'C' for classification, 'R' for regression
self.numOfMissed = 0
self.missedXvals = []
self.initNumOfRules = 0
self.numOfRules = 0
def predict(self, xvals):
# Parallel prediction
p = cpu_count()
xsets = np.array_split(xvals, p)
chunkPreds = Parallel(n_jobs=p, prefer="threads")(
delayed(self.chunkPredict)(x0) for x0 in xsets)
if (self.CorR == 'C'):
predictions = np.empty(shape=(0), dtype=int)
else:
predictions = np.empty(shape=(0), dtype=float)
for indx in range(len(chunkPreds)):
predictions = np.append(predictions, chunkPreds[indx]['predictions'])
for x0 in chunkPreds[indx]['missedXvals']:
self.missedXvals.append(x0)
self.numOfMissed += chunkPreds[indx]['numOfMissed']
# DEBUG:
# if (self.numOfMissed > 0):
# print('Warning...')
# print('Total number of missed points:' + str(self.numOfMissed))
return predictions
def chunkPredict(self, xvals):
chunkPreds = dict()
if (self.CorR == 'C'):
chunkPreds['predictions'] = np.zeros(len(xvals), dtype=int)
else:
chunkPreds['predictions'] = np.zeros(len(xvals), dtype=float)
chunkPreds['numOfMissed'] = 0
chunkPreds['missedXvals'] = []
for sindx, x0 in enumerate(xvals):
totvals = np.zeros(len(self.rules[0][-1]), dtype=float)
approxvals = np.zeros(len(self.rules[0][-1]), dtype=float)
totnum, approxnum = 0, 0
trueratios = np.zeros(len(self.rules))
for rindx, rule in enumerate(self.rules.values()):
truecount = 0
# The last value in the list stands for
# the numbers in each class
for clause in rule[:-1]:
if (clause[1] == 'l'):
if (x0[clause[0]] <= clause[2]):
truecount = truecount + 1
if (clause[1] == 'r'):
if (x0[clause[0]] > clause[2]):
truecount = truecount + 1
# Not the last one (class numbers)
trueratios[rindx] = truecount/(len(rule)-1)
if (trueratios[rindx] == 1.0):
totvals += rule[-1]
totnum += 1
else:
approxvals += trueratios[rindx]*rule[-1]
approxnum += 1
# TODO: We may return the prediction probabilities
if (sum(totvals) > 0.0):
if (self.CorR == 'C'):
chunkPreds['predictions'][sindx] = np.argmax(totvals)
else:
chunkPreds['predictions'][sindx] = (1.0/totnum)*totvals
else:
if (self.CorR == 'C'):
chunkPreds['predictions'][sindx] = np.argmax(approxvals)
else:
chunkPreds['predictions'][sindx] = (1.0/approxnum)*approxvals
chunkPreds['missedXvals'].append(x0)
chunkPreds['numOfMissed'] += 1
return chunkPreds
def exportRules(self):
for rindx, rule in enumerate(self.rules.values()):
print('RULE %d:' % rindx)
# Last compenent stores the numbers in each class
for clause in rule[:-1]:
if (clause[1] == 'l'):
print('==> x[%d] <= %.2f' % (clause[0], clause[2]))
if (clause[1] == 'r'):
print('==> x[%d] > %.2f' % (clause[0], clause[2]))
strarray = '['
for cn in rule[-1][0:-1]:
strarray += ('{0:.2f}'.format(cn) + ', ')
strarray += ('{0:.2f}'.format(rule[-1][-1]) + ']')
print('==> Class numbers: %s' % strarray)
class MIRCO:
def __init__(self, rf, solver='heu'):
# rf is a fitted Random Forest!
self.rf = rf
self.solver = solver
self.estimator = None
self.featureNames = None
def getRule(self, fitTree, nodeid):
left = fitTree.tree_.children_left
right = fitTree.tree_.children_right
threshold = fitTree.tree_.threshold
featurenames = [self.featureNames[i] for i in fitTree.tree_.feature]
def recurse(left, right, child, lineage=None):
if lineage is None:
lineage = [child]
if child in left:
parent = np.where(left == child)[0].item()
split = 'l'
else:
parent = np.where(right == child)[0].item()
split = 'r'
# The first in the list shows the feature index
lineage.append((fitTree.tree_.feature[parent], split,
threshold[parent], featurenames[parent]))
if parent == 0:
lineage.reverse()
return lineage
else:
return recurse(left, right, parent, lineage)
rule = recurse(left, right, nodeid)
# Weighted values for each class in leaf comes from tree_
# These are later filled with actual numbers instead of weights
rule[-1] = fitTree.tree_.value[nodeid][0]
return rule
def greedySCP(self, c, A):
# TODO: Can be faster by using heaps
# Mathematical model
# minimize c'x
# subject to Ax >= 1
# x in {0,1}
# c: n x 1
# A: m x n
# number of rows and number of columns
m, n = A.shape
# set of rows (items)
M = set(range(m))
# set of columns (sets)
N = set(range(n))
R = M
S = set()
while (len(R) > 0):
minratio = np.Inf
for j in N.difference(S):
# Sum of covered rows by column j
denom = np.sum(A[list(R), j])
if (denom == 0):
continue
ratio = c[j]/denom
if (ratio < minratio):
minratio = ratio
jstar = j
column = A[:, jstar]
Mjstar = set(np.where(column.toarray() == 1)[0])
R = R.difference(Mjstar)
S.add(jstar)
listS = list(S)
# Sort indices
sindx = list(np.argsort(c[listS]))
S = set()
totrow = np.zeros((m, 1), dtype=np.int32)
for i in sindx:
S.add(listS[i])
column = A[:, listS[i]]
totrow = totrow + column
if (np.sum(totrow > 0) >= m):
break
return S
def greedySCPVectorized(self, c, A):
# Acknowledgement:
# This vectorized version of the greedy heuristic
# is prepared by HUW THOMAS (September, 2020)
#
# TODO: Can be faster by using heaps
# Mathematical model
# minimize c'x
# subject to Ax >= 1
# x in {0,1}
# c: n x 1
# A: m x n
# number of rows and number of columns
m, n = A.shape
# set of rows (items)
M = set(range(m))
# set of columns (sets)
N = set(range(n))
R = M
S = set()
while (len(R) > 0):
##Ignore divide by zero errors - the result is inf so these values
#are never considered as candidates anyway.
with np.errstate(divide='ignore',invalid='ignore'):
ratios = c[list(N.difference(S))] / \
np.sum(A[list(R), :][:,list(N.difference(S))],0)
ratios = np.squeeze(np.asarray(ratios))
jstar = list(N.difference(S))[np.argmin(ratios)]
column = A[:, jstar]
Mjstar = set(np.where(column.toarray() == 1)[0])
R = R.difference(Mjstar)
S.add(jstar)
listS = list(S)
# Sort indices
sindx = list(np.argsort(c[listS]))
S = set()
totrow = np.zeros((m, 1), dtype=np.int32)
for i in sindx:
S.add(listS[i])
column = A[:, listS[i]]
totrow = totrow + column
if (np.sum(totrow > 0) >= m):
break
return S
def solveSCP(self, c, A, solver):
# Number of rows and number of columns
m, n = np.shape(A)
S = self.greedySCPVectorized(c, A)
S = np.array(list(S), dtype=np.long)
# The results in the paper are reported with the greeedy heuristic
# The following are two options that require solving
# integer programming (IP) problems:
# "app" solves an IP problem over the columns obtained with the greedy heuristic
# "opt" sovles an IP orıblem over all columns
if (solver == "app" or solver == "opt"):
modelopt = gp.Model("SCP")
rhs = np.ones(m)
if (solver == "app"):
# Only the columns in S
A = A[:, S]
c = c[S]
xopt = modelopt.addMVar(shape=int(len(S)),
vtype=GRB.BINARY, name="xopt")
for i in range(len(S)):
xopt[i].start = 1
else:
# All columns
xopt = modelopt.addMVar(shape=int(n),
vtype=GRB.BINARY, name="xopt")
for i in S:
xopt[i].start = 1
modelopt.setObjective(c.T @ xopt, GRB.MINIMIZE)
modelopt.addConstr(A @ xopt >= rhs, name="constraints")
modelopt.optimize()
if (solver == "app"):
S = S[np.where(xopt.X == 1)[0]]
else:
S = np.where(xopt.X == 1)[0]
return S
def fit(self, X, y):
if (isinstance(self.rf, RandomForestClassifier)):
fittedMIRCO = MIRCOfit(CorR = 'C')
else:
fittedMIRCO = MIRCOfit(CorR = 'R')
nOfSamples, nOfFeatures = np.shape(X)
nOfClasses = int(max(y) + 1) # classes start with 0
self.featureNames = ['x[' + str(indx) + ']'
for indx in range(nOfFeatures)]
criterion = self.rf.criterion
# Total number of rules
nOfRules = 0
for fitTree in self.rf.estimators_:
nOfRules += fitTree.get_n_leaves()
# Initial number of rules is stored
fittedMIRCO.initNumOfRules = nOfRules
# Parallel construction of SCP matrices
p = cpu_count()
estsets = np.array_split(self.rf.estimators_, p)
retdicts = Parallel(n_jobs=p, prefer="threads")(
delayed(self.chunkFit)(X, y, est, criterion, fittedMIRCO.CorR)
for chunkNo, est in enumerate(estsets))
c = np.empty(shape=(0), dtype=np.float)
rows = np.empty(shape=(0), dtype=np.int32)
cols = np.empty(shape=(0), dtype=np.int32)
colTreeNos = np.empty(shape=(0), dtype=np.int32)
colLeafNos = np.empty(shape=(0), dtype=np.int32)
colChunkNos = np.empty(shape=(0), dtype=np.int32)
colno = 0
for chunkNo in range(len(estsets)):
ncols = len(retdicts[chunkNo]['c'])
c = np.hstack((c, retdicts[chunkNo]['c']))
rows = np.hstack((rows, retdicts[chunkNo]['rows']))
colTreeNos = np.hstack((colTreeNos, retdicts[chunkNo]['colTreeNos']))
colLeafNos = np.hstack((colLeafNos, retdicts[chunkNo]['colLeafNos']))
tempcols = colno + retdicts[chunkNo]['cols']
cols = np.hstack((cols, tempcols))
colChunkNos = np.hstack((colChunkNos, np.ones(ncols,
dtype=np.int8)*chunkNo))
colno = cols[-1]+1
data = np.ones(len(rows), dtype=np.int8)
A = csr_matrix((data, (rows, cols)), dtype=np.int8)
S = self.solveSCP(c, A, self.solver)
for indx, col in enumerate(S):
chunkno = colChunkNos[col]
treeno = colTreeNos[col]
fitTree = estsets[chunkno][treeno]
leafno = colLeafNos[col]
rule = self.getRule(fitTree, leafno)
fittedMIRCO.rules[indx] = rule
# Filling the last element in 'rule'
# with actual numbers in each class
# not the weighted numbers - Though,
# we do not use weights for MIRCO
y_rules = fitTree.apply(X)
covers = np.where(y_rules == leafno)
leafyvals = y[covers] # yvals of the samples in the leaf
unique, counts = np.unique(leafyvals, return_counts=True)
numsinclasses = np.zeros(nOfClasses)
for ix, i in enumerate(unique):
numsinclasses[int(i)] = counts[ix]
fittedMIRCO.rules[indx][-1] = numsinclasses
fittedMIRCO.numOfRules = len(S)
return fittedMIRCO
def chunkFit(self, X, y, estimators, criterion, CorR):
numRules = 0
for fitTree in estimators:
numRules += fitTree.get_n_leaves()
retdict = dict()
retdict['c'] = np.zeros(numRules, dtype=np.float)
retdict['rows'] = np.empty(shape=(0), dtype=np.int32)
retdict['cols'] = np.empty(shape=(0), dtype=np.int32)
retdict['colLeafNos'] = np.zeros(numRules, dtype=np.int32)
retdict['colTreeNos'] = np.zeros(numRules, dtype=np.int32)
col = 0
for treeno, fitTree in enumerate(estimators):
# Tells us which sample is in which leaf
y_rules = fitTree.apply(X)
for leafno in np.unique(y_rules):
covers = np.where(y_rules == leafno)[0]
retdict['rows'] = np.hstack((retdict['rows'], covers))
retdict['cols'] = np.hstack((retdict['cols'],
np.ones(len(covers), dtype=np.int8)*col))
leafyvals = np.array(y[covers]) # y values of the samples in the leaf
if (CorR == 'C'): # classification
unique, counts = np.unique(leafyvals, return_counts=True)
probs = counts/np.sum(counts)
# Currently it is just Gini and Entropy
# TODO: Add other criteria
if (criterion == 'gini'):
retdict['c'][col] = 1 + (1 - np.sum(probs**2)) # 1 + Gini
else:
retdict['c'][col] = 1 + (-np.dot(probs, np.log2(probs))) # 1 + Entropy
else: # regression
# Currently it is just MSE
# TODO: Add other criteria
leafyavg = np.average(leafyvals)
mse = np.average(np.square(leafyavg - leafyvals))
if (criterion == 'mse'):
retdict['c'][col] = 1.0 + mse # 1 + MSE
retdict['colLeafNos'][col] = leafno
retdict['colTreeNos'][col] = treeno
col += 1
return retdict