source: src/main/java/weka/classifiers/trees/j48/EntropyBasedSplitCrit.java @ 25

Last change on this file since 25 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 2.5 KB
Line 
1/*
2 *    This program is free software; you can redistribute it and/or modify
3 *    it under the terms of the GNU General Public License as published by
4 *    the Free Software Foundation; either version 2 of the License, or
5 *    (at your option) any later version.
6 *
7 *    This program is distributed in the hope that it will be useful,
8 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
9 *    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 *    EntropyBasedSplitCrit.java
19 *    Copyright (C) 1999 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.classifiers.trees.j48;
24
25/**
26 * "Abstract" class for computing splitting criteria
27 * based on the entropy of a class distribution.
28 *
29 * @author Eibe Frank (eibe@cs.waikato.ac.nz)
30 * @version $Revision: 1.8 $
31 */
32public abstract class EntropyBasedSplitCrit
33  extends SplitCriterion {
34
35  /** for serialization */
36  private static final long serialVersionUID = -2618691439791653056L;
37
38  /** The log of 2. */
39  protected static double log2 = Math.log(2);
40
41  /**
42   * Help method for computing entropy.
43   */
44  public final double logFunc(double num) {
45
46    // Constant hard coded for efficiency reasons
47    if (num < 1e-6)
48      return 0;
49    else
50      return num*Math.log(num)/log2;
51  }
52
53  /**
54   * Computes entropy of distribution before splitting.
55   */
56  public final double oldEnt(Distribution bags) {
57
58    double returnValue = 0;
59    int j;
60
61    for (j=0;j<bags.numClasses();j++)
62      returnValue = returnValue+logFunc(bags.perClass(j));
63    return logFunc(bags.total())-returnValue; 
64  }
65
66  /**
67   * Computes entropy of distribution after splitting.
68   */
69  public final double newEnt(Distribution bags) {
70   
71    double returnValue = 0;
72    int i,j;
73
74    for (i=0;i<bags.numBags();i++){
75      for (j=0;j<bags.numClasses();j++)
76        returnValue = returnValue+logFunc(bags.perClassPerBag(i,j));
77      returnValue = returnValue-logFunc(bags.perBag(i));
78    }
79    return -returnValue;
80  }
81
82  /**
83   * Computes entropy after splitting without considering the
84   * class values.
85   */
86  public final double splitEnt(Distribution bags) {
87
88    double returnValue = 0;
89    int i;
90
91    for (i=0;i<bags.numBags();i++)
92      returnValue = returnValue+logFunc(bags.perBag(i));
93    return logFunc(bags.total())-returnValue;
94  }
95}
96
Note: See TracBrowser for help on using the repository browser.