source: src/main/java/weka/classifiers/MultipleClassifiersCombiner.java @ 25

Last change on this file since 25 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 6.6 KB
Line 
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 *    MultipleClassifiersCombiner.java
19 *    Copyright (C) 2004 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.classifiers;
24
25import weka.core.Capabilities;
26import weka.core.Option;
27import weka.core.OptionHandler;
28import weka.core.Utils;
29import weka.core.Capabilities.Capability;
30
31import java.util.Enumeration;
32import java.util.Vector;
33
34/**
35 * Abstract utility class for handling settings common to
36 * meta classifiers that build an ensemble from multiple classifiers.
37 *
38 * @author Eibe Frank (eibe@cs.waikato.ac.nz)
39 * @version $Revision: 6041 $
40 */
41public abstract class MultipleClassifiersCombiner extends AbstractClassifier {
42
43  /** for serialization */
44  private static final long serialVersionUID = 2776436621129422119L;
45
46  /** Array for storing the generated base classifiers. */
47  protected Classifier[] m_Classifiers = {
48    new weka.classifiers.rules.ZeroR()
49  };
50
51  /**
52   * Returns an enumeration describing the available options
53   *
54   * @return an enumeration of all the available options
55   */
56  public Enumeration listOptions() {
57
58    Vector newVector = new Vector(1);
59
60    newVector.addElement(new Option(
61          "\tFull class name of classifier to include, followed\n"
62          + "\tby scheme options. May be specified multiple times.\n"
63          + "\t(default: \"weka.classifiers.rules.ZeroR\")",
64          "B", 1, "-B <classifier specification>"));
65
66    Enumeration enu = super.listOptions();
67    while (enu.hasMoreElements()) {
68      newVector.addElement(enu.nextElement());
69    }
70    return newVector.elements();
71  }
72
73  /**
74   * Parses a given list of options. Valid options are:<p>
75   *
76   * -B classifierstring <br>
77   * Classifierstring should contain the full class name of a scheme
78   * included for selection followed by options to the classifier
79   * (required, option should be used once for each classifier).<p>
80   *
81   * @param options the list of options as an array of strings
82   * @exception Exception if an option is not supported
83   */
84  public void setOptions(String[] options) throws Exception {
85
86    // Iterate through the schemes
87    Vector classifiers = new Vector();
88    while (true) {
89      String classifierString = Utils.getOption('B', options);
90      if (classifierString.length() == 0) {
91        break;
92      }
93      String [] classifierSpec = Utils.splitOptions(classifierString);
94      if (classifierSpec.length == 0) {
95        throw new IllegalArgumentException("Invalid classifier specification string");
96      }
97      String classifierName = classifierSpec[0];
98      classifierSpec[0] = "";
99      classifiers.addElement(AbstractClassifier.forName(classifierName,
100            classifierSpec));
101    }
102    if (classifiers.size() == 0) {
103      classifiers.addElement(new weka.classifiers.rules.ZeroR());
104    }
105    Classifier [] classifiersArray = new Classifier [classifiers.size()];
106    for (int i = 0; i < classifiersArray.length; i++) {
107      classifiersArray[i] = (Classifier) classifiers.elementAt(i);
108    }
109    setClassifiers(classifiersArray);
110
111    super.setOptions(options);
112  }
113
114  /**
115   * Gets the current settings of the Classifier.
116   *
117   * @return an array of strings suitable for passing to setOptions
118   */
119  public String [] getOptions() {
120
121    String [] superOptions = super.getOptions();
122    int current = 0;
123    String[] options = new String [superOptions.length + m_Classifiers.length * 2];
124    for (int i = 0; i < m_Classifiers.length; i++) {
125      options[current++] = "-B";
126      options[current++] = "" + getClassifierSpec(i);
127    }
128    System.arraycopy(superOptions, 0, options, current,
129        superOptions.length);
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 classifiersTipText() {
139    return "The base classifiers to be used.";
140  }
141
142  /**
143   * Sets the list of possible classifers to choose from.
144   *
145   * @param classifiers an array of classifiers with all options set.
146   */
147  public void setClassifiers(Classifier [] classifiers) {
148
149    m_Classifiers = classifiers;
150  }
151
152  /**
153   * Gets the list of possible classifers to choose from.
154   *
155   * @return the array of Classifiers
156   */
157  public Classifier [] getClassifiers() {
158
159    return m_Classifiers;
160  }
161
162  /**
163   * Gets a single classifier from the set of available classifiers.
164   *
165   * @param index the index of the classifier wanted
166   * @return the Classifier
167   */
168  public Classifier getClassifier(int index) {
169
170    return m_Classifiers[index];
171  }
172
173  /**
174   * Gets the classifier specification string, which contains the class name of
175   * the classifier and any options to the classifier
176   *
177   * @param index the index of the classifier string to retrieve, starting from
178   * 0.
179   * @return the classifier string, or the empty string if no classifier
180   * has been assigned (or the index given is out of range).
181   */
182  protected String getClassifierSpec(int index) {
183
184    if (m_Classifiers.length < index) {
185      return "";
186    }
187    Classifier c = getClassifier(index);
188    return c.getClass().getName() + " "
189      + Utils.joinOptions(((OptionHandler)c).getOptions());
190  }
191
192  /**
193   * Returns combined capabilities of the base classifiers, i.e., the
194   * capabilities all of them have in common.
195   *
196   * @return      the capabilities of the base classifiers
197   */
198  public Capabilities getCapabilities() {
199    Capabilities      result;
200    int               i;
201
202    if (getClassifiers().length == 0) {
203      result = new Capabilities(this);
204      result.disableAll();
205    }
206    else {
207      result = (Capabilities) getClassifier(0).getCapabilities().clone();
208      for (i = 1; i < getClassifiers().length; i++)
209        result.and(getClassifier(i).getCapabilities());
210    }
211
212    // set dependencies
213    for (Capability cap: Capability.values())
214      result.enableDependency(cap);
215
216    result.setOwner(this);
217
218    return result;
219  }
220}
Note: See TracBrowser for help on using the repository browser.