📄 stacking.java
字号:
/* * 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.Evaluation;import weka.classifiers.Classifier;import weka.classifiers.DistributionClassifier;import weka.classifiers.rules.ZeroR;import java.io.*;import java.util.*;import weka.core.*;/** * Implements stacking. For more information, see<p> * * David H. Wolpert (1992). <i>Stacked * generalization</i>. Neural Networks, 5:241-259, Pergamon Press. <p> * * Valid options are:<p> * * -X num_folds <br> * The number of folds for the cross-validation (default 10).<p> * * -S seed <br> * Random number seed (default 1).<p> * * -B classifierstring <br> * Classifierstring should contain the full class name of a base scheme * followed by options to the classifier. * (required, option should be used once for each classifier).<p> * * -M classifierstring <br> * Classifierstring for the meta classifier. Same format as for base * classifiers. (required) <p> * * @author Eibe Frank (eibe@cs.waikato.ac.nz) * @version $Revision: 1.1.1.1 $ */public class Stacking extends Classifier implements OptionHandler { /** The meta classifier. */ protected Classifier m_MetaClassifier = new weka.classifiers.rules.ZeroR(); /** The base classifiers. */ protected Classifier [] m_BaseClassifiers = { new weka.classifiers.rules.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; /** Random number seed */ protected int m_Seed = 1; /** * Returns an enumeration describing the available options. * * @return an enumeration of all the available options. */ public Enumeration listOptions() { Vector newVector = new Vector(4); newVector.addElement(new Option( "\tFull class name of base classifiers to include, followed " + "by scheme options\n" + "\t(may be specified multiple times).\n" + "\teg: \"weka.classifiers.bayes.NaiveBayes -K\"", "B", 1, "-B <scheme specification>")); newVector.addElement(new Option( "\tFull name of meta classifier, followed by options.", "M", 0, "-M <scheme specification>")); newVector.addElement(new Option( "\tSets the number of cross-validation folds.", "X", 1, "-X <number of folds>")); newVector.addElement(new Option( "\tSets the random number seed.", "S", 1, "-S <random number seed>")); return newVector.elements(); } /** * Parses a given list of options. Valid options are:<p> * * -X num_folds <br> * The number of folds for the cross-validation (default 10).<p> * * -S seed <br> * Random number seed (default 1).<p> * * -B classifierstring <br> * Classifierstring should contain the full class name of a base scheme * followed by options to the classifier. * (required, option should be used once for each classifier).<p> * * -M classifierstring <br> * Classifierstring for the meta classifier. Same format as for base * classifiers. (required) <p> * * @param options the list of options as an array of strings * @exception 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); } String randomString = Utils.getOption('S', options); if (randomString.length() != 0) { setSeed(Integer.parseInt(randomString)); } else { setSeed(1); } // Iterate through the schemes FastVector classifiers = new FastVector(); while (true) { String classifierString = Utils.getOption('B', options); if (classifierString.length() == 0) { break; } String [] classifierSpec = Utils.splitOptions(classifierString); if (classifierSpec.length == 0) { throw new Exception("Invalid classifier specification string"); } String classifierName = classifierSpec[0]; classifierSpec[0] = ""; classifiers.addElement(Classifier.forName(classifierName, classifierSpec)); } if (classifiers.size() == 0) { throw new Exception("At least one base classifier must be specified" + " with the -B option."); } else { Classifier [] classifiersArray = new Classifier [classifiers.size()]; for (int i = 0; i < classifiersArray.length; i++) { classifiersArray[i] = (Classifier) classifiers.elementAt(i); } setBaseClassifiers(classifiersArray); } String classifierString = Utils.getOption('M', options); String [] classifierSpec = Utils.splitOptions(classifierString); if (classifierSpec.length == 0) { throw new Exception("Meta classifier has to be provided."); } String 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 [] options = new String[6]; int current = 0; if (m_BaseClassifiers.length != 0) { options = new String [m_BaseClassifiers.length * 2 + 6]; for (int i = 0; i < m_BaseClassifiers.length; i++) { options[current++] = "-B"; options[current++] = "" + getBaseClassifierSpec(i); } } options[current++] = "-X"; options[current++] = "" + getNumFolds(); options[current++] = "-S"; options[current++] = "" + getSeed(); if (getMetaClassifier() != null) { options[current++] = "-M"; options[current++] = getClassifierSpec(getMetaClassifier()); } while (current < options.length) { options[current++] = ""; } return options; } /** * Sets the seed for random number generation. * * @param seed the random number seed */ public void setSeed(int seed) { m_Seed = seed;; } /** * Gets the random number seed. * * @return the random number seed */ public int getSeed() { return m_Seed; } /** * Gets the number of folds for the cross-validation. * * @return the number of folds for the cross-validation */ public int getNumFolds() { return m_NumFolds; } /** * Sets the number of folds for the cross-validation. * * @param numFolds the number of folds for the cross-validation * @exception Exception if parameter illegal */ public void setNumFolds(int numFolds) throws Exception { if (numFolds < 0) { throw new Exception("Stacking: Number of cross-validation " + "folds must be positive."); } m_NumFolds = numFolds; } /** * Sets the list of possible classifers to choose from. * * @param classifiers an array of classifiers with all options set. */ public void setBaseClassifiers(Classifier [] classifiers) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -