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

Windows Develop

Development Platform:

Java

  1. /*
  2.  *    YongSplitInfo.java
  3.  *    Copyright (C) 1999 
  4.  *
  5.  *    This program is free software; you can redistribute it and/or modify
  6.  *    it under the terms of the GNU General Public License as published by
  7.  *    the Free Software Foundation; either version 2 of the License, or
  8.  *    (at your option) any later version.
  9.  *
  10.  *    This program is distributed in the hope that it will be useful,
  11.  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  *    GNU General Public License for more details.
  14.  *
  15.  *    You should have received a copy of the GNU General Public License
  16.  *    along with this program; if not, write to the Free Software
  17.  *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  */
  19. package weka.classifiers.trees.m5;
  20. import java.io.*;
  21. import java.util.*;
  22. import weka.core.*;
  23. /**
  24.  * Stores split information.
  25.  *
  26.  * @author Yong Wang (yongwang@cs.waikato.ac.nz)
  27.  * @author Mark Hall (mhall@cs.waikato.ac.nz)
  28.  * @version $Revision: 1.1 $
  29.  */
  30. public final class YongSplitInfo implements Cloneable, 
  31.     Serializable, 
  32.     SplitEvaluate {
  33.   private int  number;         // number of total instances
  34.   private int  first;          // first instance index
  35.   private int  last;           // last instance index
  36.   private int  position;       // position of maximum impurity reduction
  37.   private double  maxImpurity; // maximum impurity reduction
  38.   private double  leftAve;     // left average class value
  39.   private double rightAve;     // right average class value
  40.   private int  splitAttr;      // spliting attribute 
  41.   private double  splitValue;  // splitting value
  42.   /**
  43.    * Constructs an object which contains the split information
  44.    * @param low the index of the first instance
  45.    * @param high the index of the last instance
  46.    * @param attr an attribute
  47.    */
  48.   public YongSplitInfo(int low,int high,int attr) {
  49.     number = high-low+1;
  50.     first = low;
  51.     last = high;
  52.     position = -1;
  53.     maxImpurity = -1.e20;
  54.     splitAttr = attr;      // attr < 0 is an empty object 
  55.     splitValue = 0.0;
  56.     Utils.SMALL = 1e-10;
  57.   }
  58.   /**
  59.    * Makes a copy of this SplitInfo object
  60.    */ 
  61.   public final SplitEvaluate copy () throws Exception {
  62.     YongSplitInfo s = (YongSplitInfo)this.clone();
  63.     
  64.     return s;
  65.   }
  66.   /**
  67.    * Resets the object of split information
  68.    * @param low the index of the first instance
  69.    * @param high the index of the last instance
  70.    * @param attr the attribute
  71.    */
  72.   public final void  initialize(int low,int high,int attr){
  73.       
  74.     number = high-low+1;
  75.     first = low;
  76.     last = high;
  77.     position = -1;
  78.     maxImpurity = -1.e20;
  79.     splitAttr = attr;
  80.     splitValue = 0.0;
  81.   }
  82.   /**
  83.    * Converts the spliting information to string
  84.    * @param inst the instances
  85.    */
  86.   public final String  toString(Instances inst){
  87.     StringBuffer text =  new StringBuffer();
  88.     text.append("Print SplitInfo:n");
  89.     text.append("    Instances:tt" + number + " (" + first + "-" + 
  90. position + "," + (position+1) + "-" + last + ")n");
  91.     text.append("    Maximum Impurity Reduction:t" + 
  92. Utils.doubleToString(maxImpurity,1,4) + "n");
  93.     text.append("    Left average:t" + leftAve + "n");
  94.     text.append("    Right average:t" + rightAve + "n");
  95.     if(maxImpurity>0.0)
  96.       text.append("    Splitting function:t" + 
  97.   inst.attribute(splitAttr).name() + " = " 
  98.   + splitValue + "n");
  99.     else text.append("    Splitting function:tnulln");
  100.     
  101.     return text.toString();
  102.   }
  103.   
  104.   /** 
  105.    * Finds the best splitting point for an attribute in the instances
  106.    * @param attr the splitting attribute
  107.    * @param inst the instances
  108.    * @exception Exception if something goes wrong
  109.    */
  110.   public final void  attrSplit(int attr,Instances inst) throws Exception {
  111.     int i,len,count,part;
  112.     Impurity imp;
  113.     
  114.     int low = 0;
  115.     int high = inst.numInstances()-1;
  116.     this.initialize(low,high,attr);
  117.     if(number < 4) {
  118.       return;
  119.     }
  120.     
  121.     len = ((high-low+1)<5) ? 1 : (high-low+1) / 5; 
  122.     
  123.     position = low;
  124.     
  125.     part = low + len - 1;
  126.     imp = new Impurity(part,attr,inst,5);
  127.     
  128.     count=0;
  129.     for(i=low+len;i<=high-len-1;i++) {
  130.       
  131.       imp.incremental(inst.instance(i).classValue(),1);
  132.       
  133.       if(Utils.eq(inst.instance(i+1).value(attr),
  134.   inst.instance(i).value(attr)) == false) {
  135. count = i;
  136. if(imp.impurity > maxImpurity){
  137.   maxImpurity = imp.impurity;
  138.   splitValue = (inst.instance(i).value(attr) +
  139. inst.instance(i+1).value(attr)) * 0.5;
  140.   leftAve = imp.sl / imp.nl; 
  141.   rightAve = imp.sr / imp.nr; 
  142.   position=i;
  143. }
  144.       } 
  145.     }
  146.   }
  147.   /**
  148.    * Returns the impurity of this split
  149.    *
  150.    * @return the impurity of this split
  151.    */
  152.   public double maxImpurity () {
  153.     return maxImpurity;
  154.   }
  155.   /**
  156.    * Returns the attribute used in this split
  157.    *
  158.    * @return the attribute used in this split
  159.    */
  160.   public int splitAttr () {
  161.     return splitAttr;
  162.   }
  163.   /**
  164.    * Returns the position of the split in the sorted values. -1 indicates that
  165.    * a split could not be found.
  166.    *
  167.    * @return an <code>int</code> value
  168.    */
  169.   public int position () {
  170.     return position;
  171.   }
  172.   /**
  173.    * Returns the split value
  174.    *
  175.    * @return the split value
  176.    */
  177.   public double splitValue () {
  178.     return splitValue;
  179.   }
  180. }