classifiersubseteval.java

来自「Java 编写的多种数据挖掘算法 包括聚类、分类、预处理等」· Java 代码 · 共 670 行 · 第 1/2 页

JAVA
670
字号
/* *    This program is free software; you can redistribute it and/or modify *    it under the terms of the GNU General Public License as published by *    the Free Software Foundation; either version 2 of the License, or *    (at your option) any later version. * *    This program is distributed in the hope that it will be useful, *    but WITHOUT ANY WARRANTY; without even the implied warranty of *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the *    GNU General Public License for more details. * *    You should have received a copy of the GNU General Public License *    along with this program; if not, write to the Free Software *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* *    ClassifierSubsetEval.java *    Copyright (C) 2000 Mark Hall * */package weka.attributeSelection;import weka.classifiers.Classifier;import weka.classifiers.Evaluation;import weka.classifiers.rules.ZeroR;import weka.core.Instance;import weka.core.Instances;import weka.core.Option;import weka.core.OptionHandler;import weka.core.UnsupportedAttributeTypeException;import weka.core.Utils;import weka.filters.Filter;import weka.filters.unsupervised.attribute.Remove;import java.io.File;import java.util.BitSet;import java.util.Enumeration;import java.util.Vector;/** <!-- globalinfo-start --> * Classifier subset evaluator:<br/> * <br/> * Evaluates attribute subsets on training data or a seperate hold out testing set. Uses a classifier to estimate the 'merit' of a set of attributes. * <p/> <!-- globalinfo-end --> * <!-- options-start --> * Valid options are: <p/> *  * <pre> -B &lt;classifier&gt; *  class name of the classifier to use for *  accuracy estimation. Place any *  classifier options LAST on the *  command line following a "--". *  eg. -C weka.classifiers.bayes.NaiveBayes ... -- -K</pre> *  * <pre> -T *  Use the training data to estimate accuracy.</pre> *  * <pre> -H &lt;filename&gt; *  Name of the hold out/test set to  *  estimate accuracy on.</pre> *  * <pre>  * Options specific to scheme weka.classifiers.rules.ZeroR: * </pre> *  * <pre> -D *  If set, classifier is run in debug mode and *  may output additional info to the console</pre> *  <!-- options-end --> * * @author Mark Hall (mhall@cs.waikato.ac.nz) * @version $Revision: 1.14 $ */public class ClassifierSubsetEval   extends HoldOutSubsetEvaluator  implements OptionHandler, ErrorBasedMeritEvaluator {    /** for serialization */  static final long serialVersionUID = 7532217899385278710L;  /** training instances */  private Instances m_trainingInstances;  /** class index */  private int m_classIndex;  /** number of attributes in the training data */  private int m_numAttribs;    /** number of training instances */  private int m_numInstances;  /** holds the classifier to use for error estimates */  private Classifier m_Classifier = new ZeroR();  /** holds the evaluation object to use for evaluating the classifier */  private Evaluation m_Evaluation;  /** the file that containts hold out/test instances */  private File m_holdOutFile = new File("Click to set hold out or "					+"test instances");  /** the instances to test on */  private Instances m_holdOutInstances = null;  /** evaluate on training data rather than seperate hold out/test set */  private boolean m_useTraining = true;  /**   * Returns a string describing this attribute evaluator   * @return a description of the evaluator suitable for   * displaying in the explorer/experimenter gui   */  public String globalInfo() {    return         "Classifier subset evaluator:\n\nEvaluates attribute subsets on training data or a seperate "      + "hold out testing set. Uses a classifier to estimate the 'merit' of a set of attributes.";  }  /**   * Returns an enumeration describing the available options.   *   * @return an enumeration of all the available options.   **/  public Enumeration listOptions () {    Vector newVector = new Vector(3);    newVector.addElement(new Option("\tclass name of the classifier to use for" 				    + "\n\taccuracy estimation. Place any" 				    + "\n\tclassifier options LAST on the" 				    + "\n\tcommand line following a \"--\"." 				    + "\n\teg. -C weka.classifiers.bayes.NaiveBayes ... " 				    + "-- -K", "B", 1, "-B <classifier>"));        newVector.addElement(new Option("\tUse the training data to estimate"				    +" accuracy."				    ,"T",0,"-T"));        newVector.addElement(new Option("\tName of the hold out/test set to "				    +"\n\testimate accuracy on."				    ,"H", 1,"-H <filename>"));    if ((m_Classifier != null) && 	(m_Classifier instanceof OptionHandler)) {      newVector.addElement(new Option("", "", 0, "\nOptions specific to " 				      + "scheme " 				      + m_Classifier.getClass().getName() 				      + ":"));      Enumeration enu = ((OptionHandler)m_Classifier).listOptions();      while (enu.hasMoreElements()) {        newVector.addElement(enu.nextElement());      }    }    return  newVector.elements();  }  /**   * Parses a given list of options. <p/>   *   <!-- options-start -->   * Valid options are: <p/>   *    * <pre> -B &lt;classifier&gt;   *  class name of the classifier to use for   *  accuracy estimation. Place any   *  classifier options LAST on the   *  command line following a "--".   *  eg. -C weka.classifiers.bayes.NaiveBayes ... -- -K</pre>   *    * <pre> -T   *  Use the training data to estimate accuracy.</pre>   *    * <pre> -H &lt;filename&gt;   *  Name of the hold out/test set to    *  estimate accuracy on.</pre>   *    * <pre>    * Options specific to scheme weka.classifiers.rules.ZeroR:   * </pre>   *    * <pre> -D   *  If set, classifier is run in debug mode and   *  may output additional info to the console</pre>   *    <!-- options-end -->   *   * @param options the list of options as an array of strings   * @throws Exception if an option is not supported   */  public void setOptions (String[] options)    throws Exception {    String optionString;    resetOptions();    optionString = Utils.getOption('B', options);        if (optionString.length() == 0) {      throw new Exception("A classifier must be specified with -B option");    }    setClassifier(Classifier.forName(optionString,				     Utils.partitionOptions(options)));    optionString = Utils.getOption('H',options);    if (optionString.length() != 0) {      setHoldOutFile(new File(optionString));    }    setUseTraining(Utils.getFlag('T',options));  }    /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String classifierTipText() {    return "Classifier to use for estimating the accuracy of subsets";  }  /**   * Set the classifier to use for accuracy estimation   *   * @param newClassifier the Classifier to use.   */  public void setClassifier (Classifier newClassifier) {    m_Classifier = newClassifier;  }  /**   * Get the classifier used as the base learner.   *   * @return the classifier used as the classifier   */  public Classifier getClassifier () {    return  m_Classifier;  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String holdOutFileTipText() {    return "File containing hold out/test instances.";  }  /**   * Gets the file that holds hold out/test instances.   * @return File that contains hold out instances   */  public File getHoldOutFile() {    return m_holdOutFile;  }  /**   * Set the file that contains hold out/test instances   * @param h the hold out file   */  public void setHoldOutFile(File h) {    m_holdOutFile = h;  }  /**   * Returns the tip text for this property   * @return tip text for this property suitable for   * displaying in the explorer/experimenter gui   */  public String useTrainingTipText() {    return "Use training data instead of hold out/test instances.";  }  /**   * Get if training data is to be used instead of hold out/test data   * @return true if training data is to be used instead of hold out data   */  public boolean getUseTraining() {    return m_useTraining;  }  /**   * Set if training data is to be used instead of hold out/test data   * @param t true if training data is to be used instead of hold out data   */  public void setUseTraining(boolean t) {    m_useTraining = t;  }  /**   * Gets the current settings of ClassifierSubsetEval   *   * @return an array of strings suitable for passing to setOptions()   */  public String[] getOptions () {    String[] classifierOptions = new String[0];    if ((m_Classifier != null) && 	(m_Classifier instanceof OptionHandler)) {      classifierOptions = ((OptionHandler)m_Classifier).getOptions();    }    String[] options = new String[6 + classifierOptions.length];    int current = 0;    if (getClassifier() != null) {      options[current++] = "-B";      options[current++] = getClassifier().getClass().getName();    }    if (getUseTraining()) {      options[current++] = "-T";    }    options[current++] = "-H"; options[current++] = getHoldOutFile().getPath();    options[current++] = "--";    System.arraycopy(classifierOptions, 0, options, current, 		     classifierOptions.length);    current += classifierOptions.length;        while (current < options.length) {      options[current++] = "";    }    return  options;  }  /**

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?