Impurity.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 6k
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.  *    Impurity.java
  18.  *    Copyright (C) 1999 Yong Wang
  19.  *
  20.  */
  21. package weka.classifiers.m5;
  22. import java.io.*;
  23. import java.util.*;
  24. import weka.core.*;
  25. /**
  26.  * Class for handling the impurity values when spliting the instances
  27.  * @author Yong Wang (yongwang@cs.waikato.ac.nz)
  28.  * @version $Revision: 1.4 $
  29.  */
  30. public final class Impurity{
  31.   double n;                   // number of total instances 
  32.   int attr;                   // splitting attribute 
  33.   double nl;                  // number of instances in the left group 
  34.   double nr;                  // number of instances in the right group 
  35.   double sl;                  // sum of the left group  
  36.   double sr;                  // sum of the right group 
  37.   double s2l;                 // squared sum of the left group 
  38.   double s2r;                 // squared sum of the right group 
  39.   double sdl;                 // standard deviation of the left group 
  40.   double sdr;                 // standard deviation of the right group 
  41.   double vl;                  // variance of the left group 
  42.   double vr;                  // variance of the right group 
  43.   double sd;                  // overall standard deviation 
  44.   double va;                  // overall variance 
  45.   double impurity;            // impurity value;
  46.   int order;                  // order = 1, variance; order = 2, standard deviation; order = 3, the cubic root of the variance;  
  47.                               // order = k, the k-th order root of the variance
  48.   /**
  49.    * Constructs an Impurity object containing the impurity values of partitioning the instances using an attribute
  50.    * @param partition the index of the last instance in the left subset
  51.    * @param attribute the attribute used in partitioning
  52.    * @param inst instances
  53.    * @param k the order of the impurity; =1, the variance; =2, the stardard deviation; =k, the k-th order root of the variance
  54.    */
  55.   public Impurity(int partition,int attribute,Instances inst,int k){
  56.     Values values = new Values(0,inst.numInstances()-1,inst.classIndex(),inst);
  57.     attr = attribute;
  58.     n   = inst.numInstances();
  59.     sd  = values.sd; 
  60.     va  = values.va;
  61.     values = new Values(0,partition,inst.classIndex(),inst);
  62.     nl  = partition + 1;
  63.     sl  = values.sum;
  64.     s2l = values.sqrSum;
  65.     values = new Values(partition+1,inst.numInstances()-1,inst.classIndex(),inst);
  66.     nr  = inst.numInstances() - partition -1;
  67.     sr  = values.sum;
  68.     s2r = values.sqrSum;
  69.     order = k;
  70.     this.incremental(0,0);
  71.   }
  72.   /**
  73.    * Converts an Impurity object to a string
  74.    * @return the converted string
  75.    */
  76.   public final String  toString() {
  77.     
  78.     StringBuffer text = new StringBuffer();
  79.     
  80.     text.append("Print impurity values:n");
  81.     text.append("    Number of total instances:t" + n + "n");
  82.     text.append("    Splitting attribute:tt" + attr + "n");
  83.     text.append("    Number of the instances in the left:t" + nl + "n");
  84.     text.append("    Number of the instances in the right:t" + nr + "n");
  85.     text.append("    Sum of the left:ttt" + sl + "n");
  86.     text.append("    Sum of the right:ttt" + sr + "n");
  87.     text.append("    Squared sum of the left:tt" + s2l + "n"); 
  88.     text.append("    Squared sum of the right:tt" + s2r + "n");
  89.     text.append("    Standard deviation of the left:t" + sdl + "n");
  90.     text.append("    Standard deviation of the right:t" + sdr + "n");
  91.     text.append("    Variance of the left:tt" + vr + "n");
  92.     text.append("    Variance of the right:tt" + vr + "n");
  93.     text.append("    Overall standard deviation:tt" + sd + "n");
  94.     text.append("    Overall variance:ttt" + va + "n");
  95.     text.append("    Impurity (order " + order + "):tt" + impurity + "n");
  96.     return text.toString();
  97.   }
  98.   
  99.   /**
  100.    * Incrementally computes the impurirty values
  101.    * @param value the incremental value 
  102.    * @param type if type=1, value will be added to the left subset; type=-1, to the right subset; type=0, initializes
  103.    */
  104.   public final void  incremental(double value,int type){
  105.     double y=0.,yl=0.,yr=0.;
  106.     switch(type){
  107.     case 1:
  108.       nl += 1;
  109.       nr -= 1;
  110.       sl += value;
  111.       sr -= value;
  112.       s2l += value*value;
  113.       s2r -= value*value;
  114.       break;
  115.     case -1:
  116.       nl -= 1;
  117.       nr += 1;
  118.       sl -= value;
  119.       sr += value;
  120.       s2l -= value*value;
  121.       s2r += value*value;
  122.       break;
  123.     case 0:
  124.       break;
  125.     default: M5Utils.errorMsg("wrong type in Impurity.incremental().");
  126.     }
  127.     if(nl<=0.0){
  128.       vl=0.0;
  129.       sdl=0.0;
  130.     }
  131.     else {
  132.       vl = (nl*s2l-sl*sl)/((double)nl*((double)nl));
  133.       vl = Math.abs(vl);
  134.       sdl = Math.sqrt(vl);
  135.     }
  136.     if(nr<=0.0){
  137.       vr=0.0;
  138.       sdr=0.0;
  139.     }
  140.     else {
  141.       vr = (nr*s2r-sr*sr)/((double)nr*((double)nr));
  142.       vr = Math.abs(vr);
  143.       sdr = Math.sqrt(vr);
  144.     }
  145.     if(order <= 0)M5Utils.errorMsg("Impurity order less than zero in Impurity.incremental()");
  146.     else if(order == 1) {
  147.       y = va; yl = vl; yr = vr;
  148.     } else {
  149.       y  = Math.pow(va,1./order);
  150.       yl = Math.pow(vl,1./order);
  151.       yr = Math.pow(vr,1./order);
  152.     }
  153.     if(nl<=0.0 || nr<=0.0)
  154.       impurity = 0.0;
  155.     else { 
  156.       impurity = y - ((double)nl/(double)n)*yl - ((double)nr/(double)n)*yr;
  157.     }
  158.   }
  159. }