BinC45ModelSelection.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 5k
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.  *    BinC45ModelSelection.java
  18.  *    Copyright (C) 1999 Eibe Frank
  19.  *
  20.  */
  21. package weka.classifiers.j48;
  22. import java.util.*;
  23. import weka.core.*;
  24. /**
  25.  * Class for selecting a C4.5-like binary (!) split for a given dataset.
  26.  *
  27.  * @author Eibe Frank (eibe@cs.waikato.ac.nz)
  28.  * @version $Revision: 1.5.2.1 $
  29.  */
  30. public class BinC45ModelSelection extends ModelSelection{
  31.   /** Minimum number of instances in interval. */
  32.   private int m_minNoObj;               
  33.   /** The FULL training dataset. */
  34.   private Instances m_allData; 
  35.   /**
  36.    * Initializes the split selection method with the given parameters.
  37.    *
  38.    * @param m_minNoObj minimum number of instances that have to occur in at least two
  39.    * subsets induced by split
  40.    * @param allData FULL training dataset (necessary for
  41.    * selection of split points).
  42.    */
  43.   public BinC45ModelSelection(int minNoObj,Instances allData){
  44.     m_minNoObj = minNoObj;
  45.     m_allData = allData;
  46.   }
  47.   /**
  48.    * Sets reference to training data to null.
  49.    */
  50.   public void cleanup() {
  51.     m_allData = null;
  52.   }
  53.   /**
  54.    * Selects C4.5-type split for the given dataset.
  55.    */
  56.   public final ClassifierSplitModel selectModel(Instances data){
  57.     double minResult;
  58.     double currentResult;
  59.     BinC45Split [] currentModel;
  60.     BinC45Split bestModel = null;
  61.     NoSplit noSplitModel = null;
  62.     double averageInfoGain = 0;
  63.     int validModels = 0;
  64.     boolean multiVal = true;
  65.     Distribution checkDistribution;
  66.     double sumOfWeights;
  67.     int i;
  68.     
  69.     try{
  70.       // Check if all Instances belong to one class or if not
  71.       // enough Instances to split.
  72.       checkDistribution = new Distribution(data);
  73.       noSplitModel = new NoSplit(checkDistribution);
  74.       if (Utils.sm(checkDistribution.total(),2*m_minNoObj) ||
  75.   Utils.eq(checkDistribution.total(),
  76.    checkDistribution.perClass(checkDistribution.maxClass())))
  77. return noSplitModel;
  78.       // Check if all attributes are nominal and have a 
  79.       // lot of values.
  80.       Enumeration enum = data.enumerateAttributes();
  81.       while (enum.hasMoreElements()) {
  82. Attribute attribute = (Attribute) enum.nextElement();
  83. if ((attribute.isNumeric()) ||
  84.     (Utils.sm((double)attribute.numValues(),
  85.       (0.3*(double)m_allData.numInstances())))){
  86.   multiVal = false;
  87.   break;
  88. }
  89.       }
  90.       currentModel = new BinC45Split[data.numAttributes()];
  91.       sumOfWeights = data.sumOfWeights();
  92.       // For each attribute.
  93.       for (i = 0; i < data.numAttributes(); i++){
  94. // Apart from class attribute.
  95. if (i != (data).classIndex()){
  96.   
  97.   // Get models for current attribute.
  98.   currentModel[i] = new BinC45Split(i,m_minNoObj,sumOfWeights);
  99.   currentModel[i].buildClassifier(data);
  100.   
  101.   // Check if useful split for current attribute
  102.   // exists and check for enumerated attributes with 
  103.   // a lot of values.
  104.   if (currentModel[i].checkModel())
  105.     if ((data.attribute(i).isNumeric()) ||
  106. (multiVal || Utils.sm((double)data.attribute(i).numValues(),
  107.       (0.3*(double)m_allData.numInstances())))){
  108.       averageInfoGain = averageInfoGain+currentModel[i].infoGain();
  109.       validModels++;
  110.     }
  111. }else
  112.   currentModel[i] = null;
  113.       }
  114.       
  115.       // Check if any useful split was found.
  116.       if (validModels == 0)
  117. return noSplitModel;
  118.       averageInfoGain = averageInfoGain/(double)validModels;
  119.       // Find "best" attribute to split on.
  120.       minResult = 0;
  121.       for (i=0;i<data.numAttributes();i++){
  122. if ((i != (data).classIndex()) &&
  123.     (currentModel[i].checkModel()))
  124.   
  125.   // Use 1E-3 here to get a closer approximation to the original
  126.   // implementation.
  127.   if ((currentModel[i].infoGain() >= (averageInfoGain-1E-3)) &&
  128.       Utils.gr(currentModel[i].gainRatio(),minResult)){ 
  129.     bestModel = currentModel[i];
  130.     minResult = currentModel[i].gainRatio();
  131.   }
  132.       }
  133.       
  134.       // Check if useful split was found.
  135.       if (Utils.eq(minResult,0))
  136. return noSplitModel;
  137.       // Add all Instances with unknown values for the corresponding
  138.       // attribute to the distribution for the model, so that
  139.       // the complete distribution is stored with the model. 
  140.       bestModel.distribution().
  141. addInstWithUnknown(data,bestModel.attIndex());
  142.       
  143.       // Set the split point analogue to C45 if attribute numeric.
  144.       bestModel.setSplitPoint(m_allData);
  145.       return bestModel;
  146.     }catch(Exception e){
  147.       e.printStackTrace();
  148.     }
  149.     return null;
  150.   }
  151.   /**
  152.    * Selects C4.5-type split for the given dataset.
  153.    */
  154.   public final ClassifierSplitModel selectModel(Instances train, Instances test) {
  155.     return selectModel(train);
  156.   }
  157. }