MakeDecList.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.  *    MakeDecList.java
  18.  *    Copyright (C) 1999 Eibe Frank
  19.  *
  20.  */
  21. package weka.classifiers.j48;
  22. import java.util.*;
  23. import java.io.*;
  24. import weka.core.*;
  25. import weka.classifiers.*;
  26. /**
  27.  * Class for handling a decision list.
  28.  *
  29.  * @author Eibe Frank (eibe@cs.waikato.ac.nz)
  30.  * @version $Revision: 1.7 $
  31.  */
  32. public class MakeDecList implements Serializable {
  33.   /** Vector storing the rules. */
  34.   private Vector theRules;
  35.   /** The confidence for C45-type pruning. */
  36.   private double CF = 0.25f;
  37.   /** Minimum number of objects */
  38.   private int minNumObj;
  39.   /** The model selection method. */
  40.   private ModelSelection toSelectModeL;
  41.   /** How many subsets of equal size? One used for pruning, the rest for training. */
  42.   private int numSetS = 3;
  43.   /** Use reduced error pruning? */
  44.   private boolean reducedErrorPruning = false;
  45.   /**
  46.    * Constructor for dec list pruned using C4.5 pruning.
  47.    */
  48.   public MakeDecList(ModelSelection toSelectLocModel, double cf,
  49.      int minNum){
  50.     toSelectModeL = toSelectLocModel;
  51.     CF = cf;
  52.     reducedErrorPruning = false;
  53.     minNumObj = minNum;
  54.   }
  55.   /**
  56.    * Constructor for dec list pruned using hold-out pruning.
  57.    */
  58.   public MakeDecList(ModelSelection toSelectLocModel, int num,
  59.      int minNum){
  60.     toSelectModeL = toSelectLocModel;
  61.     numSetS = num;
  62.     reducedErrorPruning = true;
  63.     minNumObj = minNum;
  64.   }
  65.   /**
  66.    * Builds dec list.
  67.    *
  68.    * @exception Exception if dec list can't be built successfully
  69.    */
  70.   public void buildClassifier(Instances data) throws Exception{
  71.     
  72.     ClassifierDecList currentRule;
  73.     double currentWeight;
  74.     Instances oldGrowData, newGrowData, oldPruneData,
  75.       newPruneData;
  76.     int numRules = 0;
  77.     
  78.     if (data.classAttribute().isNumeric())
  79.       throw new UnsupportedClassTypeException("Class is numeric!");
  80.     if (data.checkForStringAttributes()) {
  81.       throw new UnsupportedAttributeTypeException("Can't handle string attributes!");
  82.     }
  83.     
  84.     theRules = new Vector();
  85.     data = new Instances(data);
  86.     data.deleteWithMissingClass();
  87.     if (data.numInstances() == 0)
  88.       throw new Exception("No training instances/Only instances with missing class!");
  89.     if (reducedErrorPruning) {
  90.       data.stratify(numSetS);
  91.       oldGrowData = data.trainCV(numSetS, numSetS - 1);
  92.       oldPruneData = data.testCV(numSetS, numSetS - 1);
  93.     } else {
  94.       oldGrowData = data;
  95.       oldPruneData = null;
  96.     }
  97.     while (Utils.gr(oldGrowData.numInstances(),0)){
  98.       // Create rule
  99.       if (reducedErrorPruning) {
  100. currentRule = new PruneableDecList(toSelectModeL,
  101.    minNumObj);
  102. ((PruneableDecList)currentRule).buildRule(oldGrowData, 
  103.   oldPruneData);
  104.       } else {
  105. currentRule = new C45PruneableDecList(toSelectModeL, CF,
  106.       minNumObj);
  107. ((C45PruneableDecList)currentRule).buildRule(oldGrowData);
  108.       }
  109.       numRules++;
  110.       // Remove instances from growing data
  111.       newGrowData = new Instances(oldGrowData,
  112.    oldGrowData.numInstances());
  113.       Enumeration enum = oldGrowData.enumerateInstances();
  114.       while (enum.hasMoreElements()) {
  115. Instance instance = (Instance) enum.nextElement();
  116. currentWeight = currentRule.weight(instance);
  117. if (Utils.sm(currentWeight,1)) {
  118.   instance.setWeight(instance.weight()*(1-currentWeight));
  119.   newGrowData.add(instance);
  120. }
  121.       }
  122.       newGrowData.compactify();
  123.       oldGrowData = newGrowData;
  124.       
  125.       // Remove instances from pruning data
  126.       if (reducedErrorPruning) {
  127. newPruneData = new Instances(oldPruneData,
  128.      oldPruneData.numInstances());
  129. enum = oldPruneData.enumerateInstances();
  130. while (enum.hasMoreElements()) {
  131.   Instance instance = (Instance) enum.nextElement();
  132.   currentWeight = currentRule.weight(instance);
  133.   if (Utils.sm(currentWeight,1)) {
  134.     instance.setWeight(instance.weight()*(1-currentWeight));
  135.     newPruneData.add(instance);
  136.   }
  137. }
  138. newPruneData.compactify();
  139. oldPruneData = newPruneData;
  140.       }
  141.       theRules.addElement(currentRule);
  142.     }
  143.   }
  144.   /**
  145.    * Outputs the classifier into a string.
  146.    */
  147.   public String toString(){
  148.     StringBuffer text = new StringBuffer();
  149.     for (int i=0;i<theRules.size();i++)
  150.       text.append((ClassifierDecList)theRules.elementAt(i)+"n");
  151.     text.append("Number of Rules  : t"+theRules.size()+"n");
  152.     return text.toString();
  153.   }
  154.   /** 
  155.    * Classifies an instance.
  156.    *
  157.    * @exception Exception if instance can't be classified
  158.    */
  159.   public double classifyInstance(Instance instance) 
  160.        throws Exception {
  161.     double maxProb = -1;
  162.     double [] sumProbs;
  163.     int maxIndex = 0;
  164.     sumProbs = distributionForInstance(instance);
  165.     for (int j = 0; j < sumProbs.length; j++) {
  166.       if (Utils.gr(sumProbs[j],maxProb)){
  167. maxIndex = j;
  168. maxProb = sumProbs[j];
  169.       }
  170.     }
  171.     return (double)maxIndex;
  172.   }
  173.   /** 
  174.    * Returns the class distribution for an instance.
  175.    *
  176.    * @exception Exception if distribution can't be computed
  177.    */
  178.   public double[] distributionForInstance(Instance instance) 
  179.        throws Exception {
  180.     double [] currentProbs = null;
  181.     double [] sumProbs;
  182.     double currentWeight, weight = 1;
  183.     int i,j;
  184.     // Get probabilities.
  185.     sumProbs = new double [instance.numClasses()];
  186.     i = 0;
  187.     while (Utils.gr(weight,0)){
  188.       currentWeight = 
  189. ((ClassifierDecList)theRules.elementAt(i)).weight(instance);
  190.       if (Utils.gr(currentWeight,0)) {
  191. currentProbs = ((ClassifierDecList)theRules.elementAt(i)).
  192.   distributionForInstance(instance);
  193. for (j = 0; j < sumProbs.length; j++)
  194.   sumProbs[j] += weight*currentProbs[j];
  195. weight = weight*(1-currentWeight);
  196.       }
  197.       i++;
  198.     }
  199.     return sumProbs;
  200.   }
  201.   /**
  202.    * Outputs the number of rules in the classifier.
  203.    */
  204.   public int numRules(){
  205.     return theRules.size();
  206.   }
  207. }