source: src/main/java/weka/classifiers/trees/M5P.java @ 4

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

Import di weka.

File size: 6.8 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 *    M5P.java
19 *    Copyright (C) 2001 University of Waikato, Hamilton, New Zealand
20 *
21 */
22
23package weka.classifiers.trees;
24
25import weka.classifiers.trees.m5.M5Base;
26import weka.classifiers.trees.m5.Rule;
27import weka.core.Drawable;
28import weka.core.Option;
29import weka.core.RevisionUtils;
30import weka.core.Utils;
31
32import java.util.Enumeration;
33import java.util.Vector;
34
35/**
36 <!-- globalinfo-start -->
37 * M5Base. Implements base routines for generating M5 Model trees and rules<br/>
38 * The original algorithm M5 was invented by R. Quinlan and Yong Wang made improvements.<br/>
39 * <br/>
40 * For more information see:<br/>
41 * <br/>
42 * Ross J. Quinlan: Learning with Continuous Classes. In: 5th Australian Joint Conference on Artificial Intelligence, Singapore, 343-348, 1992.<br/>
43 * <br/>
44 * Y. Wang, I. H. Witten: Induction of model trees for predicting continuous classes. In: Poster papers of the 9th European Conference on Machine Learning, 1997.
45 * <p/>
46 <!-- globalinfo-end -->
47 *
48 <!-- technical-bibtex-start -->
49 * BibTeX:
50 * <pre>
51 * &#64;inproceedings{Quinlan1992,
52 *    address = {Singapore},
53 *    author = {Ross J. Quinlan},
54 *    booktitle = {5th Australian Joint Conference on Artificial Intelligence},
55 *    pages = {343-348},
56 *    publisher = {World Scientific},
57 *    title = {Learning with Continuous Classes},
58 *    year = {1992}
59 * }
60 *
61 * &#64;inproceedings{Wang1997,
62 *    author = {Y. Wang and I. H. Witten},
63 *    booktitle = {Poster papers of the 9th European Conference on Machine Learning},
64 *    publisher = {Springer},
65 *    title = {Induction of model trees for predicting continuous classes},
66 *    year = {1997}
67 * }
68 * </pre>
69 * <p/>
70 <!-- technical-bibtex-end -->
71 *
72 <!-- options-start -->
73 * Valid options are: <p/>
74 *
75 * <pre> -N
76 *  Use unpruned tree/rules</pre>
77 *
78 * <pre> -U
79 *  Use unsmoothed predictions</pre>
80 *
81 * <pre> -R
82 *  Build regression tree/rule rather than a model tree/rule</pre>
83 *
84 * <pre> -M &lt;minimum number of instances&gt;
85 *  Set minimum number of instances per leaf
86 *  (default 4)</pre>
87 *
88 * <pre> -L
89 *  Save instances at the nodes in
90 *  the tree (for visualization purposes)</pre>
91 *
92 <!-- options-end -->
93 *
94 * @author <a href="mailto:mhall@cs.waikato.ac.nz">Mark Hall</a>
95 * @version $Revision: 1.10 $
96 */
97public class M5P 
98  extends M5Base
99  implements Drawable {
100
101  /** for serialization */
102  static final long serialVersionUID = -6118439039768244417L;
103 
104  /**
105   * Creates a new <code>M5P</code> instance.
106   */
107  public M5P() {
108    super();
109    setGenerateRules(false);
110  }
111
112  /**
113   *  Returns the type of graph this classifier
114   *  represents.
115   *  @return Drawable.TREE
116   */   
117  public int graphType() {
118      return Drawable.TREE;
119  }
120
121  /**
122   * Return a dot style String describing the tree.
123   *
124   * @return a <code>String</code> value
125   * @throws Exception if an error occurs
126   */
127  public String graph() throws Exception {
128    StringBuffer text = new StringBuffer();
129   
130    text.append("digraph M5Tree {\n");
131    Rule temp = (Rule)m_ruleSet.elementAt(0);
132    temp.topOfTree().graph(text);
133    text.append("}\n");
134    return text.toString();
135  }
136
137  /**
138   * Returns the tip text for this property
139   *
140   * @return            tip text for this property suitable for
141   *                    displaying in the explorer/experimenter gui
142   */
143  public String saveInstancesTipText() {
144    return 
145        "Whether to save instance data at each node in the tree for "
146      + "visualization purposes.";
147  }
148
149  /**
150   * Set whether to save instance data at each node in the
151   * tree for visualization purposes
152   *
153   * @param save a <code>boolean</code> value
154   */
155  public void setSaveInstances(boolean save) {
156    m_saveInstances = save;
157  }
158
159  /**
160   * Get whether instance data is being save.
161   *
162   * @return a <code>boolean</code> value
163   */
164  public boolean getSaveInstances() {
165    return m_saveInstances;
166  }
167
168  /**
169   * Returns an enumeration describing the available options
170   *
171   * @return an enumeration of all the available options
172   */
173  public Enumeration listOptions() {
174    Enumeration superOpts = super.listOptions();
175   
176    Vector newVector = new Vector();
177    while (superOpts.hasMoreElements()) {
178      newVector.addElement((Option)superOpts.nextElement());
179    }
180
181    newVector.addElement(new Option("\tSave instances at the nodes in\n"
182                                    +"\tthe tree (for visualization purposes)",
183                                    "L", 0, "-L"));
184    return newVector.elements();
185  }
186
187  /**
188   * Parses a given list of options. <p/>
189   *
190   <!-- options-start -->
191   * Valid options are: <p/>
192   *
193   * <pre> -N
194   *  Use unpruned tree/rules</pre>
195   *
196   * <pre> -U
197   *  Use unsmoothed predictions</pre>
198   *
199   * <pre> -R
200   *  Build regression tree/rule rather than a model tree/rule</pre>
201   *
202   * <pre> -M &lt;minimum number of instances&gt;
203   *  Set minimum number of instances per leaf
204   *  (default 4)</pre>
205   *
206   * <pre> -L
207   *  Save instances at the nodes in
208   *  the tree (for visualization purposes)</pre>
209   *
210   <!-- options-end -->
211   *
212   * @param options the list of options as an array of strings
213   * @throws Exception if an option is not supported
214   */
215  public void setOptions(String[] options) throws Exception {
216    setSaveInstances(Utils.getFlag('L', options));
217    super.setOptions(options);
218  }
219
220  /**
221   * Gets the current settings of the classifier.
222   *
223   * @return an array of strings suitable for passing to setOptions
224   */
225  public String [] getOptions() {
226    String[] superOpts = super.getOptions();
227    String [] options = new String [superOpts.length+1];
228    int current = superOpts.length;
229    for (int i = 0; i < current; i++) {
230      options[i] = superOpts[i];
231    }
232   
233    if (getSaveInstances()) {
234      options[current++] = "-L";
235    }
236
237    while (current < options.length) {
238      options[current++] = "";
239    }
240
241    return options;
242  }
243 
244  /**
245   * Returns the revision string.
246   *
247   * @return            the revision
248   */
249  public String getRevision() {
250    return RevisionUtils.extract("$Revision: 1.10 $");
251  }
252
253  /**
254   * Main method by which this class can be tested
255   *
256   * @param args an array of options
257   */
258  public static void main(String[] args) {
259    runClassifier(new M5P(), args);
260  } 
261}
Note: See TracBrowser for help on using the repository browser.