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