Changeset 68
- Timestamp:
- Jan 7, 2010, 9:02:10 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
proiecte/Parallel-DT/R8/Src/discr.c
r26 r68 6 6 /*************************************************************************/ 7 7 8 9 8 #include "buildex.i" 10 9 11 12 10 /*************************************************************************/ 13 11 /* */ … … 16 14 /*************************************************************************/ 17 15 18 19 EvalDiscreteAtt(Att, Fp, Lp, Items) 20 /* --------------- */ 21 Attribute Att; 22 ItemNo Fp, Lp; 23 ItemCount Items; 24 { 25 ItemCount KnownItems; 26 float DiscrKnownBaseInfo(), ComputeGain(), TotalInfo(); 27 28 ComputeFrequencies(Att, Fp, Lp); 29 30 KnownItems = Items - ValFreq[0]; 31 32 /* Special case when no known values of the attribute */ 33 34 if ( Items <= ValFreq[0] ) 35 { 36 Verbosity(2) printf("\tAtt %s: no known values\n", AttName[Att]); 37 38 Gain[Att] = -Epsilon; 39 Info[Att] = 0.0; 40 return; 41 } 42 43 Gain[Att] = ComputeGain(DiscrKnownBaseInfo(KnownItems, MaxAttVal[Att]), 44 UnknownRate[Att], MaxAttVal[Att], KnownItems); 45 Info[Att] = TotalInfo(ValFreq, 0, MaxAttVal[Att]) / Items; 46 47 Verbosity(2) 48 { 49 printf("\tAtt %s", AttName[Att]); 50 Verbosity(3) PrintDistribution(Att, MaxAttVal[Att], true); 51 printf("\tinf %.3f, gain %.3f\n", Info[Att], Gain[Att]); 52 } 53 54 } 55 56 57 16 EvalDiscreteAtt(Att, Fp, Lp, Items) 17 /* --------------- */ 18 Attribute Att;ItemNo Fp, Lp;ItemCount Items; { 19 ItemCount KnownItems; 20 float DiscrKnownBaseInfo(), ComputeGain(), TotalInfo(); 21 22 ComputeFrequencies(Att, Fp, Lp); 23 24 KnownItems = Items - ValFreq[0]; 25 26 /* Special case when no known values of the attribute */ 27 28 if (Items <= ValFreq[0]) { 29 Verbosity(2) 30 printf("\tAtt %s: no known values\n", AttName[Att]); 31 32 Gain[Att] = -Epsilon; 33 Info[Att] = 0.0; 34 return; 35 } 36 37 Gain[Att] = ComputeGain(DiscrKnownBaseInfo(KnownItems, MaxAttVal[Att]), 38 UnknownRate[Att], MaxAttVal[Att], KnownItems); 39 Info[Att] = TotalInfo(ValFreq, 0, MaxAttVal[Att]) / Items; 40 41 Verbosity(2) { 42 printf("\tAtt %s", AttName[Att]); 43 Verbosity(3) 44 PrintDistribution(Att, MaxAttVal[Att], true); 45 printf("\tinf %.3f, gain %.3f\n", Info[Att], Gain[Att]); 46 } 47 48 } 49 50 EvalDiscreteAtt_Discr(Att, Fp, Lp, Items, Freq, ValFreq, UnknownRate) 51 /* --------------- */ 52 Attribute Att;ItemNo Fp, Lp;ItemCount Items; ItemCount** Freq; ItemCount* ValFreq; float* UnknownRate; { 53 ItemCount KnownItems; 54 float DiscrKnownBaseInfo_Discr(), ComputeGain_Discr(), TotalInfo(); 55 56 ComputeFrequencies_Discr(Att, Fp, Lp, Freq, ValFreq, UnknownRate); 57 58 KnownItems = Items - ValFreq[0]; 59 60 /* Special case when no known values of the attribute */ 61 62 if (Items <= ValFreq[0]) { 63 Verbosity(2) 64 printf("\tAtt %s: no known values\n", AttName[Att]); 65 66 Gain[Att] = -Epsilon; 67 Info[Att] = 0.0; 68 return; 69 } 70 71 Gain[Att] = ComputeGain_Discr(DiscrKnownBaseInfo_Discr(KnownItems, MaxAttVal[Att], Freq), 72 UnknownRate[Att], MaxAttVal[Att], KnownItems, Freq, ValFreq); 73 Info[Att] = TotalInfo(ValFreq, 0, MaxAttVal[Att]) / Items; 74 75 Verbosity(2) { 76 printf("\tAtt %s", AttName[Att]); 77 Verbosity(3) 78 PrintDistribution_Discr(Att, MaxAttVal[Att], true, Freq); 79 printf("\tinf %.3f, gain %.3f\n", Info[Att], Gain[Att]); 80 } 81 82 } 58 83 /*************************************************************************/ 59 84 /* */ … … 63 88 /*************************************************************************/ 64 89 65 66 ComputeFrequencies(Att, Fp, Lp) 67 /* ------------------ */ 68 Attribute Att; 69 ItemNo Fp, Lp; 70 { 71 Description Case; 72 ClassNo c; 73 DiscrValue v; 74 ItemCount CountItems(); 75 ItemNo p; 76 77 ResetFreq(MaxAttVal[Att]); 78 79 /* Determine the frequency of each class amongst cases 80 with each possible value for the given attribute */ 81 82 ForEach(p, Fp, Lp) 83 { 84 Case = Item[p]; 85 Freq[ DVal(Case,Att) ][ Class(Case) ] += Weight[p]; 86 } 87 88 /* Determine the frequency of each possible value for the 89 given attribute */ 90 91 ForEach(v, 0, MaxAttVal[Att]) 92 { 93 ForEach(c, 0, MaxClass) 94 { 95 ValFreq[v] += Freq[v][c]; 96 } 97 } 98 99 /* Set the rate of unknown values of the attribute */ 100 101 UnknownRate[Att] = ValFreq[0] / CountItems(Fp, Lp); 102 } 103 104 105 90 ComputeFrequencies(Att, Fp, Lp) 91 /* ------------------ */ 92 Attribute Att;ItemNo Fp, Lp; { 93 Description Case; 94 ClassNo c; 95 DiscrValue v; 96 ItemCount CountItems(); 97 ItemNo p; 98 99 ResetFreq(MaxAttVal[Att], Freq, ValFreq); 100 101 /* Determine the frequency of each class amongst cases 102 with each possible value for the given attribute */ 103 104 #pragma omp parallel for private(p, Case) 105 ForEach(p, Fp, Lp) { 106 Case = Item[p]; 107 #pragma omp critical 108 Freq[DVal(Case,Att)][Class(Case)] += Weight[p]; 109 } 110 111 /* Determine the frequency of each possible value for the 112 given attribute */ 113 114 #pragma omp parallel for private(v) 115 ForEach(v, 0, MaxAttVal[Att]) { 116 ForEach(c, 0, MaxClass) { 117 ValFreq[v] += Freq[v][c]; 118 } 119 } 120 121 /* Set the rate of unknown values of the attribute */ 122 123 UnknownRate[Att] = ValFreq[0] / CountItems(Fp, Lp); 124 } 125 126 ComputeFrequencies_Discr(Att, Fp, Lp, Freq, ValFreq, UnknownRate) 127 /* ------------------ */ 128 Attribute Att;ItemNo Fp, Lp; ItemCount** Freq; ItemCount* ValFreq; float* UnknownRate;{ 129 Description Case; 130 ClassNo c; 131 DiscrValue v; 132 ItemCount CountItems(); 133 ItemNo p; 134 135 ResetFreq(MaxAttVal[Att], Freq, ValFreq); 136 137 /* Determine the frequency of each class amongst cases 138 with each possible value for the given attribute */ 139 140 ForEach(p, Fp, Lp) { 141 Case = Item[p]; 142 Freq[DVal(Case,Att)][Class(Case)] += Weight[p]; 143 } 144 145 /* Determine the frequency of each possible value for the 146 given attribute */ 147 148 ForEach(v, 0, MaxAttVal[Att]) { 149 ForEach(c, 0, MaxClass) { 150 ValFreq[v] += Freq[v][c]; 151 } 152 } 153 154 /* Set the rate of unknown values of the attribute */ 155 156 UnknownRate[Att] = ValFreq[0] / CountItems(Fp, Lp); 157 } 106 158 /*************************************************************************/ 107 159 /* */ … … 111 163 /*************************************************************************/ 112 164 113 114 165 float DiscrKnownBaseInfo(KnownItems, MaxVal) 115 /* ------------------ */ 116 DiscrValue MaxVal; 117 ItemCount KnownItems; 118 { 119 ClassNo c; 120 ItemCount ClassCount; 121 double Sum=0; 122 DiscrValue v; 123 124 ForEach(c, 0, MaxClass) 125 { 126 ClassCount = 0; 127 ForEach(v, 1, MaxVal) 128 { 129 ClassCount += Freq[v][c]; 130 } 131 Sum += ClassCount * Log(ClassCount); 132 } 133 134 return (KnownItems * Log(KnownItems) - Sum) / KnownItems; 135 } 136 137 166 /* ------------------ */ 167 DiscrValue MaxVal;ItemCount KnownItems; { 168 ClassNo c; 169 ItemCount ClassCount; 170 double Sum = 0; 171 DiscrValue v; 172 173 //#pragma omp parallel for 174 ForEach(c, 0, MaxClass) { 175 ClassCount = 0; 176 ForEach(v, 1, MaxVal) { 177 ClassCount += Freq[v][c]; 178 } 179 Sum += ClassCount * Log(ClassCount); 180 } 181 182 return (KnownItems * Log(KnownItems) - Sum) / KnownItems; 183 } 184 185 float DiscrKnownBaseInfo_Discr(KnownItems, MaxVal, Freq) 186 /* ------------------ */ 187 DiscrValue MaxVal;ItemCount KnownItems; ItemCount** Freq;{ 188 ClassNo c; 189 ItemCount ClassCount; 190 double Sum = 0; 191 DiscrValue v; 192 193 ForEach(c, 0, MaxClass) { 194 ClassCount = 0; 195 ForEach(v, 1, MaxVal) { 196 ClassCount += Freq[v][c]; 197 } 198 Sum += ClassCount * Log(ClassCount); 199 } 200 201 return (KnownItems * Log(KnownItems) - Sum) / KnownItems; 202 } 138 203 139 204 /*************************************************************************/ … … 143 208 /*************************************************************************/ 144 209 145 146 DiscreteTest(Node, Att) 147 /* ---------- */ 148 Tree Node; 149 Attribute Att; 150 { 151 ItemCount CountItems(); 152 153 Sprout(Node, MaxAttVal[Att]); 154 155 Node->NodeType = BrDiscr; 156 Node->Tested = Att; 157 Node->Errors = 0; 158 } 210 DiscreteTest(Node, Att) 211 /* ---------- */ 212 Tree Node;Attribute Att; { 213 ItemCount CountItems(); 214 215 Sprout(Node, MaxAttVal[Att]); 216 217 Node->NodeType = BrDiscr; 218 Node->Tested = Att; 219 Node->Errors = 0; 220 }
Note: See TracChangeset
for help on using the changeset viewer.