[26] | 1 | /*************************************************************************/ |
---|
| 2 | /* */ |
---|
| 3 | /* Determine the class of a case description from a decision tree */ |
---|
| 4 | /* -------------------------------------------------------------- */ |
---|
| 5 | /* */ |
---|
| 6 | /*************************************************************************/ |
---|
| 7 | |
---|
| 8 | |
---|
| 9 | #include "defns.i" |
---|
| 10 | #include "types.i" |
---|
| 11 | #include "extern.i" |
---|
| 12 | |
---|
| 13 | float *ClassSum=Nil; /* ClassSum[c] = total weight of class c */ |
---|
| 14 | |
---|
| 15 | |
---|
| 16 | |
---|
| 17 | /*************************************************************************/ |
---|
| 18 | /* */ |
---|
| 19 | /* Categorize a case description using the given decision tree */ |
---|
| 20 | /* */ |
---|
| 21 | /*************************************************************************/ |
---|
| 22 | |
---|
| 23 | |
---|
| 24 | ClassNo Category(CaseDesc, DecisionTree) |
---|
| 25 | /* -------- */ |
---|
| 26 | Description CaseDesc; |
---|
| 27 | Tree DecisionTree; |
---|
| 28 | { |
---|
| 29 | ClassNo c, BestClass; |
---|
| 30 | |
---|
| 31 | if ( ! ClassSum ) |
---|
| 32 | { |
---|
| 33 | ClassSum = (float *) malloc((MaxClass+1) * sizeof(float)); |
---|
| 34 | } |
---|
| 35 | |
---|
| 36 | ForEach(c, 0, MaxClass) |
---|
| 37 | { |
---|
| 38 | ClassSum[c] = 0; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | Classify(CaseDesc, DecisionTree, 1.0); |
---|
| 42 | |
---|
| 43 | BestClass = 0; |
---|
| 44 | ForEach(c, 0, MaxClass) |
---|
| 45 | { |
---|
| 46 | Verbosity(5) printf("class %s weight %.2f\n", ClassName[c], ClassSum[c]); |
---|
| 47 | |
---|
| 48 | if ( ClassSum[c] > ClassSum[BestClass] ) BestClass = c; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | return BestClass; |
---|
| 52 | } |
---|
| 53 | |
---|
| 54 | |
---|
| 55 | |
---|
| 56 | /*************************************************************************/ |
---|
| 57 | /* */ |
---|
| 58 | /* Classify a case description using the given subtree by adjusting */ |
---|
| 59 | /* the value ClassSum for each class */ |
---|
| 60 | /* */ |
---|
| 61 | /*************************************************************************/ |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | Classify(CaseDesc, T, Weight) |
---|
| 65 | /* -------- */ |
---|
| 66 | Description CaseDesc; |
---|
| 67 | Tree T; |
---|
| 68 | float Weight; |
---|
| 69 | { |
---|
| 70 | DiscrValue v, dv; |
---|
| 71 | float Cv; |
---|
| 72 | Attribute a; |
---|
| 73 | ClassNo c; |
---|
| 74 | |
---|
| 75 | switch ( T->NodeType ) |
---|
| 76 | { |
---|
| 77 | case 0: /* leaf */ |
---|
| 78 | |
---|
| 79 | if ( T->Items > 0 ) |
---|
| 80 | { |
---|
| 81 | /* Update from ALL classes */ |
---|
| 82 | |
---|
| 83 | ForEach(c, 0, MaxClass) |
---|
| 84 | { |
---|
| 85 | if ( T->ClassDist[c] ) |
---|
| 86 | { |
---|
| 87 | ClassSum[c] += Weight * T->ClassDist[c] / T->Items; |
---|
| 88 | } |
---|
| 89 | } |
---|
| 90 | } |
---|
| 91 | else |
---|
| 92 | { |
---|
| 93 | ClassSum[T->Leaf] += Weight; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | return; |
---|
| 97 | |
---|
| 98 | case BrDiscr: /* test of discrete attribute */ |
---|
| 99 | |
---|
| 100 | a = T->Tested; |
---|
| 101 | v = DVal(CaseDesc, a); |
---|
| 102 | |
---|
| 103 | if ( v && v <= T->Forks ) /* Make sure not new discrete value */ |
---|
| 104 | { |
---|
| 105 | Classify(CaseDesc, T->Branch[v], Weight); |
---|
| 106 | } |
---|
| 107 | else |
---|
| 108 | { |
---|
| 109 | ForEach(v, 1, T->Forks) |
---|
| 110 | { |
---|
| 111 | Classify(CaseDesc, T->Branch[v], |
---|
| 112 | (Weight * T->Branch[v]->Items) / T->Items); |
---|
| 113 | } |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | return; |
---|
| 117 | |
---|
| 118 | case ThreshContin: /* test of continuous attribute */ |
---|
| 119 | |
---|
| 120 | a = T->Tested; |
---|
| 121 | Cv = CVal(CaseDesc, a); |
---|
| 122 | |
---|
| 123 | if ( Cv == Unknown ) |
---|
| 124 | { |
---|
| 125 | ForEach(v, 1, 2) |
---|
| 126 | { |
---|
| 127 | Classify(CaseDesc, T->Branch[v], |
---|
| 128 | (Weight * T->Branch[v]->Items) / T->Items); |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | else |
---|
| 132 | { |
---|
| 133 | v = ( Cv <= T->Cut ? 1 : 2 ); |
---|
| 134 | Classify(CaseDesc, T->Branch[v], Weight); |
---|
| 135 | } |
---|
| 136 | |
---|
| 137 | return; |
---|
| 138 | |
---|
| 139 | case BrSubset: /* subset test on discrete attribute */ |
---|
| 140 | |
---|
| 141 | a = T->Tested; |
---|
| 142 | dv = DVal(CaseDesc, a); |
---|
| 143 | |
---|
| 144 | if ( dv ) |
---|
| 145 | { |
---|
| 146 | ForEach(v, 1, T->Forks) |
---|
| 147 | { |
---|
| 148 | if ( In(dv, T->Subset[v]) ) |
---|
| 149 | { |
---|
| 150 | Classify(CaseDesc, T->Branch[v], Weight); |
---|
| 151 | |
---|
| 152 | return; |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /* Value unknown or not found in any of the subsets */ |
---|
| 158 | |
---|
| 159 | ForEach(v, 1, T->Forks) |
---|
| 160 | { |
---|
| 161 | Classify(CaseDesc, T->Branch[v], |
---|
| 162 | (Weight * T->Branch[v]->Items) / T->Items); |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | return; |
---|
| 166 | } |
---|
| 167 | } |
---|