-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinfo.c
306 lines (249 loc) · 6.98 KB
/
info.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*************************************************************************/
/* */
/* Calculate information, information gain, and print dists */
/* -------------------------------------------------------- */
/* */
/*************************************************************************/
#include "c45extern.h"
float TotalInfo(float V[], DiscrValue MinVal, DiscrValue MaxVal);
ClassNo GetBestClass(int Fp, int Lp)
{
ClassNo c, BestClass;
int i;
float *ClsFreq;
/* Generate the class frequency distribution */
ClsFreq = (float *) calloc(MaxClass+1, sizeof(float));
// ForEach(c, 0, MaxClass)
// {
// ClassFreq[c] = 0;
// }
ForEach(i, Fp, Lp)
{
ClsFreq[ Class(Item[i]) ] += Weight[i];
}
/* Find the most frequent class */
BestClass = 0;
ForEach(c, 0, MaxClass)
{
if ( ClsFreq[c] > ClsFreq[BestClass] )
{
BestClass = c;
}
}
free(ClsFreq);
return BestClass;
}
/*************************************************************************/
/* */
/* Determine the worth of a particular split according to the */
/* operative criterion */
/* */
/* Parameters: */
/* SplitInfo: potential info of the split */
/* SplitGain: gain in info of the split */
/* MinGain: gain above which the Gain Ratio */
/* may be used */
/* */
/* If the Gain criterion is being used, the information gain of */
/* the split is returned, but if the Gain Ratio criterion is */
/* being used, the ratio of the information gain of the split to */
/* its potential information is returned. */
/* */
/*************************************************************************/
float Worth(float ThisInfo, float ThisGain, float MinGain)
/* -----
float ThisInfo, ThisGain, MinGain;
*/
{
if ( GAINRATIO )
{
if ( ThisGain >= MinGain - Epsilon && ThisInfo > Epsilon )
{
return ThisGain / ThisInfo;
}
else
{
return -Epsilon;
}
}
else
{
return ( ThisInfo > 0 && ThisGain > -Epsilon ? ThisGain : -Epsilon );
}
}
/*************************************************************************/
/* */
/* Zero the frequency tables Freq[][] and ValFreq[] up to MaxVal */
/* */
/*************************************************************************/
void ResetFreq(MaxVal)
/* --------- */
DiscrValue MaxVal;
{
DiscrValue v;
ClassNo c;
ForEach(v, 0, MaxVal)
{
ForEach(c, 0, MaxClass)
{
Freq[v][c] = 0;
}
ValFreq[v] = 0;
}
}
/*************************************************************************/
/* */
/* Given tables Freq[][] and ValFreq[], compute the information gain. */
/* */
/* Parameters: */
/* BaseInfo: average information for all items with */
/* known values of the test attribute */
/* UnknownRate: fraction of items with unknown ditto */
/* MaxVal: number of forks */
/* TotalItems: number of items with known values of */
/* test att */
/* */
/* where Freq[x][y] contains the no. of cases with value x for a */
/* particular attribute that are members of class y, */
/* and ValFreq[x] contains the no. of cases with value x for a */
/* particular attribute */
/* */
/*************************************************************************/
float ComputeGain(float BaseInfo, float UnknFrac, DiscrValue MaxVal, float TotalItems, int i, int Sp, int Ep)
/* -----------
float BaseInfo, UnknFrac;
DiscrValue MaxVal;
float TotalItems;
int i, Sp, Ep;
*/
{
DiscrValue v;
float ThisInfo=0.0, ThisGain;// TotalInfo();
short ReasonableSubsets=0;
/* Check whether all values are unknown or the same */
if ( ! TotalItems ) return -Epsilon;
/* There must be at least two subsets with MINOBJS items */
// ForEach(v, 1, MaxVal)
// {
//if ( ValFreq[v] >= MINOBJS ) ReasonableSubsets++;
// }
/* if (i - Sp + 1 >= MINOBJS) ReasonableSubsets++;
if (Ep - i >= MINOBJS) ReasonableSubsets++;
*/
if (FIRSTHSP && Sp == 0) //fist hsp's first segment
{
if (i - Sp + 1 >= MINOBJS) ReasonableSubsets++;
if (GetBestClass(i+1, Ep) == 0) //second branch is intron
{
if (Ep - i >= MIN_INTRON_LEN_AA) ReasonableSubsets++;
}
else
{
if (Ep - i >= MINOBJS) ReasonableSubsets++;
}
}
else
{
if (LASTHSP && Ep == MaxItem) //last hsp's last segment
{
if (GetBestClass(Sp, i) == 0) //first branch is intron
{
if (i - Sp + 1 >= MIN_INTRON_LEN_AA) ReasonableSubsets++;
}
else
{
if (i - Sp + 1 >= MINOBJS) ReasonableSubsets++;
}
if (Ep - i >= MINOBJS) ReasonableSubsets++;
}
else
{
if (GetBestClass(Sp, i) == 0) //first branch is intron
{
if (i - Sp + 1 >= MIN_INTRON_LEN_AA) ReasonableSubsets++;
if (Ep - i >= MIN_INTERNAL_EXON_LEN_AA) ReasonableSubsets++;
}
else //first branch is exon
{
if (i - Sp + 1 >= MIN_INTERNAL_EXON_LEN_AA) ReasonableSubsets++;
if (Ep - i >= MIN_INTRON_LEN_AA) ReasonableSubsets++;
}
}
}
if ( ReasonableSubsets < 2 ) return -Epsilon;
/* Compute total info after split, by summing the
info of each of the subsets formed by the test */
ForEach(v, 1, MaxVal)
{
ThisInfo += TotalInfo(Freq[v], 0, MaxClass);
}
/* Set the gain in information for all items, adjusted for unknowns */
ThisGain = (1 - UnknFrac) * (BaseInfo - ThisInfo / TotalItems);
Verbosity(5)
printf("ComputeThisGain: items %.1f info %.3f base %.3f unkn %.3f result %.3f\n",
TotalItems + ValFreq[0], ThisInfo, BaseInfo, UnknFrac, ThisGain);
return ThisGain;
}
/*************************************************************************/
/* */
/* Compute the total information in V[ MinVal..MaxVal ] */
/* */
/*************************************************************************/
float TotalInfo(float V[], DiscrValue MinVal, DiscrValue MaxVal)
/* ---------
float V[];
DiscrValue MinVal, MaxVal;
*/
{
DiscrValue v;
float Sum=0.0;
float N, TotalItems=0;
ForEach(v, MinVal, MaxVal)
{
N = V[v];
Sum += N * Log(N);
TotalItems += N;
}
return TotalItems * Log(TotalItems) - Sum;
}
/*************************************************************************/
/* */
/* Print distribution table for given attribute */
/* */
/*************************************************************************/
/*
PrintDistribution(Att, MaxVal, ShowNames)
Attribute Att;
DiscrValue MaxVal;
Boolean ShowNames;
{
DiscrValue v;
ClassNo c;
String Val;
printf("\n\t\t\t ");
ForEach(c, 0, MaxClass)
{
printf("%7.6s", ClassName[c]);
}
printf("\n");
ForEach(v, 0, MaxVal)
{
if ( ShowNames )
{
Val = ( !v ? "unknown" :
MaxAttVal[Att] ? AttValName[Att][v] :
v == 1 ? "below" : "above" );
printf("\t\t[%-7.7s:", Val);
}
else
{
printf("\t\t[%-7d:", v);
}
ForEach(c, 0, MaxClass)
{
printf(" %6.1f", Freq[v][c]);
}
printf("]\n");
}
}
*/