[26] | 1 | /*************************************************************************/ |
---|
| 2 | /* */ |
---|
| 3 | /* Soften thresholds for continuous attributes */ |
---|
| 4 | /* ------------------------------------------- */ |
---|
| 5 | /* */ |
---|
| 6 | /*************************************************************************/ |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | #include "defns.i" |
---|
| 10 | #include "types.i" |
---|
| 11 | #include "extern.i" |
---|
| 12 | |
---|
| 13 | |
---|
| 14 | Boolean *LHSErr, /* Does a misclassification occur with this value of an att */ |
---|
| 15 | *RHSErr; /* if the below or above threshold branches are taken */ |
---|
| 16 | |
---|
| 17 | ItemNo *ThreshErrs; /* ThreshErrs[i] is the no. of misclassifications if thresh is i */ |
---|
| 18 | |
---|
| 19 | float *CVals; /* All values of a continuous attribute */ |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | #define Below(v,t) (v <= t + 1E-6) |
---|
| 23 | |
---|
| 24 | |
---|
| 25 | /*************************************************************************/ |
---|
| 26 | /* */ |
---|
| 27 | /* Soften all thresholds for continuous attributes in tree T */ |
---|
| 28 | /* */ |
---|
| 29 | /*************************************************************************/ |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | SoftenThresh(T) |
---|
| 33 | /* ------------ */ |
---|
| 34 | Tree T; |
---|
| 35 | { |
---|
| 36 | CVals = (float *) calloc(MaxItem+1, sizeof(float)); |
---|
| 37 | LHSErr = (Boolean *) calloc(MaxItem+1, sizeof(Boolean)); |
---|
| 38 | RHSErr = (Boolean *) calloc(MaxItem+1, sizeof(Boolean)); |
---|
| 39 | ThreshErrs = (ItemNo *) calloc(MaxItem+1, sizeof(ItemNo)); |
---|
| 40 | |
---|
| 41 | InitialiseWeights(); |
---|
| 42 | |
---|
| 43 | ScanTree(T, 0, MaxItem); |
---|
| 44 | |
---|
| 45 | cfree(ThreshErrs); |
---|
| 46 | cfree(RHSErr); |
---|
| 47 | cfree(LHSErr); |
---|
| 48 | cfree(CVals); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | /*************************************************************************/ |
---|
| 54 | /* */ |
---|
| 55 | /* Calculate upper and lower bounds for each test on a continuous */ |
---|
| 56 | /* attribute in tree T, using data items from Fp to Lp */ |
---|
| 57 | /* */ |
---|
| 58 | /*************************************************************************/ |
---|
| 59 | |
---|
| 60 | |
---|
| 61 | ScanTree(T, Fp, Lp) |
---|
| 62 | /* -------- */ |
---|
| 63 | Tree T; |
---|
| 64 | ItemNo Fp, Lp; |
---|
| 65 | { |
---|
| 66 | short v; |
---|
| 67 | float Val, Se, Limit, Lower, Upper, GreatestValueBelow(); |
---|
| 68 | ItemNo i, Kp, Ep, LastI, Errors, BaseErrors; |
---|
| 69 | ClassNo CaseClass, Class1, Class2, Category(); |
---|
| 70 | Boolean LeftThresh=false; |
---|
| 71 | Description CaseDesc; |
---|
| 72 | Attribute Att; |
---|
| 73 | void Swap(); |
---|
| 74 | |
---|
| 75 | /* Stop when get to a leaf */ |
---|
| 76 | |
---|
| 77 | if ( ! T->NodeType ) return; |
---|
| 78 | |
---|
| 79 | /* Group the unknowns together */ |
---|
| 80 | |
---|
| 81 | Kp = Group(0, Fp, Lp, T); |
---|
| 82 | |
---|
| 83 | /* Soften a threshold for a continuous attribute */ |
---|
| 84 | |
---|
| 85 | Att = T->Tested; |
---|
| 86 | |
---|
| 87 | if ( T->NodeType == ThreshContin ) |
---|
| 88 | { |
---|
| 89 | printf("\nTest %s <> %g\n", AttName[Att], T->Cut); |
---|
| 90 | |
---|
| 91 | Quicksort(Kp+1, Lp, Att, Swap); |
---|
| 92 | |
---|
| 93 | ForEach(i, Kp+1, Lp) |
---|
| 94 | { |
---|
| 95 | /* See how this item would be classified if its |
---|
| 96 | value were on each side of the threshold */ |
---|
| 97 | |
---|
| 98 | CaseDesc = Item[i]; |
---|
| 99 | CaseClass = Class(CaseDesc); |
---|
| 100 | Val = CVal(CaseDesc, Att); |
---|
| 101 | |
---|
| 102 | Class1 = Category(CaseDesc, T->Branch[1]); |
---|
| 103 | Class2 = Category(CaseDesc, T->Branch[2]); |
---|
| 104 | |
---|
| 105 | CVals[i] = Val; |
---|
| 106 | LHSErr[i] = (Class1 != CaseClass ? 1 : 0); |
---|
| 107 | RHSErr[i] = (Class2 != CaseClass ? 1 : 0); |
---|
| 108 | } |
---|
| 109 | |
---|
| 110 | /* Set Errors to total errors if take above thresh branch, |
---|
| 111 | and BaseErrors to errors if threshold has original value */ |
---|
| 112 | |
---|
| 113 | Errors = BaseErrors = 0; |
---|
| 114 | ForEach(i, Kp+1, Lp) |
---|
| 115 | { |
---|
| 116 | Errors += RHSErr[i]; |
---|
| 117 | |
---|
| 118 | if ( Below(CVals[i], T->Cut) ) |
---|
| 119 | { |
---|
| 120 | BaseErrors += LHSErr[i]; |
---|
| 121 | } |
---|
| 122 | else |
---|
| 123 | { |
---|
| 124 | BaseErrors += RHSErr[i]; |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | |
---|
| 128 | /* Calculate standard deviation of the number of errors */ |
---|
| 129 | |
---|
| 130 | Se = sqrt( (BaseErrors+0.5) * (Lp-Kp-BaseErrors+0.5) / (Lp-Kp+1) ); |
---|
| 131 | Limit = BaseErrors + Se; |
---|
| 132 | |
---|
| 133 | Verbosity(1) |
---|
| 134 | { |
---|
| 135 | printf("\t\t\tBase errors %d, items %d, se=%.1f\n", |
---|
| 136 | BaseErrors, Lp-Kp, Se); |
---|
| 137 | printf("\n\tVal <= Errors\t\t+Errors\n"); |
---|
| 138 | printf("\t %6d\n", Errors); |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | /* Set ThreshErrs[i] to the no. of errors if the threshold were i */ |
---|
| 142 | |
---|
| 143 | ForEach(i, Kp+1, Lp) |
---|
| 144 | { |
---|
| 145 | ThreshErrs[i] = Errors = Errors + LHSErr[i] - RHSErr[i]; |
---|
| 146 | |
---|
| 147 | if ( i == Lp || CVals[i] != CVals[i+1] ) |
---|
| 148 | { |
---|
| 149 | Verbosity(1) |
---|
| 150 | printf("\t%6g %6d\t\t%7d\n", |
---|
| 151 | CVals[i], Errors, Errors - BaseErrors); |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | /* Choose Lower and Upper so that if threshold were set to |
---|
| 156 | either, the number of items misclassified would be one |
---|
| 157 | standard deviation above BaseErrors */ |
---|
| 158 | |
---|
| 159 | LastI = Kp+1; |
---|
| 160 | Lower = Min(T->Cut, CVals[LastI]); |
---|
| 161 | Upper = Max(T->Cut, CVals[Lp]); |
---|
| 162 | while ( CVals[LastI+1] == CVals[LastI] ) LastI++; |
---|
| 163 | |
---|
| 164 | while ( LastI < Lp ) |
---|
| 165 | { |
---|
| 166 | i = LastI + 1; |
---|
| 167 | while ( i < Lp && CVals[i+1] == CVals[i] ) i++; |
---|
| 168 | |
---|
| 169 | if ( ! LeftThresh && |
---|
| 170 | ThreshErrs[LastI] > Limit && |
---|
| 171 | ThreshErrs[i] <= Limit && |
---|
| 172 | Below(CVals[i], T->Cut) ) |
---|
| 173 | { |
---|
| 174 | Lower = CVals[i] - |
---|
| 175 | (CVals[i] - CVals[LastI]) * (Limit - ThreshErrs[i]) / |
---|
| 176 | (ThreshErrs[LastI] - ThreshErrs[i]); |
---|
| 177 | LeftThresh = true; |
---|
| 178 | } |
---|
| 179 | else |
---|
| 180 | if ( ThreshErrs[LastI] <= Limit && |
---|
| 181 | ThreshErrs[i] > Limit && |
---|
| 182 | ! Below(CVals[i], T->Cut) ) |
---|
| 183 | { |
---|
| 184 | Upper = CVals[LastI] + |
---|
| 185 | (CVals[i] - CVals[LastI]) * (Limit - ThreshErrs[LastI]) / |
---|
| 186 | (ThreshErrs[i] - ThreshErrs[LastI]); |
---|
| 187 | if ( Upper < T->Cut ) Upper = T->Cut; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | LastI = i; |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | T->Lower = Lower; |
---|
| 194 | T->Upper = Upper; |
---|
| 195 | |
---|
| 196 | Verbosity(1) printf("\n"); |
---|
| 197 | |
---|
| 198 | printf("\tLower = %g, Upper = %g\n", T->Lower, T->Upper); |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /* Recursively scan each branch */ |
---|
| 202 | |
---|
| 203 | ForEach(v, 1, T->Forks) |
---|
| 204 | { |
---|
| 205 | Ep = Group(v, Kp+1, Lp, T); |
---|
| 206 | |
---|
| 207 | if ( Kp < Ep ) |
---|
| 208 | { |
---|
| 209 | ScanTree(T->Branch[v], Kp+1, Ep); |
---|
| 210 | Kp = Ep; |
---|
| 211 | } |
---|
| 212 | } |
---|
| 213 | } |
---|