[29] | 1 | /* |
---|
| 2 | * This program is free software; you can redistribute it and/or modify |
---|
| 3 | * it under the terms of the GNU General Public License as published by |
---|
| 4 | * the Free Software Foundation; either version 2 of the License, or |
---|
| 5 | * (at your option) any later version. |
---|
| 6 | * |
---|
| 7 | * This program is distributed in the hope that it will be useful, |
---|
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 10 | * GNU General Public License for more details. |
---|
| 11 | * |
---|
| 12 | * You should have received a copy of the GNU General Public License |
---|
| 13 | * along with this program; if not, write to the Free Software |
---|
| 14 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
| 15 | */ |
---|
| 16 | |
---|
| 17 | /* |
---|
| 18 | * IteratedSingleClassifierEnhancer.java |
---|
| 19 | * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.classifiers; |
---|
| 24 | |
---|
| 25 | import weka.core.Instances; |
---|
| 26 | import weka.core.Option; |
---|
| 27 | import weka.core.Utils; |
---|
| 28 | |
---|
| 29 | import java.util.Enumeration; |
---|
| 30 | import java.util.Vector; |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * Abstract utility class for handling settings common to |
---|
| 34 | * meta classifiers that build an ensemble from a single base learner. |
---|
| 35 | * |
---|
| 36 | * @author Eibe Frank (eibe@cs.waikato.ac.nz) |
---|
| 37 | * @version $Revision: 6041 $ |
---|
| 38 | */ |
---|
| 39 | public abstract class IteratedSingleClassifierEnhancer |
---|
| 40 | extends SingleClassifierEnhancer { |
---|
| 41 | |
---|
| 42 | /** for serialization */ |
---|
| 43 | private static final long serialVersionUID = -6217979135443319724L; |
---|
| 44 | |
---|
| 45 | /** Array for storing the generated base classifiers. */ |
---|
| 46 | protected Classifier[] m_Classifiers; |
---|
| 47 | |
---|
| 48 | /** The number of iterations. */ |
---|
| 49 | protected int m_NumIterations = 10; |
---|
| 50 | |
---|
| 51 | /** |
---|
| 52 | * Stump method for building the classifiers. |
---|
| 53 | * |
---|
| 54 | * @param data the training data to be used for generating the |
---|
| 55 | * bagged classifier. |
---|
| 56 | * @exception Exception if the classifier could not be built successfully |
---|
| 57 | */ |
---|
| 58 | public void buildClassifier(Instances data) throws Exception { |
---|
| 59 | |
---|
| 60 | if (m_Classifier == null) { |
---|
| 61 | throw new Exception("A base classifier has not been specified!"); |
---|
| 62 | } |
---|
| 63 | m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, m_NumIterations); |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /** |
---|
| 67 | * Returns an enumeration describing the available options. |
---|
| 68 | * |
---|
| 69 | * @return an enumeration of all the available options. |
---|
| 70 | */ |
---|
| 71 | public Enumeration listOptions() { |
---|
| 72 | |
---|
| 73 | Vector newVector = new Vector(2); |
---|
| 74 | |
---|
| 75 | newVector.addElement(new Option( |
---|
| 76 | "\tNumber of iterations.\n" |
---|
| 77 | + "\t(default 10)", |
---|
| 78 | "I", 1, "-I <num>")); |
---|
| 79 | |
---|
| 80 | Enumeration enu = super.listOptions(); |
---|
| 81 | while (enu.hasMoreElements()) { |
---|
| 82 | newVector.addElement(enu.nextElement()); |
---|
| 83 | } |
---|
| 84 | return newVector.elements(); |
---|
| 85 | } |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Parses a given list of options. Valid options are:<p> |
---|
| 89 | * |
---|
| 90 | * -W classname <br> |
---|
| 91 | * Specify the full class name of the base learner.<p> |
---|
| 92 | * |
---|
| 93 | * -I num <br> |
---|
| 94 | * Set the number of iterations (default 10). <p> |
---|
| 95 | * |
---|
| 96 | * Options after -- are passed to the designated classifier.<p> |
---|
| 97 | * |
---|
| 98 | * @param options the list of options as an array of strings |
---|
| 99 | * @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 [] superOptions = super.getOptions(); |
---|
| 121 | String [] options = new String [superOptions.length + 2]; |
---|
| 122 | |
---|
| 123 | int current = 0; |
---|
| 124 | options[current++] = "-I"; |
---|
| 125 | options[current++] = "" + getNumIterations(); |
---|
| 126 | |
---|
| 127 | System.arraycopy(superOptions, 0, options, current, |
---|
| 128 | superOptions.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 | } |
---|