source: proiecte/Parallel-DT/R8/Src/discr.c @ 103

Last change on this file since 103 was 103, checked in by (none), 14 years ago
File size: 6.0 KB
RevLine 
[26]1/*************************************************************************/
2/*                                                                       */
3/*      Evaluation of a test on a discrete valued attribute              */
4/*      ---------------------------------------------------              */
5/*                                                                       */
6/*************************************************************************/
7
8#include "buildex.i"
9
10/*************************************************************************/
11/*                                                                       */
12/*  Set Info[] and Gain[] for discrete partition of items Fp to Lp       */
13/*                                                                       */
14/*************************************************************************/
15
[68]16EvalDiscreteAtt(Att, Fp, Lp, Items)
17        /*  ---------------  */
18        Attribute Att;ItemNo Fp, Lp;ItemCount Items; {
19        ItemCount KnownItems;
20        float DiscrKnownBaseInfo(), ComputeGain(), TotalInfo();
[26]21
[68]22        ComputeFrequencies(Att, Fp, Lp);
[26]23
[68]24        KnownItems = Items - ValFreq[0];
[26]25
[68]26        /*  Special case when no known values of the attribute  */
[26]27
[68]28        if (Items <= ValFreq[0]) {
29                Verbosity(2)
30                        printf("\tAtt %s: no known values\n", AttName[Att]);
[26]31
[68]32                Gain[Att] = -Epsilon;
33                Info[Att] = 0.0;
34                return;
35        }
[26]36
[68]37        Gain[Att] = ComputeGain(DiscrKnownBaseInfo(KnownItems, MaxAttVal[Att]),
38                        UnknownRate[Att], MaxAttVal[Att], KnownItems);
39        Info[Att] = TotalInfo(ValFreq, 0, MaxAttVal[Att]) / Items;
[26]40
[68]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        }
[26]47
[68]48}
[26]49
[103]50EvalDiscreteAtt_Discr(Att, Fp, Lp, Items, Freq_discr, ValFreq_discr, UnknownRate_discr, the_gain, the_info)
[68]51        /*  ---------------  */
[103]52        Attribute Att;ItemNo Fp, Lp;ItemCount Items; ItemCount** Freq_discr; ItemCount* ValFreq_discr; float* UnknownRate_discr;
53        float* the_gain; float* the_info;{
[68]54        ItemCount KnownItems;
55        float DiscrKnownBaseInfo_Discr(), ComputeGain_Discr(), TotalInfo();
[26]56
[84]57        ComputeFrequencies_Discr(Att, Fp, Lp, Freq_discr, ValFreq_discr, UnknownRate_discr);
[26]58
[84]59        KnownItems = Items - ValFreq_discr[0];
[26]60
[68]61        /*  Special case when no known values of the attribute  */
62
[84]63        if (Items <= ValFreq_discr[0]) {
[68]64                Verbosity(2)
65                        printf("\tAtt %s: no known values\n", AttName[Att]);
66
[103]67                *the_gain = -Epsilon;
68                *the_info = 0.0;
[68]69                return;
70        }
71
[103]72        *the_gain = ComputeGain_Discr(DiscrKnownBaseInfo_Discr(KnownItems, MaxAttVal[Att], Freq_discr),
[84]73                        UnknownRate_discr[Att], MaxAttVal[Att], KnownItems, Freq_discr, ValFreq_discr);
[103]74        *the_info = TotalInfo(ValFreq_discr, 0, MaxAttVal[Att]) / Items;
[68]75
76        Verbosity(2) {
77                printf("\tAtt %s", AttName[Att]);
78                Verbosity(3)
[84]79                        PrintDistribution_Discr(Att, MaxAttVal[Att], true, Freq_discr);
[103]80                printf("\tinf %.3f, gain %.3f\n", *the_info, *the_gain);
[68]81        }
82
83}
[26]84/*************************************************************************/
85/*                                                                       */
86/*  Compute frequency tables Freq[][] and ValFreq[] for attribute        */
87/*  Att from items Fp to Lp, and set the UnknownRate for Att             */
88/*                                                                       */
89/*************************************************************************/
90
[68]91ComputeFrequencies(Att, Fp, Lp)
92        /*  ------------------  */
93        Attribute Att;ItemNo Fp, Lp; {
94        Description Case;
95        ClassNo c;
96        DiscrValue v;
97        ItemCount CountItems();
98        ItemNo p;
[26]99
[68]100        ResetFreq(MaxAttVal[Att], Freq, ValFreq);
[26]101
[68]102        /*  Determine the frequency of each class amongst cases
103         with each possible value for the given attribute  */
[26]104
[68]105        ForEach(p, Fp, Lp) {
106                Case = Item[p];
107                Freq[DVal(Case,Att)][Class(Case)] += Weight[p];
108        }
[26]109
[68]110        /*  Determine the frequency of each possible value for the
111         given attribute  */
[26]112
[68]113        ForEach(v, 0, MaxAttVal[Att]) {
114                ForEach(c, 0, MaxClass) {
115                        ValFreq[v] += Freq[v][c];
116                }
[26]117        }
118
[68]119        /*  Set the rate of unknown values of the attribute  */
120
121        UnknownRate[Att] = ValFreq[0] / CountItems(Fp, Lp);
[26]122}
123
[84]124ComputeFrequencies_Discr(Att, Fp, Lp, Freq_discr, ValFreq_discr, UnknownRate_discr)
[68]125        /*  ------------------  */
[84]126        Attribute Att;ItemNo Fp, Lp; ItemCount** Freq_discr; ItemCount* ValFreq_discr; float* UnknownRate_discr;{
[68]127        Description Case;
128        ClassNo c;
129        DiscrValue v;
130        ItemCount CountItems();
131        ItemNo p;
[26]132
[84]133        ResetFreq_discr(MaxAttVal[Att], Freq_discr, ValFreq_discr);
[26]134
[68]135        /*  Determine the frequency of each class amongst cases
136         with each possible value for the given attribute  */
137
138        ForEach(p, Fp, Lp) {
139                Case = Item[p];
[84]140                Freq_discr[DVal(Case,Att)][Class(Case)] += Weight[p];
[68]141        }
142
143        /*  Determine the frequency of each possible value for the
144         given attribute  */
145
146        ForEach(v, 0, MaxAttVal[Att]) {
147                ForEach(c, 0, MaxClass) {
[84]148                        ValFreq_discr[v] += Freq_discr[v][c];
[68]149                }
150        }
151
152        /*  Set the rate of unknown values of the attribute  */
153
[84]154        UnknownRate_discr[Att] = ValFreq_discr[0] / CountItems(Fp, Lp);
[68]155}
[26]156/*************************************************************************/
157/*                                                                       */
158/*  Return the base info for items with known values of a discrete       */
159/*  attribute, using the frequency table Freq[][]                        */
160/*                                                                       */
161/*************************************************************************/
162
163float DiscrKnownBaseInfo(KnownItems, MaxVal)
[68]164        /*    ------------------  */
165        DiscrValue MaxVal;ItemCount KnownItems; {
166        ClassNo c;
167        ItemCount ClassCount;
168        double Sum = 0;
169        DiscrValue v;
[26]170
[68]171        ForEach(c, 0, MaxClass) {
172                ClassCount = 0;
173                ForEach(v, 1, MaxVal) {
174                        ClassCount += Freq[v][c];
175                }
176                Sum += ClassCount * Log(ClassCount);
[26]177        }
178
[68]179        return (KnownItems * Log(KnownItems) - Sum) / KnownItems;
[26]180}
181
[84]182float DiscrKnownBaseInfo_Discr(KnownItems, MaxVal, Freq_discr)
[68]183        /*    ------------------  */
[84]184        DiscrValue MaxVal;ItemCount KnownItems; ItemCount** Freq_discr;{
[68]185        ClassNo c;
186        ItemCount ClassCount;
187        double Sum = 0;
188        DiscrValue v;
[26]189
[68]190        ForEach(c, 0, MaxClass) {
191                ClassCount = 0;
192                ForEach(v, 1, MaxVal) {
[84]193                        ClassCount += Freq_discr[v][c];
[68]194                }
195                Sum += ClassCount * Log(ClassCount);
196        }
[26]197
[68]198        return (KnownItems * Log(KnownItems) - Sum) / KnownItems;
199}
200
[26]201/*************************************************************************/
202/*                                                                       */
203/*  Construct and return a node for a test on a discrete attribute       */
204/*                                                                       */
205/*************************************************************************/
206
[68]207DiscreteTest(Node, Att)
208        /*  ----------  */
209        Tree Node;Attribute Att; {
210        ItemCount CountItems();
[26]211
[68]212        Sprout(Node, MaxAttVal[Att]);
[26]213
[68]214        Node->NodeType = BrDiscr;
215        Node->Tested = Att;
216        Node->Errors = 0;
[26]217}
Note: See TracBrowser for help on using the repository browser.