001: /*
002: * This program is free software; you can redistribute it and/or modify
003: * it under the terms of the GNU General Public License as published by
004: * the Free Software Foundation; either version 2 of the License, or
005: * (at your option) any later version.
006: *
007: * This program is distributed in the hope that it will be useful,
008: * but WITHOUT ANY WARRANTY; without even the implied warranty of
009: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
010: * GNU General Public License for more details.
011: *
012: * You should have received a copy of the GNU General Public License
013: * along with this program; if not, write to the Free Software
014: * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
015: */
016:
017: /*
018: * IteratedSingleClassifierEnhancer.java
019: * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
020: *
021: */
022:
023: package weka.classifiers;
024:
025: import weka.core.Instances;
026: import weka.core.Option;
027: import weka.core.Utils;
028:
029: import java.util.Enumeration;
030: import java.util.Vector;
031:
032: /**
033: * Abstract utility class for handling settings common to
034: * meta classifiers that build an ensemble from a single base learner.
035: *
036: * @author Eibe Frank (eibe@cs.waikato.ac.nz)
037: * @version $Revision: 1.4 $
038: */
039: public abstract class IteratedSingleClassifierEnhancer extends
040: SingleClassifierEnhancer {
041:
042: /** for serialization */
043: private static final long serialVersionUID = -6217979135443319724L;
044:
045: /** Array for storing the generated base classifiers. */
046: protected Classifier[] m_Classifiers;
047:
048: /** The number of iterations. */
049: protected int m_NumIterations = 10;
050:
051: /**
052: * Stump method for building the classifiers.
053: *
054: * @param data the training data to be used for generating the
055: * bagged classifier.
056: * @exception Exception if the classifier could not be built successfully
057: */
058: public void buildClassifier(Instances data) throws Exception {
059:
060: if (m_Classifier == null) {
061: throw new Exception(
062: "A base classifier has not been specified!");
063: }
064: m_Classifiers = Classifier.makeCopies(m_Classifier,
065: m_NumIterations);
066: }
067:
068: /**
069: * Returns an enumeration describing the available options.
070: *
071: * @return an enumeration of all the available options.
072: */
073: public Enumeration listOptions() {
074:
075: Vector newVector = new Vector(2);
076:
077: newVector.addElement(new Option("\tNumber of iterations.\n"
078: + "\t(default 10)", "I", 1, "-I <num>"));
079:
080: Enumeration enu = super .listOptions();
081: while (enu.hasMoreElements()) {
082: newVector.addElement(enu.nextElement());
083: }
084: return newVector.elements();
085: }
086:
087: /**
088: * Parses a given list of options. Valid options are:<p>
089: *
090: * -W classname <br>
091: * Specify the full class name of the base learner.<p>
092: *
093: * -I num <br>
094: * Set the number of iterations (default 10). <p>
095: *
096: * Options after -- are passed to the designated classifier.<p>
097: *
098: * @param options the list of options as an array of strings
099: * @exception Exception if an option is not supported
100: */
101: public void setOptions(String[] options) throws Exception {
102:
103: String iterations = Utils.getOption('I', options);
104: if (iterations.length() != 0) {
105: setNumIterations(Integer.parseInt(iterations));
106: } else {
107: setNumIterations(10);
108: }
109:
110: super .setOptions(options);
111: }
112:
113: /**
114: * Gets the current settings of the classifier.
115: *
116: * @return an array of strings suitable for passing to setOptions
117: */
118: public String[] getOptions() {
119:
120: String[] super Options = super .getOptions();
121: String[] options = new String[super Options.length + 2];
122:
123: int current = 0;
124: options[current++] = "-I";
125: options[current++] = "" + getNumIterations();
126:
127: System.arraycopy(super Options, 0, options, current,
128: super Options.length);
129:
130: return options;
131: }
132:
133: /**
134: * Returns the tip text for this property
135: * @return tip text for this property suitable for
136: * displaying in the explorer/experimenter gui
137: */
138: public String numIterationsTipText() {
139: return "The number of iterations to be performed.";
140: }
141:
142: /**
143: * Sets the number of bagging iterations
144: */
145: public void setNumIterations(int numIterations) {
146:
147: m_NumIterations = numIterations;
148: }
149:
150: /**
151: * Gets the number of bagging iterations
152: *
153: * @return the maximum number of bagging iterations
154: */
155: public int getNumIterations() {
156:
157: return m_NumIterations;
158: }
159: }
|