1
2
3
4
5
6 """ functionality to allow adjusting composite model contents
7
8 """
9 from __future__ import print_function
10 import numpy
11 import copy
12
14 """ adjusts the contents of the composite model so as to maximize
15 the weighted classification accuracty across the two data sets.
16
17 The resulting composite model, with _targetSize_ models, is returned.
18
19 **Notes**:
20
21 - if _names1_ and _names2_ are not provided, _set1_ and _set2_ should
22 have the same ordering of columns and _model_ should have already
23 have had _SetInputOrder()_ called.
24
25 """
26
27
28
29
30
31 S1 = len(set1)
32 S2 = len(set2)
33 weight1 = float(S1+S2)*(1-weight)/S1
34 weight2 = float(S1+S2)*weight/S2
35
36
37
38 res = copy.copy(model)
39 res.modelList = []
40 res.errList = []
41 res.countList = []
42 res.quantizationRequirements = []
43
44 startSize = len(model)
45 scores = numpy.zeros(startSize,numpy.float)
46 actQuantBounds = model.GetActivityQuantBounds()
47 if names1 is not None:
48 model.SetInputOrder(names1)
49 for pt in set1:
50 pred,conf = model.ClassifyExample(pt)
51 if actQuantBounds:
52 ans = model.QuantizeActivity(pt)[-1]
53 else:
54 ans = pt[-1]
55 votes = model.GetVoteDetails()
56 for i in range(startSize):
57 if votes[i]==ans: scores[i] += weight1
58 if names2 is not None:
59 model.SetInputOrder(names2)
60 for pt in set2:
61 pred,conf = model.ClassifyExample(pt)
62 if actQuantBounds:
63 ans = model.QuantizeActivity(pt)[-1]
64 else:
65 ans = pt[-1]
66 votes = model.GetVoteDetails()
67 for i in range(startSize):
68 if votes[i]==ans: scores[i] += weight2
69
70 nPts = S1+S2
71 scores /= nPts
72
73 bestOrder = list(numpy.argsort(scores))
74 bestOrder.reverse()
75 print('\tTAKE:',bestOrder[:targetSize])
76
77 for i in range(targetSize):
78 idx = bestOrder[i]
79 mdl = model.modelList[idx]
80 res.modelList.append(mdl)
81 res.errList.append(1.-scores[idx])
82 res.countList.append(1)
83
84 res.quantizationRequirements.append(0)
85 return res
86