source: src/main/java/weka/classifiers/meta/OrdinalClassClassifier.java @ 8

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

Import di weka.

File size: 16.9 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 *    OrdinalClassClassifier.java
19 *    Copyright (C) 2001 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.classifiers.meta;
24
25import weka.classifiers.Classifier;
26import weka.classifiers.AbstractClassifier;
27import weka.classifiers.SingleClassifierEnhancer;
28import weka.classifiers.rules.ZeroR;
29import weka.core.Capabilities;
30import weka.core.Instance;
31import weka.core.Instances;
32import weka.core.Option;
33import weka.core.OptionHandler;
34import weka.core.RevisionUtils;
35import weka.core.TechnicalInformation;
36import weka.core.TechnicalInformationHandler;
37import weka.core.Utils;
38import weka.core.Capabilities.Capability;
39import weka.core.TechnicalInformation.Field;
40import weka.core.TechnicalInformation.Type;
41import weka.filters.Filter;
42import weka.filters.unsupervised.attribute.MakeIndicator;
43
44import java.util.Enumeration;
45import java.util.Vector;
46
47/**
48 <!-- globalinfo-start -->
49 * Meta classifier that allows standard classification algorithms to be applied to ordinal class problems.<br/>
50 * <br/>
51 * For more information see: <br/>
52 * <br/>
53 * Eibe Frank, Mark Hall: A Simple Approach to Ordinal Classification. In: 12th European Conference on Machine Learning, 145-156, 2001.<br/>
54 * <br/>
55 * Robert E. Schapire, Peter Stone, David A. McAllester, Michael L. Littman, Janos A. Csirik: Modeling Auction Price Uncertainty Using Boosting-based Conditional Density Estimation. In: Machine Learning, Proceedings of the Nineteenth International Conference (ICML 2002), 546-553, 2002.
56 * <p/>
57 <!-- globalinfo-end -->
58 *
59 <!-- technical-bibtex-start -->
60 * BibTeX:
61 * <pre>
62 * &#64;inproceedings{Frank2001,
63 *    author = {Eibe Frank and Mark Hall},
64 *    booktitle = {12th European Conference on Machine Learning},
65 *    pages = {145-156},
66 *    publisher = {Springer},
67 *    title = {A Simple Approach to Ordinal Classification},
68 *    year = {2001}
69 * }
70 *
71 * &#64;inproceedings{Schapire2002,
72 *    author = {Robert E. Schapire and Peter Stone and David A. McAllester and Michael L. Littman and Janos A. Csirik},
73 *    booktitle = {Machine Learning, Proceedings of the Nineteenth International Conference (ICML 2002)},
74 *    pages = {546-553},
75 *    publisher = {Morgan Kaufmann},
76 *    title = {Modeling Auction Price Uncertainty Using Boosting-based Conditional Density Estimation},
77 *    year = {2002}
78 * }
79 * </pre>
80 * <p/>
81 <!-- technical-bibtex-end -->
82 *
83 <!-- options-start -->
84 * Valid options are: <p/>
85 *
86 * <pre> -S
87 *  Turn off Schapire et al.'s smoothing heuristic (ICML02, pp. 550).</pre>
88 *
89 * <pre> -D
90 *  If set, classifier is run in debug mode and
91 *  may output additional info to the console</pre>
92 *
93 * <pre> -W
94 *  Full name of base classifier.
95 *  (default: weka.classifiers.trees.J48)</pre>
96 *
97 * <pre>
98 * Options specific to classifier weka.classifiers.trees.J48:
99 * </pre>
100 *
101 * <pre> -U
102 *  Use unpruned tree.</pre>
103 *
104 * <pre> -C &lt;pruning confidence&gt;
105 *  Set confidence threshold for pruning.
106 *  (default 0.25)</pre>
107 *
108 * <pre> -M &lt;minimum number of instances&gt;
109 *  Set minimum number of instances per leaf.
110 *  (default 2)</pre>
111 *
112 * <pre> -R
113 *  Use reduced error pruning.</pre>
114 *
115 * <pre> -N &lt;number of folds&gt;
116 *  Set number of folds for reduced error
117 *  pruning. One fold is used as pruning set.
118 *  (default 3)</pre>
119 *
120 * <pre> -B
121 *  Use binary splits only.</pre>
122 *
123 * <pre> -S
124 *  Don't perform subtree raising.</pre>
125 *
126 * <pre> -L
127 *  Do not clean up after the tree has been built.</pre>
128 *
129 * <pre> -A
130 *  Laplace smoothing for predicted probabilities.</pre>
131 *
132 * <pre> -Q &lt;seed&gt;
133 *  Seed for random data shuffling (default 1).</pre>
134 *
135 <!-- options-end -->
136 *
137 * @author Mark Hall
138 * @author Eibe Frank
139 * @version $Revision: 5928 $
140 * @see OptionHandler
141 */
142public class OrdinalClassClassifier 
143  extends SingleClassifierEnhancer
144  implements OptionHandler, TechnicalInformationHandler {
145 
146  /** for serialization */
147  static final long serialVersionUID = -3461971774059603636L;
148
149  /** The classifiers. (One for each class.) */
150  private Classifier [] m_Classifiers;
151
152  /** The filters used to transform the class. */
153  private MakeIndicator[] m_ClassFilters;
154
155  /** ZeroR classifier for when all base classifier return zero probability. */
156  private ZeroR m_ZeroR;
157
158  /** Whether to use smoothing to prevent negative "probabilities". */
159  private boolean m_UseSmoothing = true;
160
161  /**
162   * String describing default classifier.
163   *
164   * @return the default classifier classname
165   */
166  protected String defaultClassifierString() {
167   
168    return "weka.classifiers.trees.J48";
169  }
170
171  /**
172   * Default constructor.
173   */
174  public OrdinalClassClassifier() {
175    m_Classifier = new weka.classifiers.trees.J48();
176  }
177
178  /**
179   * Returns a string describing this attribute evaluator
180   * @return a description of the evaluator suitable for
181   * displaying in the explorer/experimenter gui
182   */
183  public String globalInfo() {
184    return "Meta classifier that allows standard classification algorithms "
185      +"to be applied to ordinal class problems.\n\n"
186      + "For more information see: \n\n"
187      + getTechnicalInformation().toString();
188  }
189
190  /**
191   * Returns an instance of a TechnicalInformation object, containing
192   * detailed information about the technical background of this class,
193   * e.g., paper reference or book this class is based on.
194   *
195   * @return the technical information about this class
196   */
197  public TechnicalInformation getTechnicalInformation() {
198    TechnicalInformation        result;
199    TechnicalInformation        additional;
200   
201    result = new TechnicalInformation(Type.INPROCEEDINGS);
202    result.setValue(Field.AUTHOR, "Eibe Frank and Mark Hall");
203    result.setValue(Field.TITLE, "A Simple Approach to Ordinal Classification");
204    result.setValue(Field.BOOKTITLE, "12th European Conference on Machine Learning");
205    result.setValue(Field.YEAR, "2001");
206    result.setValue(Field.PAGES, "145-156");
207    result.setValue(Field.PUBLISHER, "Springer");
208   
209    additional = result.add(Type.INPROCEEDINGS);
210    additional.setValue(Field.AUTHOR, "Robert E. Schapire and Peter Stone and David A. McAllester " +
211                "and Michael L. Littman and Janos A. Csirik");
212    additional.setValue(Field.TITLE, "Modeling Auction Price Uncertainty Using Boosting-based " +
213                "Conditional Density Estimation");
214    additional.setValue(Field.BOOKTITLE, "Machine Learning, Proceedings of the Nineteenth " +
215                "International Conference (ICML 2002)");
216    additional.setValue(Field.YEAR, "2002");
217    additional.setValue(Field.PAGES, "546-553");
218    additional.setValue(Field.PUBLISHER, "Morgan Kaufmann");
219   
220    return result;
221  }
222
223  /**
224   * Returns default capabilities of the classifier.
225   *
226   * @return      the capabilities of this classifier
227   */
228  public Capabilities getCapabilities() {
229    Capabilities result = super.getCapabilities();
230
231    // class
232    result.disableAllClasses();
233    result.disableAllClassDependencies();
234    result.enable(Capability.NOMINAL_CLASS);
235   
236    return result;
237  }
238
239  /**
240   * Builds the classifiers.
241   *
242   * @param insts the training data.
243   * @throws Exception if a classifier can't be built
244   */
245  public void buildClassifier(Instances insts) throws Exception {
246
247    Instances newInsts;
248
249    // can classifier handle the data?
250    getCapabilities().testWithFail(insts);
251
252    // remove instances with missing class
253    insts = new Instances(insts);
254    insts.deleteWithMissingClass();
255   
256    if (m_Classifier == null) {
257      throw new Exception("No base classifier has been set!");
258    }
259    m_ZeroR = new ZeroR();
260    m_ZeroR.buildClassifier(insts);
261
262    int numClassifiers = insts.numClasses() - 1;
263
264    numClassifiers = (numClassifiers == 0) ? 1 : numClassifiers;
265
266    if (numClassifiers == 1) {
267      m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, 1);
268      m_Classifiers[0].buildClassifier(insts);
269    } else {
270      m_Classifiers = AbstractClassifier.makeCopies(m_Classifier, numClassifiers);
271      m_ClassFilters = new MakeIndicator[numClassifiers];
272
273      for (int i = 0; i < m_Classifiers.length; i++) {
274        m_ClassFilters[i] = new MakeIndicator();
275        m_ClassFilters[i].setAttributeIndex("" + (insts.classIndex() + 1));
276        m_ClassFilters[i].setValueIndices(""+(i+2)+"-last");
277        m_ClassFilters[i].setNumeric(false);
278        m_ClassFilters[i].setInputFormat(insts);
279        newInsts = Filter.useFilter(insts, m_ClassFilters[i]);
280        m_Classifiers[i].buildClassifier(newInsts);
281      }
282    }
283  }
284 
285  /**
286   * Returns the distribution for an instance.
287   *
288   * @param inst the instance to compute the distribution for
289   * @return the class distribution for the given instance
290   * @throws Exception if the distribution can't be computed successfully
291   */
292  public double [] distributionForInstance(Instance inst) throws Exception {
293   
294    if (m_Classifiers.length == 1) {
295      return m_Classifiers[0].distributionForInstance(inst);
296    }
297
298    double [] probs = new double[inst.numClasses()];
299   
300    double [][] distributions = new double[m_ClassFilters.length][0];
301    for(int i = 0; i < m_ClassFilters.length; i++) {
302      m_ClassFilters[i].input(inst);
303      m_ClassFilters[i].batchFinished();
304     
305      distributions[i] = m_Classifiers[i].
306        distributionForInstance(m_ClassFilters[i].output());
307     
308    }
309
310    // Use Schapire et al.'s smoothing heuristic?
311    if (getUseSmoothing()) {
312     
313      double[] fScores = new double[distributions.length + 2];
314      fScores[0] = 1;
315      fScores[distributions.length + 1] = 0;
316      for (int i = 0; i < distributions.length; i++) {
317        fScores[i + 1] = distributions[i][1];
318      }
319     
320      // Sort scores in ascending order
321      int[] sortOrder = Utils.sort(fScores);
322     
323      // Compute pointwise maximum of lower bound
324      int minSoFar = sortOrder[0];
325      int index = 0;
326      double[] pointwiseMaxLowerBound = new double[fScores.length];
327      for (int i = 0; i < sortOrder.length; i++) {
328
329        // Progress to next higher value if possible
330        while (minSoFar > sortOrder.length - i - 1) {
331          minSoFar = sortOrder[++index];
332        }
333        pointwiseMaxLowerBound[sortOrder.length - i - 1] = fScores[minSoFar];
334      }
335     
336      // Get scores in descending order
337      int[] newSortOrder = new int[sortOrder.length];
338      for (int i = sortOrder.length - 1; i >= 0; i--) {
339        newSortOrder[sortOrder.length - i - 1] = sortOrder[i];
340      }
341      sortOrder = newSortOrder;
342     
343      // Compute pointwise minimum of upper bound
344      int maxSoFar = sortOrder[0];
345      index = 0;
346      double[] pointwiseMinUpperBound = new double[fScores.length];
347      for (int i = 0; i < sortOrder.length; i++) {
348       
349        // Progress to next lower value if possible
350        while (maxSoFar < i) {
351          maxSoFar = sortOrder[++index];
352        }
353        pointwiseMinUpperBound[i] = fScores[maxSoFar];
354      }
355
356      // Compute average
357      for (int i = 0; i < distributions.length; i++) {
358        distributions[i][1] = (pointwiseMinUpperBound[i + 1] +
359                               pointwiseMaxLowerBound[i + 1]) / 2.0;
360      }
361    }
362
363    for (int i = 0; i < inst.numClasses(); i++) {
364      if (i == 0) {
365        probs[i] = 1.0 - distributions[0][1];
366      } else if (i == inst.numClasses() - 1) {
367        probs[i] = distributions[i - 1][1];
368      } else {
369        probs[i] = distributions[i - 1][1] - distributions[i][1];
370        if (!(probs[i] >= 0)) {
371          System.err.println("Warning: estimated probability " + probs[i] +
372                             ". Rounding to 0.");
373          probs[i] = 0;
374        }
375      }
376    }
377   
378    if (Utils.gr(Utils.sum(probs), 0)) {
379      Utils.normalize(probs);
380      return probs;
381    } else {
382      return m_ZeroR.distributionForInstance(inst);
383    }
384  }
385
386  /**
387   * Returns an enumeration describing the available options.
388   *
389   * @return an enumeration of all the available options.
390   */
391  public Enumeration listOptions()  {
392
393    Vector vec = new Vector();
394    vec.addElement(new Option(
395              "\tTurn off Schapire et al.'s smoothing " + 
396              "heuristic (ICML02, pp. 550).",
397              "S", 0, "-S"));
398
399    Enumeration enu = super.listOptions();
400    while (enu.hasMoreElements()) {
401      vec.addElement(enu.nextElement());
402    }
403    return vec.elements();
404  }
405  /**
406   * Parses a given list of options. <p/>
407   *
408   <!-- options-start -->
409   * Valid options are: <p/>
410   *
411   * <pre> -S
412   *  Turn off Schapire et al.'s smoothing heuristic (ICML02, pp. 550).</pre>
413   *
414   * <pre> -D
415   *  If set, classifier is run in debug mode and
416   *  may output additional info to the console</pre>
417   *
418   * <pre> -W
419   *  Full name of base classifier.
420   *  (default: weka.classifiers.trees.J48)</pre>
421   *
422   * <pre>
423   * Options specific to classifier weka.classifiers.trees.J48:
424   * </pre>
425   *
426   * <pre> -U
427   *  Use unpruned tree.</pre>
428   *
429   * <pre> -C &lt;pruning confidence&gt;
430   *  Set confidence threshold for pruning.
431   *  (default 0.25)</pre>
432   *
433   * <pre> -M &lt;minimum number of instances&gt;
434   *  Set minimum number of instances per leaf.
435   *  (default 2)</pre>
436   *
437   * <pre> -R
438   *  Use reduced error pruning.</pre>
439   *
440   * <pre> -N &lt;number of folds&gt;
441   *  Set number of folds for reduced error
442   *  pruning. One fold is used as pruning set.
443   *  (default 3)</pre>
444   *
445   * <pre> -B
446   *  Use binary splits only.</pre>
447   *
448   * <pre> -S
449   *  Don't perform subtree raising.</pre>
450   *
451   * <pre> -L
452   *  Do not clean up after the tree has been built.</pre>
453   *
454   * <pre> -A
455   *  Laplace smoothing for predicted probabilities.</pre>
456   *
457   * <pre> -Q &lt;seed&gt;
458   *  Seed for random data shuffling (default 1).</pre>
459   *
460   <!-- options-end -->
461   *
462   * @param options the list of options as an array of strings
463   * @throws Exception if an option is not supported
464   */
465  public void setOptions(String[] options) throws Exception {
466
467    setUseSmoothing(!Utils.getFlag('S', options));
468    super.setOptions(options);
469  }
470
471  /**
472   * Gets the current settings of the Classifier.
473   *
474   * @return an array of strings suitable for passing to setOptions
475   */
476  public String [] getOptions() {
477
478    String [] superOptions = super.getOptions();
479    String [] options = new String [superOptions.length + 1];
480
481    int current = 0;
482    if (!getUseSmoothing()) {
483      options[current++] = "-S";
484    }
485    System.arraycopy(superOptions, 0, options, current, 
486                     superOptions.length);
487
488    current += superOptions.length;
489    while (current < options.length) {
490      options[current++] = "";
491    }
492
493    return options;
494  }
495
496  /**
497   * Tip text method.
498   *
499   * @return a tip text string suitable for displaying as a popup in the GUI.
500   */
501  public String useSmoothingTipText() {
502    return "If true, use Schapire et al.'s heuristic (ICML02, pp. 550).";
503  }
504
505  /**
506   * Determines whether Schapire et al.'s smoothing method is used.
507   *
508   * @param b true if the smoothing heuristic is to be used.
509   */
510  public void setUseSmoothing(boolean b) {
511   
512    m_UseSmoothing = b;
513  }
514
515  /**
516   * Checks whether Schapire et al.'s smoothing method is used.
517   *
518   * @return true if the smoothing heuristic is to be used.
519   */
520  public boolean getUseSmoothing() {
521   
522    return m_UseSmoothing;
523  }
524
525 
526  /**
527   * Prints the classifiers.
528   *
529   * @return a string representation of this classifier
530   */
531  public String toString() {
532   
533    if (m_Classifiers == null) {
534      return "OrdinalClassClassifier: No model built yet.";
535    }
536    StringBuffer text = new StringBuffer();
537    text.append("OrdinalClassClassifier\n\n");
538    for (int i = 0; i < m_Classifiers.length; i++) {
539      text.append("Classifier ").append(i + 1);
540      if (m_Classifiers[i] != null) {
541         if ((m_ClassFilters != null) && (m_ClassFilters[i] != null)) {
542          text.append(", using indicator values: ");
543          text.append(m_ClassFilters[i].getValueRange());
544        }
545        text.append('\n');
546        text.append(m_Classifiers[i].toString() + "\n");
547      } else {
548        text.append(" Skipped (no training examples)\n");
549      }
550    }
551
552    return text.toString();
553  }
554 
555  /**
556   * Returns the revision string.
557   *
558   * @return            the revision
559   */
560  public String getRevision() {
561    return RevisionUtils.extract("$Revision: 5928 $");
562  }
563
564  /**
565   * Main method for testing this class.
566   *
567   * @param argv the options
568   */
569  public static void main(String [] argv) {
570    runClassifier(new OrdinalClassClassifier(), argv);
571  }
572}
573
Note: See TracBrowser for help on using the repository browser.