InfoGainSplitCrit.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 3k
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.  *    InfoGainSplitCrit.java
  18.  *    Copyright (C) 1999 Eibe Frank
  19.  *
  20.  */
  21. package weka.classifiers.j48;
  22. import weka.core.*;
  23. /**
  24.  * Class for computing the information gain for a given distribution.
  25.  *
  26.  * @author Eibe Frank (eibe@cs.waikato.ac.nz)
  27.  * @version $Revision: 1.5 $
  28.  */
  29. public final class InfoGainSplitCrit extends EntropyBasedSplitCrit{
  30.   /**
  31.    * This method is a straightforward implementation of the information
  32.    * gain criterion for the given distribution.
  33.    */
  34.   public final double splitCritValue(Distribution bags) {
  35.     double numerator;
  36.         
  37.     numerator = oldEnt(bags)-newEnt(bags);
  38.     // Splits with no gain are useless.
  39.     if (Utils.eq(numerator,0))
  40.       return Double.MAX_VALUE;
  41.         
  42.     // We take the reciprocal value because we want to minimize the
  43.     // splitting criterion's value.
  44.     return bags.total()/numerator;
  45.   }
  46.   /**
  47.    * This method computes the information gain in the same way 
  48.    * C4.5 does.
  49.    *
  50.    * @param distribution the distribution
  51.    * @param totalNoInst weight of ALL instances (including the
  52.    * ones with missing values).
  53.    */
  54.   public final double splitCritValue(Distribution bags,double totalNoInst) {
  55.     
  56.     double numerator;
  57.     double noUnknown;
  58.     double unknownRate;
  59.     int i;
  60.     
  61.     noUnknown = totalNoInst-bags.total();
  62.     unknownRate = noUnknown/totalNoInst;
  63.     numerator = (oldEnt(bags)-newEnt(bags));
  64.     numerator = (1-unknownRate)*numerator;
  65.     
  66.     // Splits with no gain are useless.
  67.     if (Utils.eq(numerator,0))
  68.       return 0;
  69.     
  70.     return numerator/bags.total();
  71.   }
  72.   /**
  73.    * This method computes the information gain in the same way 
  74.    * C4.5 does.
  75.    *
  76.    * @param distribution the distribution
  77.    * @param totalNoInst weight of ALL instances 
  78.    * @param oldEnt entropy with respect to "no-split"-model.
  79.    */
  80.   public final double splitCritValue(Distribution bags,double totalNoInst,
  81.                                      double oldEnt) {
  82.     
  83.     double numerator;
  84.     double noUnknown;
  85.     double unknownRate;
  86.     int i;
  87.     
  88.     noUnknown = totalNoInst-bags.total();
  89.     unknownRate = noUnknown/totalNoInst;
  90.     numerator = (oldEnt-newEnt(bags));
  91.     numerator = (1-unknownRate)*numerator;
  92.     
  93.     // Splits with no gain are useless.
  94.     if (Utils.eq(numerator,0))
  95.       return 0;
  96.     
  97.     return numerator/bags.total();
  98.   }
  99. }