PART.java
Upload User: rhdiban
Upload Date: 2013-08-09
Package Size: 15085k
Code Size: 10k
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.  *    PART.java
  18.  *    Copyright (C) 1999 Eibe Frank
  19.  *
  20.  */
  21. package weka.classifiers.j48;
  22. import java.util.*;
  23. import weka.core.*;
  24. import weka.classifiers.*;
  25. /**
  26.  * Class for generating a PART decision list. For more information, see<p>
  27.  *
  28.  * Eibe Frank and Ian H. Witten (1998).  <a
  29.  * href="http://www.cs.waikato.ac.nz/~eibe/pubs/ML98-57.ps.gz">Generating
  30.  * Accurate Rule Sets Without Global Optimization.</a> In Shavlik, J.,
  31.  * ed., <i>Machine Learning: Proceedings of the Fifteenth
  32.  * International Conference</i>, Morgan Kaufmann Publishers, San
  33.  * Francisco, CA. <p>
  34.  *
  35.  * Valid options are: <p>
  36.  *
  37.  * -C confidence <br>
  38.  * Set confidence threshold for pruning. (Default: 0.25) <p>
  39.  *
  40.  * -M number <br>
  41.  * Set minimum number of instances per leaf. (Default: 2) <p>
  42.  *
  43.  * -R <br>
  44.  * Use reduced error pruning. <p>
  45.  *
  46.  * -N number <br>
  47.  * Set number of folds for reduced error pruning. One fold is
  48.  * used as the pruning set. (Default: 3) <p>
  49.  *
  50.  * -B <br>
  51.  * Use binary splits for nominal attributes. <p>
  52.  *
  53.  * @author Eibe Frank (eibe@cs.waikato.ac.nz)
  54.  * @version $Revision: 1.14 $
  55.  */
  56. public class PART extends DistributionClassifier implements OptionHandler,
  57.   WeightedInstancesHandler, Summarizable, AdditionalMeasureProducer {
  58.   /** The decision list */
  59.   private MakeDecList m_root;
  60.   /** Confidence level */
  61.   private float m_CF = 0.25f;
  62.   /** Minimum number of objects */
  63.   private int m_minNumObj = 2;
  64.   /** Use reduced error pruning? */
  65.   private boolean m_reducedErrorPruning = false;
  66.   /** Number of folds for reduced error pruning. */
  67.   private int m_numFolds = 3;
  68.   /** Binary splits on nominal attributes? */
  69.   private boolean m_binarySplits = false;
  70.   
  71.   /**
  72.    * Generates the classifier.
  73.    *
  74.    * @exception Exception if classifier can't be built successfully
  75.    */
  76.   public void buildClassifier(Instances instances) 
  77.        throws Exception{
  78.     ModelSelection modSelection;  
  79.     if (m_binarySplits)
  80.       modSelection = new BinC45ModelSelection(m_minNumObj, instances);
  81.     else
  82.       modSelection = new C45ModelSelection(m_minNumObj, instances);
  83.     if (m_reducedErrorPruning) 
  84.       m_root = new MakeDecList(modSelection, m_numFolds, m_minNumObj);
  85.     else
  86.       m_root = new MakeDecList(modSelection, m_CF, m_minNumObj);
  87.     m_root.buildClassifier(instances);
  88.     if (m_binarySplits) {
  89.       ((BinC45ModelSelection)modSelection).cleanup();
  90.     } else {
  91.       ((C45ModelSelection)modSelection).cleanup();
  92.     }
  93.   }
  94.   /**
  95.    * Classifies an instance.
  96.    *
  97.    * @exception Exception if instance can't be classified successfully
  98.    */
  99.   public double classifyInstance(Instance instance) 
  100.        throws Exception {
  101.     return m_root.classifyInstance(instance);
  102.   }
  103.   /** 
  104.    * Returns class probabilities for an instance.
  105.    *
  106.    * @exception Exception if the distribution can't be computed successfully
  107.    */
  108.   public final double [] distributionForInstance(Instance instance) 
  109.        throws Exception {
  110.     return m_root.distributionForInstance(instance);
  111.   }
  112.   /**
  113.    * Returns an enumeration describing the available options
  114.    *
  115.    * Valid options are: <p>
  116.    *
  117.    * -C confidence <br>
  118.    * Set confidence threshold for pruning. (Default: 0.25) <p>
  119.    *
  120.    * -M number <br>
  121.    * Set minimum number of instances per leaf. (Default: 2) <p>
  122.    *
  123.    * -R <br>
  124.    * Use reduced error pruning. <p>
  125.    *
  126.    * -N number <br>
  127.    * Set number of folds for reduced error pruning. One fold is
  128.    * used as the pruning set. (Default: 3) <p>
  129.    *
  130.    * -B <br>
  131.    * Use binary splits for nominal attributes. <p>
  132.    *
  133.    * @return an enumeration of all the available options
  134.    */
  135.   public Enumeration listOptions() {
  136.     Vector newVector = new Vector(5);
  137.     newVector.
  138. addElement(new Option("tSet confidence threshold for pruning.n" +
  139.       "t(default 0.25)",
  140.       "C", 1, "-C <pruning confidence>"));
  141.     newVector.
  142. addElement(new Option("tSet minimum number of objects per leaf.n" +
  143.       "t(default 2)",
  144.       "M", 1, "-M <minimum number of objects>"));
  145.     newVector.
  146. addElement(new Option("tUse reduced error pruning.",
  147.       "R", 0, "-R"));
  148.     newVector.
  149. addElement(new Option("tSet number of folds for reduced errorn" +
  150.       "tpruning. One fold is used as pruning set.n" +
  151.       "t(default 3)",
  152.       "N", 1, "-N <number of folds>"));
  153.     newVector.
  154. addElement(new Option("tUse binary splits only.",
  155.       "B", 0, "-B"));
  156.     return newVector.elements();
  157.   }
  158.   /**
  159.    * Parses a given list of options.
  160.    *
  161.    * @param options the list of options as an array of strings
  162.    * @exception Exception if an option is not supported
  163.    */
  164.   public void setOptions(String[] options) throws Exception{
  165.     // Pruning options
  166.     m_reducedErrorPruning = Utils.getFlag('R', options);
  167.     m_binarySplits = Utils.getFlag('B', options);
  168.     String confidenceString = Utils.getOption('C', options);
  169.     if (confidenceString.length() != 0) {
  170.       if (m_reducedErrorPruning) {
  171. throw new Exception("Setting CF doesn't make sense " +
  172.     "for reduced error pruning.");
  173.       } else {
  174. m_CF = (new Float(confidenceString)).floatValue();
  175. if ((m_CF <= 0) || (m_CF >= 1)) {
  176.   throw new Exception("CF has to be greater than zero and smaller than one!");
  177.       }
  178.     } else {
  179.       m_CF = 0.25f;
  180.     }
  181.     String numFoldsString = Utils.getOption('N', options);
  182.     if (numFoldsString.length() != 0) {
  183.       if (!m_reducedErrorPruning) {
  184. throw new Exception("Setting the number of folds" +
  185.     " does only make sense for" +
  186.     " reduced error pruning.");
  187.       } else {
  188. m_numFolds = Integer.parseInt(numFoldsString);
  189.       }
  190.     } else {
  191.       m_numFolds = 3;
  192.     }
  193.     // Other options
  194.     String minNumString = Utils.getOption('M', options);
  195.     if (minNumString.length() != 0) {
  196.       m_minNumObj = Integer.parseInt(minNumString);
  197.     } else {
  198.       m_minNumObj = 2;
  199.     }
  200.   }
  201.   /**
  202.    * Gets the current settings of the Classifier.
  203.    *
  204.    * @return an array of strings suitable for passing to setOptions
  205.    */
  206.   public String [] getOptions() {
  207.     String [] options = new String [6];
  208.     int current = 0;
  209.     if (m_reducedErrorPruning) {
  210.       options[current++] = "-R";
  211.       options[current++] = "-N"; options[current++] = "" + m_numFolds;
  212.     } else {
  213.       options[current++] = "-C"; options[current++] = "" + m_CF;
  214.     }
  215.     if (m_binarySplits) {
  216.       options[current++] = "-B";
  217.     }
  218.     options[current++] = "-M"; options[current++] = "" + m_minNumObj;
  219.     while (current < options.length) {
  220.       options[current++] = "";
  221.     }
  222.     return options;
  223.   }
  224.   /**
  225.    * Returns a description of the classifier
  226.    */
  227.   public String toString() {
  228.     if (m_root == null) {
  229.       return "No classifier built";
  230.     }
  231.     return "PART decision listn------------------nn" + m_root.toString();
  232.   }
  233.   
  234.   /**
  235.    * Returns a superconcise version of the model
  236.    */
  237.   public String toSummaryString() {
  238.     return "Number of rules: " + m_root.numRules() + "n";
  239.   }
  240.   
  241.   /**
  242.    * Return the number of rules.
  243.    * @return the number of rules
  244.    */
  245.   public double measureNumRules() {
  246.     return m_root.numRules();
  247.   }
  248.   
  249.   /**
  250.    * Returns an enumeration of the additional measure names
  251.    * @return an enumeration of the measure names
  252.    */
  253.   public Enumeration enumerateMeasures() {
  254.     Vector newVector = new Vector(1);
  255.     newVector.addElement("measureNumRules");
  256.     return newVector.elements();
  257.   }
  258.   /**
  259.    * Returns the value of the named measure
  260.    * @param measureName the name of the measure to query for its value
  261.    * @return the value of the named measure
  262.    * @exception IllegalArgumentException if the named measure is not supported
  263.    */
  264.   public double getMeasure(String additionalMeasureName) {
  265.     if (additionalMeasureName.compareTo("measureNumRules") == 0) {
  266.       return measureNumRules();
  267.     } else {
  268.       throw new IllegalArgumentException(additionalMeasureName 
  269.   + " not supported (PART)");
  270.     }
  271.   }
  272.   /**
  273.    * Get the value of CF.
  274.    *
  275.    * @return Value of CF.
  276.    */
  277.   public float getConfidenceFactor() {
  278.     
  279.     return m_CF;
  280.   }
  281.   
  282.   /**
  283.    * Set the value of CF.
  284.    *
  285.    * @param v  Value to assign to CF.
  286.    */
  287.   public void setConfidenceFactor(float v) {
  288.     
  289.     m_CF = v;
  290.   }
  291.   
  292.   /**
  293.    * Get the value of minNumObj.
  294.    *
  295.    * @return Value of minNumObj.
  296.    */
  297.   public int getMinNumObj() {
  298.     
  299.     return m_minNumObj;
  300.   }
  301.   
  302.   /**
  303.    * Set the value of minNumObj.
  304.    *
  305.    * @param v  Value to assign to minNumObj.
  306.    */
  307.   public void setMinNumObj(int v) {
  308.     
  309.     m_minNumObj = v;
  310.   }
  311.   
  312.   /**
  313.    * Get the value of reducedErrorPruning.
  314.    *
  315.    * @return Value of reducedErrorPruning.
  316.    */
  317.   public boolean getReducedErrorPruning() {
  318.     
  319.     return m_reducedErrorPruning;
  320.   }
  321.   
  322.   /**
  323.    * Set the value of reducedErrorPruning.
  324.    *
  325.    * @param v  Value to assign to reducedErrorPruning.
  326.    */
  327.   public void setReducedErrorPruning(boolean v) {
  328.     
  329.     m_reducedErrorPruning = v;
  330.   }
  331.   
  332.   /**
  333.    * Get the value of numFolds.
  334.    *
  335.    * @return Value of numFolds.
  336.    */
  337.   public int getNumFolds() {
  338.     
  339.     return m_numFolds;
  340.   }
  341.   
  342.   /**
  343.    * Set the value of numFolds.
  344.    *
  345.    * @param v  Value to assign to numFolds.
  346.    */
  347.   public void setNumFolds(int v) {
  348.     
  349.     m_numFolds = v;
  350.   }
  351.   
  352.   /**
  353.    * Get the value of binarySplits.
  354.    *
  355.    * @return Value of binarySplits.
  356.    */
  357.   public boolean getBinarySplits() {
  358.     
  359.     return m_binarySplits;
  360.   }
  361.   
  362.   /**
  363.    * Set the value of binarySplits.
  364.    *
  365.    * @param v  Value to assign to binarySplits.
  366.    */
  367.   public void setBinarySplits(boolean v) {
  368.     
  369.     m_binarySplits = v;
  370.   }
  371.   
  372.   /**
  373.    * Main method for testing this class.
  374.    *
  375.    * @param String options 
  376.    */
  377.   public static void main(String [] argv){
  378.     try {
  379.       System.out.println(Evaluation.evaluateModel(new PART(), argv));
  380.     } catch (Exception e) {
  381.       System.out.println(e.getMessage());
  382.     }
  383.   }
  384. }
  385.