EntropyBasedSplitCrit.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 2k
Category:

Windows Develop

Development Platform:

Java

  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.  *    EntropyBasedSplitCrit.java
  18.  *    Copyright (C) 1999 Eibe Frank
  19.  *
  20.  */
  21. package weka.classifiers.trees.j48;
  22. /**
  23.  * "Abstract" class for computing splitting criteria
  24.  * based on the entropy of a class distribution.
  25.  *
  26.  * @author Eibe Frank (eibe@cs.waikato.ac.nz)
  27.  * @version $Revision: 1.5 $
  28.  */
  29. public abstract class EntropyBasedSplitCrit extends SplitCriterion{
  30.   /** The log of 2. */
  31.   protected static double log2 = Math.log(2);
  32.   /**
  33.    * Help method for computing entropy.
  34.    */
  35.   public final double logFunc(double num) {
  36.     // Constant hard coded for efficiency reasons
  37.     if (num < 1e-6)
  38.       return 0;
  39.     else
  40.       return num*Math.log(num)/log2;
  41.   }
  42.   /**
  43.    * Computes entropy of distribution before splitting.
  44.    */
  45.   public final double oldEnt(Distribution bags) {
  46.     double returnValue = 0;
  47.     int j;
  48.     for (j=0;j<bags.numClasses();j++)
  49.       returnValue = returnValue+logFunc(bags.perClass(j));
  50.     return logFunc(bags.total())-returnValue; 
  51.   }
  52.   /**
  53.    * Computes entropy of distribution after splitting.
  54.    */
  55.   public final double newEnt(Distribution bags) {
  56.     
  57.     double returnValue = 0;
  58.     int i,j;
  59.     for (i=0;i<bags.numBags();i++){
  60.       for (j=0;j<bags.numClasses();j++)
  61. returnValue = returnValue+logFunc(bags.perClassPerBag(i,j));
  62.       returnValue = returnValue-logFunc(bags.perBag(i));
  63.     }
  64.     return -returnValue;
  65.   }
  66.   /**
  67.    * Computes entropy after splitting without considering the
  68.    * class values.
  69.    */
  70.   public final double splitEnt(Distribution bags) {
  71.     double returnValue = 0;
  72.     int i;
  73.     for (i=0;i<bags.numBags();i++)
  74.       returnValue = returnValue+logFunc(bags.perBag(i));
  75.     return logFunc(bags.total())-returnValue;
  76.   }
  77. }