⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stacking.java

📁 代码是一个分类器的实现,其中使用了部分weka的源代码。可以将项目导入eclipse运行
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* *    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. *//* *    Stacking.java *    Copyright (C) 1999 Eibe Frank * */package weka.classifiers.meta;import weka.classifiers.Classifier;import weka.classifiers.RandomizableMultipleClassifiersCombiner;import weka.classifiers.rules.ZeroR;import weka.core.Attribute;import weka.core.Capabilities;import weka.core.FastVector;import weka.core.Instance;import weka.core.Instances;import weka.core.Option;import weka.core.OptionHandler;import weka.core.TechnicalInformation;import weka.core.TechnicalInformationHandler;import weka.core.Utils;import weka.core.TechnicalInformation.Field;import weka.core.TechnicalInformation.Type;import java.util.Enumeration;import java.util.Random;import java.util.Vector;/** <!-- globalinfo-start --> * Combines several classifiers using the stacking method. Can do classification or regression.<br/> * <br/> * For more information, see<br/> * <br/> * David H. Wolpert (1992). Stacked generalization. Neural Networks. 5:241-259. * <p/> <!-- globalinfo-end --> * <!-- technical-bibtex-start --> * BibTeX: * <pre> * &#64;article{Wolpert1992, *    author = {David H. Wolpert}, *    journal = {Neural Networks}, *    pages = {241-259}, *    publisher = {Pergamon Press}, *    title = {Stacked generalization}, *    volume = {5}, *    year = {1992} * } * </pre> * <p/> <!-- technical-bibtex-end --> * <!-- options-start --> * Valid options are: <p/> *  * <pre> -M &lt;scheme specification&gt; *  Full name of meta classifier, followed by options. *  (default: "weka.classifiers.rules.Zero")</pre> *  * <pre> -X &lt;number of folds&gt; *  Sets the number of cross-validation folds.</pre> *  * <pre> -S &lt;num&gt; *  Random number seed. *  (default 1)</pre> *  * <pre> -B &lt;classifier specification&gt; *  Full class name of classifier to include, followed *  by scheme options. May be specified multiple times. *  (default: "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 Eibe Frank (eibe@cs.waikato.ac.nz) * @version $Revision: 1.30 $  */public class Stacking   extends RandomizableMultipleClassifiersCombiner  implements TechnicalInformationHandler {  /** for serialization */  static final long serialVersionUID = 5134738557155845452L;    /** The meta classifier */  protected Classifier m_MetaClassifier = new ZeroR();   /** Format for meta data */  protected Instances m_MetaFormat = null;  /** Format for base data */  protected Instances m_BaseFormat = null;  /** Set the number of folds for the cross-validation */  protected int m_NumFolds = 10;      /**   * Returns a string describing classifier   * @return a description suitable for   * displaying in the explorer/experimenter gui   */  public String globalInfo() {    return "Combines several classifiers using the stacking method. "      + "Can do classification or regression.\n\n"      + "For more information, see\n\n"      + getTechnicalInformation().toString();  }  /**   * Returns an instance of a TechnicalInformation object, containing    * detailed information about the technical background of this class,   * e.g., paper reference or book this class is based on.   *    * @return the technical information about this class   */  public TechnicalInformation getTechnicalInformation() {    TechnicalInformation 	result;        result = new TechnicalInformation(Type.ARTICLE);    result.setValue(Field.AUTHOR, "David H. Wolpert");    result.setValue(Field.YEAR, "1992");    result.setValue(Field.TITLE, "Stacked generalization");    result.setValue(Field.JOURNAL, "Neural Networks");    result.setValue(Field.VOLUME, "5");    result.setValue(Field.PAGES, "241-259");    result.setValue(Field.PUBLISHER, "Pergamon Press");        return result;  }    /**   * Returns an enumeration describing the available options.   *   * @return an enumeration of all the available options.   */  public Enumeration listOptions() {        Vector newVector = new Vector(2);    newVector.addElement(new Option(	      metaOption(),	      "M", 0, "-M <scheme specification>"));    newVector.addElement(new Option(	      "\tSets the number of cross-validation folds.",	      "X", 1, "-X <number of folds>"));    Enumeration enu = super.listOptions();    while (enu.hasMoreElements()) {      newVector.addElement(enu.nextElement());    }    return newVector.elements();  }  /**   * String describing option for setting meta classifier   *    * @return the string describing the option   */  protected String metaOption() {    return "\tFull name of meta classifier, followed by options.\n" +      "\t(default: \"weka.classifiers.rules.Zero\")";  }  /**   * Parses a given list of options. <p/>   *   <!-- options-start -->   * Valid options are: <p/>   *    * <pre> -M &lt;scheme specification&gt;   *  Full name of meta classifier, followed by options.   *  (default: "weka.classifiers.rules.Zero")</pre>   *    * <pre> -X &lt;number of folds&gt;   *  Sets the number of cross-validation folds.</pre>   *    * <pre> -S &lt;num&gt;   *  Random number seed.   *  (default 1)</pre>   *    * <pre> -B &lt;classifier specification&gt;   *  Full class name of classifier to include, followed   *  by scheme options. May be specified multiple times.   *  (default: "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 numFoldsString = Utils.getOption('X', options);    if (numFoldsString.length() != 0) {      setNumFolds(Integer.parseInt(numFoldsString));    } else {      setNumFolds(10);    }    processMetaOptions(options);    super.setOptions(options);  }  /**   * Process options setting meta classifier.   *    * @param options the options to parse   * @throws Exception if the parsing fails   */  protected void processMetaOptions(String[] options) throws Exception {    String classifierString = Utils.getOption('M', options);    String [] classifierSpec = Utils.splitOptions(classifierString);    String classifierName;    if (classifierSpec.length == 0) {      classifierName = "weka.classifiers.rules.ZeroR";    } else {      classifierName = classifierSpec[0];      classifierSpec[0] = "";    }    setMetaClassifier(Classifier.forName(classifierName, classifierSpec));  }  /**   * Gets the current settings of the Classifier.   *   * @return an array of strings suitable for passing to setOptions   */  public String [] getOptions() {    String [] superOptions = super.getOptions();    String [] options = new String [superOptions.length + 4];    int current = 0;    options[current++] = "-X"; options[current++] = "" + getNumFolds();    options[current++] = "-M";    options[current++] = getMetaClassifier().getClass().getName() + " "      + Utils.joinOptions(((OptionHandler)getMetaClassifier()).getOptions());

⌨️ 快捷键说明

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