EntropySplitCrit.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.  *    EntropySplitCrit.java
  18.  *    Copyright (C) 1999 Eibe Frank
  19.  *
  20.  */
  21. package weka.classifiers.j48;
  22. import weka.core.*;
  23. /**
  24.  * Class for computing the entropy for a given distribution.
  25.  *
  26.  * @author Eibe Frank (eibe@cs.waikato.ac.nz)
  27.  * @version $Revision: 1.4 $
  28.  */
  29. public final class EntropySplitCrit extends EntropyBasedSplitCrit {
  30.   /**
  31.    * Computes entropy for given distribution.
  32.    */
  33.   public final double splitCritValue(Distribution bags) {
  34.     
  35.     return newEnt(bags);
  36.   }
  37.   /**
  38.    * Computes entropy of test distribution with respect to training distribution.
  39.    */
  40.   public final double splitCritValue(Distribution train, Distribution test) {
  41.     double result = 0;
  42.     int numClasses = 0;
  43.     int i, j;
  44.     
  45.     // Find out relevant number of classes
  46.     for (j = 0; j < test.numClasses(); j++)
  47.       if (Utils.gr(train.perClass(j), 0) || Utils.gr(test.perClass(j), 0))
  48. numClasses++;
  49.     // Compute entropy of test data with respect to training data
  50.     for (i = 0; i < test.numBags(); i++)
  51.       if (Utils.gr(test.perBag(i),0)) {
  52. for (j = 0; j < test.numClasses(); j++)
  53.   if (Utils.gr(test.perClassPerBag(i, j), 0))
  54.     result -= test.perClassPerBag(i, j)*
  55.       Math.log(train.perClassPerBag(i, j) + 1);
  56. result += test.perBag(i) * Math.log(train.perBag(i) + numClasses);
  57.       }
  58.   
  59.     return result / log2;
  60.   }
  61. }