01: /*
02: * This program is free software; you can redistribute it and/or modify
03: * it under the terms of the GNU General Public License as published by
04: * the Free Software Foundation; either version 2 of the License, or
05: * (at your option) any later version.
06: *
07: * This program is distributed in the hope that it will be useful,
08: * but WITHOUT ANY WARRANTY; without even the implied warranty of
09: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10: * GNU General Public License for more details.
11: *
12: * You should have received a copy of the GNU General Public License
13: * along with this program; if not, write to the Free Software
14: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15: */
16:
17: /*
18: * EntropySplitCrit.java
19: * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20: *
21: */
22:
23: package weka.classifiers.trees.j48;
24:
25: import weka.core.Utils;
26:
27: /**
28: * Class for computing the entropy for a given distribution.
29: *
30: * @author Eibe Frank (eibe@cs.waikato.ac.nz)
31: * @version $Revision: 1.7 $
32: */
33: public final class EntropySplitCrit extends EntropyBasedSplitCrit {
34:
35: /** for serialization */
36: private static final long serialVersionUID = 5986252682266803935L;
37:
38: /**
39: * Computes entropy for given distribution.
40: */
41: public final double splitCritValue(Distribution bags) {
42:
43: return newEnt(bags);
44: }
45:
46: /**
47: * Computes entropy of test distribution with respect to training distribution.
48: */
49: public final double splitCritValue(Distribution train,
50: Distribution test) {
51:
52: double result = 0;
53: int numClasses = 0;
54: int i, j;
55:
56: // Find out relevant number of classes
57: for (j = 0; j < test.numClasses(); j++)
58: if (Utils.gr(train.perClass(j), 0)
59: || Utils.gr(test.perClass(j), 0))
60: numClasses++;
61:
62: // Compute entropy of test data with respect to training data
63: for (i = 0; i < test.numBags(); i++)
64: if (Utils.gr(test.perBag(i), 0)) {
65: for (j = 0; j < test.numClasses(); j++)
66: if (Utils.gr(test.perClassPerBag(i, j), 0))
67: result -= test.perClassPerBag(i, j)
68: * Math
69: .log(train.perClassPerBag(i, j) + 1);
70: result += test.perBag(i)
71: * Math.log(train.perBag(i) + numClasses);
72: }
73:
74: return result / log2;
75: }
76: }
|