📄 dec.java
字号:
/* * DEC.java (Diverse Ensemble Classifier) * Copyright (C) 2002 Prem Melville * * UNDER DEVELOPMENT */package weka.classifiers.meta;import weka.classifiers.*;import com.jmage.*;import java.util.*;import weka.core.*;import weka.experiment.*;/*import weka.core.Instance;import weka.core.Instances;import weka.core.OptionHandler;import weka.core.Option;import weka.core.Utils;*//** * Class for creating Diverse Ensembles of a Classifier * * Valid options are:<p> * * -W classname <br> * Specify the full class name of a weak as the basis for * DEC (required).<p> * * -I num <br> * Set the number of DEC iterations (default 50). <p> * * -N num <br> * Specify the desired size of the committee (default 15). <p> * * -S seed <br> * Random number seed for generating random examples (default random). <p> * * -R num <br> * Number of random instances to add at each iteration (default 20). <p> * * Options after -- are passed to the designated classifier.<p> * * @author Prem Melville (melville@cs.utexas.edu) * @version 1.0 */public class DEC extends EnsembleClassifier implements OptionHandler{ /** Use weights for committe votes - default equal wts*/ protected int m_UseWeights = 0; /** The model base classifier to use */ protected Classifier m_Classifier = new weka.classifiers.trees.j48.J48(); /** Vector of classifiers that make up the committee */ Vector committee=null; /** The number of iterations. */ protected int m_NumIterations = 50; /** The number of iterations. */ protected int m_DesiredSize = 15; /** The seed for random number generation. */ protected int m_Seed = 0; /** Number of random instances to add at each iteration. */ protected double m_RandomSize = 20.0 ; /** Confidence threshold above committee decisions are to be trusted. */ protected double m_Threshold = 1.0 ; /** Possible methods to use for labeling randomly generated instances. */ int LOW_PROB = 0, HIGH_PROB = 1, LEAST_LIKELY = 2, MOST_LIKELY = 3; /** Possible methods for creation of artificial data */ int UNIFORM = 0, TRAINING_DIST = 1, MIXED = 2; /** Method to use for creation of artificial data */ protected int m_DataCreationMethod = TRAINING_DIST; /** Method to use for labeling randomly generated instances. */ protected int labeling_method = LOW_PROB; /** Random number generator */ Random random = new Random(0); /** Attribute statistics */ HashMap attribute_stats; /** * Returns an enumeration describing the available options * * @return an enumeration of all the available options */ public Enumeration listOptions() { Vector newVector = new Vector(7); newVector.addElement(new Option( "\tNumber of DEC iterations.\n" + "\t(default 50)", "I", 1, "-I <num>")); newVector.addElement(new Option( "\tFull name of base classifier.\n" + "\teg: weka.classifiers.NaiveBayes", "W", 1, "-W")); newVector.addElement(new Option( "\tSeed for random number generator.\n" + "\t(default 0)", "S", 1, "-S")); newVector.addElement(new Option( "\rDesired size of committee.\n" + "\t(default 15)", "N", 1, "-N")); newVector.addElement(new Option( "\tConfidence threshold above committee decisions are to be trusted.\n" + "\t(default 1.0)", "C", 1, "-C")); newVector.addElement(new Option( "\tNumber of random instances to add at each iteration.\n R=-1 uses the training set size.\n" + "\t(default 20)", "R", 1, "-R")); newVector.addElement(new Option( "\tMethod to use for artificial data generation (0=Uniform, 1=Training Distribution, 2=Mixed.\n" + "\t(default 1)", "A", 1, "-A")); newVector.addElement(new Option( "\tUse weights for committee votes (0=no weights, 1=proportional to accuracy).\n" + "\t(default 0)", "V", 1, "-V")); if ((m_Classifier != null) && (m_Classifier instanceof OptionHandler)) { newVector.addElement(new Option( "", "", 0, "\nOptions specific to classifier " + m_Classifier.getClass().getName() + ":")); Enumeration enum = ((OptionHandler)m_Classifier).listOptions(); while (enum.hasMoreElements()) { newVector.addElement(enum.nextElement()); } } return newVector.elements(); } /** * Parses a given list of options. Valid options are:<p> * * -W classname <br> * Specify the full class name of a weak classifier as the basis for * bagging (required).<p> * * -I num <br> * Set the number of bagging iterations (default 50). <p> * * -S seed <br> * Random number seed for resampling (default 0).<p> * * -N num <br> * Specify the desired size of the committee (default 15). <p> * * -R num <br> * Number of random instances to add at each iteration (default 5). <p> * * Options after -- are passed to the designated classifier.<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 bagIterations = Utils.getOption('I', options); if (bagIterations.length() != 0) { setNumIterations(Integer.parseInt(bagIterations)); } else { setNumIterations(50); } String seed = Utils.getOption('S', options); if (seed.length() != 0) { setSeed(Integer.parseInt(seed)); } else { setSeed(0); } String desired_size = Utils.getOption('N', options); if (desired_size.length() != 0) { setDesiredSize(Integer.parseInt(desired_size)); } else { setDesiredSize(15); } String rnd_size = Utils.getOption('R', options); if (rnd_size.length() != 0) { setRandomSize(Double.parseDouble(rnd_size)); } else { setRandomSize(20); } String threshold_str = Utils.getOption('C', options); if (threshold_str.length() != 0) { setThreshold(Double.parseDouble(threshold_str)); } else { setThreshold(1.0); } //}catch (Exception e) { e.printStackTrace();}; String data_create_str = Utils.getOption('A', options); if (data_create_str.length() != 0) { setDataCreationMethod(Integer.parseInt(data_create_str)); } else { setDataCreationMethod(TRAINING_DIST); } String use_weights_str = Utils.getOption('V', options); if(use_weights_str.length() != 0) { setUseWeights(Integer.parseInt(use_weights_str)); } else { setUseWeights(0); } String classifierName = Utils.getOption('W', options); if (classifierName.length() == 0) { throw new Exception("A classifier must be specified with" + " the -W option."); //classifierName = default_classifier_name; } setClassifier(Classifier.forName(classifierName, Utils.partitionOptions(options))); } /** * Gets the current settings of the Classifier. * * @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 [classifierOptions.length + 17]; int current = 0; options[current++] = "-S"; options[current++] = "" + getSeed(); options[current++] = "-I"; options[current++] = "" + getNumIterations(); options[current++] = "-N"; options[current++] = "" + getDesiredSize(); options[current++] = "-R"; options[current++] = "" + getRandomSize(); options[current++] = "-C"; options[current++] = "" + getThreshold(); options[current++] = "-A"; options[current++] = "" + getDataCreationMethod(); options[current++] = "-V"; options[current++] = "" + getUseWeights(); if (getClassifier() != null) { options[current++] = "-W"; options[current++] = getClassifier().getClass().getName(); } options[current++] = "--"; System.arraycopy(classifierOptions, 0, options, current, classifierOptions.length); current += classifierOptions.length; while (current < options.length) { options[current++] = ""; } return options; } /** * Set flag for using weights for committee votes. * * @param newUseWeights flag for using weights for committee votes. */ public void setUseWeights(int newUseWeights){ m_UseWeights = newUseWeights; } /** * Get flag for using weights for committee votes. * * @return flag for using weights for committee votes */ public int getUseWeights(){ return m_UseWeights; } /** * Set the classifier for bagging. * * @param newClassifier the Classifier to use. */ public void setClassifier(Classifier newClassifier) { m_Classifier = newClassifier; } /** * Get the classifier used as the classifier * * @return the classifier used as the classifier */ public Classifier getClassifier() { return m_Classifier; } /** * Method to use for creating artificial data * * @return Method to use for creating artificial data */ public int getDataCreationMethod() { return m_DataCreationMethod; } /** * Sets method to use for creating artificial data * * @param method the method to use for creating artificial data */ public void setDataCreationMethod(int method) { m_DataCreationMethod = method; } /** * Number of random instances to add at each iteration. * * @return Number of random instances to add at each iteration */ public double getRandomSize() { return m_RandomSize; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -