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 | * J48.java |
---|
19 | * Copyright (C) 1999 University of Waikato, Hamilton, New Zealand |
---|
20 | * |
---|
21 | */ |
---|
22 | |
---|
23 | package weka.classifiers.trees; |
---|
24 | |
---|
25 | import weka.classifiers.Classifier; |
---|
26 | import weka.classifiers.AbstractClassifier; |
---|
27 | import weka.classifiers.Sourcable; |
---|
28 | import weka.classifiers.trees.j48.BinC45ModelSelection; |
---|
29 | import weka.classifiers.trees.j48.C45ModelSelection; |
---|
30 | import weka.classifiers.trees.j48.C45PruneableClassifierTree; |
---|
31 | import weka.classifiers.trees.j48.ClassifierTree; |
---|
32 | import weka.classifiers.trees.j48.ModelSelection; |
---|
33 | import weka.classifiers.trees.j48.PruneableClassifierTree; |
---|
34 | import weka.core.AdditionalMeasureProducer; |
---|
35 | import weka.core.Capabilities; |
---|
36 | import weka.core.Drawable; |
---|
37 | import weka.core.Instance; |
---|
38 | import weka.core.Instances; |
---|
39 | import weka.core.Matchable; |
---|
40 | import weka.core.Option; |
---|
41 | import weka.core.OptionHandler; |
---|
42 | import weka.core.RevisionUtils; |
---|
43 | import weka.core.Summarizable; |
---|
44 | import weka.core.TechnicalInformation; |
---|
45 | import weka.core.TechnicalInformationHandler; |
---|
46 | import weka.core.Utils; |
---|
47 | import weka.core.WeightedInstancesHandler; |
---|
48 | import weka.core.TechnicalInformation.Field; |
---|
49 | import weka.core.TechnicalInformation.Type; |
---|
50 | |
---|
51 | import java.util.Enumeration; |
---|
52 | import java.util.Vector; |
---|
53 | |
---|
54 | /** |
---|
55 | <!-- globalinfo-start --> |
---|
56 | * Class for generating a pruned or unpruned C4.5 decision tree. For more information, see<br/> |
---|
57 | * <br/> |
---|
58 | * Ross Quinlan (1993). C4.5: Programs for Machine Learning. Morgan Kaufmann Publishers, San Mateo, CA. |
---|
59 | * <p/> |
---|
60 | <!-- globalinfo-end --> |
---|
61 | * |
---|
62 | <!-- technical-bibtex-start --> |
---|
63 | * BibTeX: |
---|
64 | * <pre> |
---|
65 | * @book{Quinlan1993, |
---|
66 | * address = {San Mateo, CA}, |
---|
67 | * author = {Ross Quinlan}, |
---|
68 | * publisher = {Morgan Kaufmann Publishers}, |
---|
69 | * title = {C4.5: Programs for Machine Learning}, |
---|
70 | * year = {1993} |
---|
71 | * } |
---|
72 | * </pre> |
---|
73 | * <p/> |
---|
74 | <!-- technical-bibtex-end --> |
---|
75 | * |
---|
76 | <!-- options-start --> |
---|
77 | * Valid options are: <p/> |
---|
78 | * |
---|
79 | * <pre> -U |
---|
80 | * Use unpruned tree.</pre> |
---|
81 | * |
---|
82 | * <pre> -O |
---|
83 | * Do not collapse tree.</pre> |
---|
84 | * |
---|
85 | * <pre> -C <pruning confidence> |
---|
86 | * Set confidence threshold for pruning. |
---|
87 | * (default 0.25)</pre> |
---|
88 | * |
---|
89 | * <pre> -M <minimum number of instances> |
---|
90 | * Set minimum number of instances per leaf. |
---|
91 | * (default 2)</pre> |
---|
92 | * |
---|
93 | * <pre> -R |
---|
94 | * Use reduced error pruning.</pre> |
---|
95 | * |
---|
96 | * <pre> -N <number of folds> |
---|
97 | * Set number of folds for reduced error |
---|
98 | * pruning. One fold is used as pruning set. |
---|
99 | * (default 3)</pre> |
---|
100 | * |
---|
101 | * <pre> -B |
---|
102 | * Use binary splits only.</pre> |
---|
103 | * |
---|
104 | * <pre> -S |
---|
105 | * Don't perform subtree raising.</pre> |
---|
106 | * |
---|
107 | * <pre> -L |
---|
108 | * Do not clean up after the tree has been built.</pre> |
---|
109 | * |
---|
110 | * <pre> -A |
---|
111 | * Laplace smoothing for predicted probabilities.</pre> |
---|
112 | * |
---|
113 | * <pre> -J |
---|
114 | * Do not use MDL correction for info gain on numeric attributes.</pre> |
---|
115 | * |
---|
116 | * <pre> -Q <seed> |
---|
117 | * Seed for random data shuffling (default 1).</pre> |
---|
118 | * |
---|
119 | <!-- options-end --> |
---|
120 | * |
---|
121 | * @author Eibe Frank (eibe@cs.waikato.ac.nz) |
---|
122 | * @version $Revision: 6088 $ |
---|
123 | */ |
---|
124 | public class J48 |
---|
125 | extends AbstractClassifier |
---|
126 | implements OptionHandler, Drawable, Matchable, Sourcable, |
---|
127 | WeightedInstancesHandler, Summarizable, AdditionalMeasureProducer, |
---|
128 | TechnicalInformationHandler { |
---|
129 | |
---|
130 | /** for serialization */ |
---|
131 | static final long serialVersionUID = -217733168393644444L; |
---|
132 | |
---|
133 | /** The decision tree */ |
---|
134 | protected ClassifierTree m_root; |
---|
135 | |
---|
136 | /** Unpruned tree? */ |
---|
137 | private boolean m_unpruned = false; |
---|
138 | |
---|
139 | /** Collapse tree? */ |
---|
140 | private boolean m_collapseTree = true; |
---|
141 | |
---|
142 | /** Confidence level */ |
---|
143 | private float m_CF = 0.25f; |
---|
144 | |
---|
145 | /** Minimum number of instances */ |
---|
146 | private int m_minNumObj = 2; |
---|
147 | |
---|
148 | /** Use MDL correction? */ |
---|
149 | private boolean m_useMDLcorrection = true; |
---|
150 | |
---|
151 | /** Determines whether probabilities are smoothed using |
---|
152 | Laplace correction when predictions are generated */ |
---|
153 | private boolean m_useLaplace = false; |
---|
154 | |
---|
155 | /** Use reduced error pruning? */ |
---|
156 | private boolean m_reducedErrorPruning = false; |
---|
157 | |
---|
158 | /** Number of folds for reduced error pruning. */ |
---|
159 | private int m_numFolds = 3; |
---|
160 | |
---|
161 | /** Binary splits on nominal attributes? */ |
---|
162 | private boolean m_binarySplits = false; |
---|
163 | |
---|
164 | /** Subtree raising to be performed? */ |
---|
165 | private boolean m_subtreeRaising = true; |
---|
166 | |
---|
167 | /** Cleanup after the tree has been built. */ |
---|
168 | private boolean m_noCleanup = false; |
---|
169 | |
---|
170 | /** Random number seed for reduced-error pruning. */ |
---|
171 | private int m_Seed = 1; |
---|
172 | |
---|
173 | /** |
---|
174 | * Returns a string describing classifier |
---|
175 | * @return a description suitable for |
---|
176 | * displaying in the explorer/experimenter gui |
---|
177 | */ |
---|
178 | public String globalInfo() { |
---|
179 | |
---|
180 | return "Class for generating a pruned or unpruned C4.5 decision tree. For more " |
---|
181 | + "information, see\n\n" |
---|
182 | + getTechnicalInformation().toString(); |
---|
183 | } |
---|
184 | |
---|
185 | /** |
---|
186 | * Returns an instance of a TechnicalInformation object, containing |
---|
187 | * detailed information about the technical background of this class, |
---|
188 | * e.g., paper reference or book this class is based on. |
---|
189 | * |
---|
190 | * @return the technical information about this class |
---|
191 | */ |
---|
192 | public TechnicalInformation getTechnicalInformation() { |
---|
193 | TechnicalInformation result; |
---|
194 | |
---|
195 | result = new TechnicalInformation(Type.BOOK); |
---|
196 | result.setValue(Field.AUTHOR, "Ross Quinlan"); |
---|
197 | result.setValue(Field.YEAR, "1993"); |
---|
198 | result.setValue(Field.TITLE, "C4.5: Programs for Machine Learning"); |
---|
199 | result.setValue(Field.PUBLISHER, "Morgan Kaufmann Publishers"); |
---|
200 | result.setValue(Field.ADDRESS, "San Mateo, CA"); |
---|
201 | |
---|
202 | return result; |
---|
203 | } |
---|
204 | |
---|
205 | /** |
---|
206 | * Returns default capabilities of the classifier. |
---|
207 | * |
---|
208 | * @return the capabilities of this classifier |
---|
209 | */ |
---|
210 | public Capabilities getCapabilities() { |
---|
211 | Capabilities result; |
---|
212 | |
---|
213 | try { |
---|
214 | if (!m_reducedErrorPruning) |
---|
215 | result = new C45PruneableClassifierTree(null, !m_unpruned, m_CF, m_subtreeRaising, !m_noCleanup, m_collapseTree).getCapabilities(); |
---|
216 | else |
---|
217 | result = new PruneableClassifierTree(null, !m_unpruned, m_numFolds, !m_noCleanup, m_Seed).getCapabilities(); |
---|
218 | } |
---|
219 | catch (Exception e) { |
---|
220 | result = new Capabilities(this); |
---|
221 | result.disableAll(); |
---|
222 | } |
---|
223 | |
---|
224 | result.setOwner(this); |
---|
225 | |
---|
226 | return result; |
---|
227 | } |
---|
228 | |
---|
229 | /** |
---|
230 | * Generates the classifier. |
---|
231 | * |
---|
232 | * @param instances the data to train the classifier with |
---|
233 | * @throws Exception if classifier can't be built successfully |
---|
234 | */ |
---|
235 | public void buildClassifier(Instances instances) |
---|
236 | throws Exception { |
---|
237 | |
---|
238 | ModelSelection modSelection; |
---|
239 | |
---|
240 | if (m_binarySplits) |
---|
241 | modSelection = new BinC45ModelSelection(m_minNumObj, instances, m_useMDLcorrection); |
---|
242 | else |
---|
243 | modSelection = new C45ModelSelection(m_minNumObj, instances, m_useMDLcorrection); |
---|
244 | if (!m_reducedErrorPruning) |
---|
245 | m_root = new C45PruneableClassifierTree(modSelection, !m_unpruned, m_CF, |
---|
246 | m_subtreeRaising, !m_noCleanup, m_collapseTree); |
---|
247 | else |
---|
248 | m_root = new PruneableClassifierTree(modSelection, !m_unpruned, m_numFolds, |
---|
249 | !m_noCleanup, m_Seed); |
---|
250 | m_root.buildClassifier(instances); |
---|
251 | if (m_binarySplits) { |
---|
252 | ((BinC45ModelSelection)modSelection).cleanup(); |
---|
253 | } else { |
---|
254 | ((C45ModelSelection)modSelection).cleanup(); |
---|
255 | } |
---|
256 | } |
---|
257 | |
---|
258 | /** |
---|
259 | * Classifies an instance. |
---|
260 | * |
---|
261 | * @param instance the instance to classify |
---|
262 | * @return the classification for the instance |
---|
263 | * @throws Exception if instance can't be classified successfully |
---|
264 | */ |
---|
265 | public double classifyInstance(Instance instance) throws Exception { |
---|
266 | |
---|
267 | return m_root.classifyInstance(instance); |
---|
268 | } |
---|
269 | |
---|
270 | /** |
---|
271 | * Returns class probabilities for an instance. |
---|
272 | * |
---|
273 | * @param instance the instance to calculate the class probabilities for |
---|
274 | * @return the class probabilities |
---|
275 | * @throws Exception if distribution can't be computed successfully |
---|
276 | */ |
---|
277 | public final double [] distributionForInstance(Instance instance) |
---|
278 | throws Exception { |
---|
279 | |
---|
280 | return m_root.distributionForInstance(instance, m_useLaplace); |
---|
281 | } |
---|
282 | |
---|
283 | /** |
---|
284 | * Returns the type of graph this classifier |
---|
285 | * represents. |
---|
286 | * @return Drawable.TREE |
---|
287 | */ |
---|
288 | public int graphType() { |
---|
289 | return Drawable.TREE; |
---|
290 | } |
---|
291 | |
---|
292 | /** |
---|
293 | * Returns graph describing the tree. |
---|
294 | * |
---|
295 | * @return the graph describing the tree |
---|
296 | * @throws Exception if graph can't be computed |
---|
297 | */ |
---|
298 | public String graph() throws Exception { |
---|
299 | |
---|
300 | return m_root.graph(); |
---|
301 | } |
---|
302 | |
---|
303 | /** |
---|
304 | * Returns tree in prefix order. |
---|
305 | * |
---|
306 | * @return the tree in prefix order |
---|
307 | * @throws Exception if something goes wrong |
---|
308 | */ |
---|
309 | public String prefix() throws Exception { |
---|
310 | |
---|
311 | return m_root.prefix(); |
---|
312 | } |
---|
313 | |
---|
314 | |
---|
315 | /** |
---|
316 | * Returns tree as an if-then statement. |
---|
317 | * |
---|
318 | * @param className the name of the Java class |
---|
319 | * @return the tree as a Java if-then type statement |
---|
320 | * @throws Exception if something goes wrong |
---|
321 | */ |
---|
322 | public String toSource(String className) throws Exception { |
---|
323 | |
---|
324 | StringBuffer [] source = m_root.toSource(className); |
---|
325 | return |
---|
326 | "class " + className + " {\n\n" |
---|
327 | +" public static double classify(Object[] i)\n" |
---|
328 | +" throws Exception {\n\n" |
---|
329 | +" double p = Double.NaN;\n" |
---|
330 | + source[0] // Assignment code |
---|
331 | +" return p;\n" |
---|
332 | +" }\n" |
---|
333 | + source[1] // Support code |
---|
334 | +"}\n"; |
---|
335 | } |
---|
336 | |
---|
337 | /** |
---|
338 | * Returns an enumeration describing the available options. |
---|
339 | * |
---|
340 | * Valid options are: <p> |
---|
341 | * |
---|
342 | * -U <br> |
---|
343 | * Use unpruned tree.<p> |
---|
344 | * |
---|
345 | * -C confidence <br> |
---|
346 | * Set confidence threshold for pruning. (Default: 0.25) <p> |
---|
347 | * |
---|
348 | * -M number <br> |
---|
349 | * Set minimum number of instances per leaf. (Default: 2) <p> |
---|
350 | * |
---|
351 | * -R <br> |
---|
352 | * Use reduced error pruning. No subtree raising is performed. <p> |
---|
353 | * |
---|
354 | * -N number <br> |
---|
355 | * Set number of folds for reduced error pruning. One fold is |
---|
356 | * used as the pruning set. (Default: 3) <p> |
---|
357 | * |
---|
358 | * -B <br> |
---|
359 | * Use binary splits for nominal attributes. <p> |
---|
360 | * |
---|
361 | * -S <br> |
---|
362 | * Don't perform subtree raising. <p> |
---|
363 | * |
---|
364 | * -L <br> |
---|
365 | * Do not clean up after the tree has been built. |
---|
366 | * |
---|
367 | * -A <br> |
---|
368 | * If set, Laplace smoothing is used for predicted probabilites. <p> |
---|
369 | * |
---|
370 | * -Q <br> |
---|
371 | * The seed for reduced-error pruning. <p> |
---|
372 | * |
---|
373 | * @return an enumeration of all the available options. |
---|
374 | */ |
---|
375 | public Enumeration listOptions() { |
---|
376 | |
---|
377 | Vector newVector = new Vector(12); |
---|
378 | |
---|
379 | newVector. |
---|
380 | addElement(new Option("\tUse unpruned tree.", |
---|
381 | "U", 0, "-U")); |
---|
382 | newVector. |
---|
383 | addElement(new Option("\tDo not collapse tree.", |
---|
384 | "O", 0, "-O")); |
---|
385 | newVector. |
---|
386 | addElement(new Option("\tSet confidence threshold for pruning.\n" + |
---|
387 | "\t(default 0.25)", |
---|
388 | "C", 1, "-C <pruning confidence>")); |
---|
389 | newVector. |
---|
390 | addElement(new Option("\tSet minimum number of instances per leaf.\n" + |
---|
391 | "\t(default 2)", |
---|
392 | "M", 1, "-M <minimum number of instances>")); |
---|
393 | newVector. |
---|
394 | addElement(new Option("\tUse reduced error pruning.", |
---|
395 | "R", 0, "-R")); |
---|
396 | newVector. |
---|
397 | addElement(new Option("\tSet number of folds for reduced error\n" + |
---|
398 | "\tpruning. One fold is used as pruning set.\n" + |
---|
399 | "\t(default 3)", |
---|
400 | "N", 1, "-N <number of folds>")); |
---|
401 | newVector. |
---|
402 | addElement(new Option("\tUse binary splits only.", |
---|
403 | "B", 0, "-B")); |
---|
404 | newVector. |
---|
405 | addElement(new Option("\tDon't perform subtree raising.", |
---|
406 | "S", 0, "-S")); |
---|
407 | newVector. |
---|
408 | addElement(new Option("\tDo not clean up after the tree has been built.", |
---|
409 | "L", 0, "-L")); |
---|
410 | newVector. |
---|
411 | addElement(new Option("\tLaplace smoothing for predicted probabilities.", |
---|
412 | "A", 0, "-A")); |
---|
413 | newVector. |
---|
414 | addElement(new Option("\tDo not use MDL correction for info gain on numeric attributes.", |
---|
415 | "J", 0, "-J")); |
---|
416 | newVector. |
---|
417 | addElement(new Option("\tSeed for random data shuffling (default 1).", |
---|
418 | "Q", 1, "-Q <seed>")); |
---|
419 | |
---|
420 | return newVector.elements(); |
---|
421 | } |
---|
422 | |
---|
423 | /** |
---|
424 | * Parses a given list of options. |
---|
425 | * |
---|
426 | <!-- options-start --> |
---|
427 | * Valid options are: <p/> |
---|
428 | * |
---|
429 | * <pre> -U |
---|
430 | * Use unpruned tree.</pre> |
---|
431 | * |
---|
432 | * <pre> -O |
---|
433 | * Do not collapse tree.</pre> |
---|
434 | * |
---|
435 | * <pre> -C <pruning confidence> |
---|
436 | * Set confidence threshold for pruning. |
---|
437 | * (default 0.25)</pre> |
---|
438 | * |
---|
439 | * <pre> -M <minimum number of instances> |
---|
440 | * Set minimum number of instances per leaf. |
---|
441 | * (default 2)</pre> |
---|
442 | * |
---|
443 | * <pre> -R |
---|
444 | * Use reduced error pruning.</pre> |
---|
445 | * |
---|
446 | * <pre> -N <number of folds> |
---|
447 | * Set number of folds for reduced error |
---|
448 | * pruning. One fold is used as pruning set. |
---|
449 | * (default 3)</pre> |
---|
450 | * |
---|
451 | * <pre> -B |
---|
452 | * Use binary splits only.</pre> |
---|
453 | * |
---|
454 | * <pre> -S |
---|
455 | * Don't perform subtree raising.</pre> |
---|
456 | * |
---|
457 | * <pre> -L |
---|
458 | * Do not clean up after the tree has been built.</pre> |
---|
459 | * |
---|
460 | * <pre> -A |
---|
461 | * Laplace smoothing for predicted probabilities.</pre> |
---|
462 | * |
---|
463 | * <pre> -J |
---|
464 | * Do not use MDL correction for info gain on numeric attributes.</pre> |
---|
465 | * |
---|
466 | * <pre> -Q <seed> |
---|
467 | * Seed for random data shuffling (default 1).</pre> |
---|
468 | * |
---|
469 | <!-- options-end --> |
---|
470 | * |
---|
471 | * @param options the list of options as an array of strings |
---|
472 | * @throws Exception if an option is not supported |
---|
473 | */ |
---|
474 | public void setOptions(String[] options) throws Exception { |
---|
475 | |
---|
476 | // Other options |
---|
477 | String minNumString = Utils.getOption('M', options); |
---|
478 | if (minNumString.length() != 0) { |
---|
479 | m_minNumObj = Integer.parseInt(minNumString); |
---|
480 | } else { |
---|
481 | m_minNumObj = 2; |
---|
482 | } |
---|
483 | m_binarySplits = Utils.getFlag('B', options); |
---|
484 | m_useLaplace = Utils.getFlag('A', options); |
---|
485 | m_useMDLcorrection = !Utils.getFlag('J', options); |
---|
486 | |
---|
487 | // Pruning options |
---|
488 | m_unpruned = Utils.getFlag('U', options); |
---|
489 | m_collapseTree = !Utils.getFlag('O', options); |
---|
490 | m_subtreeRaising = !Utils.getFlag('S', options); |
---|
491 | m_noCleanup = Utils.getFlag('L', options); |
---|
492 | if ((m_unpruned) && (!m_subtreeRaising)) { |
---|
493 | throw new Exception("Subtree raising doesn't need to be unset for unpruned tree!"); |
---|
494 | } |
---|
495 | m_reducedErrorPruning = Utils.getFlag('R', options); |
---|
496 | if ((m_unpruned) && (m_reducedErrorPruning)) { |
---|
497 | throw new Exception("Unpruned tree and reduced error pruning can't be selected " + |
---|
498 | "simultaneously!"); |
---|
499 | } |
---|
500 | String confidenceString = Utils.getOption('C', options); |
---|
501 | if (confidenceString.length() != 0) { |
---|
502 | if (m_reducedErrorPruning) { |
---|
503 | throw new Exception("Setting the confidence doesn't make sense " + |
---|
504 | "for reduced error pruning."); |
---|
505 | } else if (m_unpruned) { |
---|
506 | throw new Exception("Doesn't make sense to change confidence for unpruned " |
---|
507 | +"tree!"); |
---|
508 | } else { |
---|
509 | m_CF = (new Float(confidenceString)).floatValue(); |
---|
510 | if ((m_CF <= 0) || (m_CF >= 1)) { |
---|
511 | throw new Exception("Confidence has to be greater than zero and smaller " + |
---|
512 | "than one!"); |
---|
513 | } |
---|
514 | } |
---|
515 | } else { |
---|
516 | m_CF = 0.25f; |
---|
517 | } |
---|
518 | String numFoldsString = Utils.getOption('N', options); |
---|
519 | if (numFoldsString.length() != 0) { |
---|
520 | if (!m_reducedErrorPruning) { |
---|
521 | throw new Exception("Setting the number of folds" + |
---|
522 | " doesn't make sense if" + |
---|
523 | " reduced error pruning is not selected."); |
---|
524 | } else { |
---|
525 | m_numFolds = Integer.parseInt(numFoldsString); |
---|
526 | } |
---|
527 | } else { |
---|
528 | m_numFolds = 3; |
---|
529 | } |
---|
530 | String seedString = Utils.getOption('Q', options); |
---|
531 | if (seedString.length() != 0) { |
---|
532 | m_Seed = Integer.parseInt(seedString); |
---|
533 | } else { |
---|
534 | m_Seed = 1; |
---|
535 | } |
---|
536 | } |
---|
537 | |
---|
538 | /** |
---|
539 | * Gets the current settings of the Classifier. |
---|
540 | * |
---|
541 | * @return an array of strings suitable for passing to setOptions |
---|
542 | */ |
---|
543 | public String [] getOptions() { |
---|
544 | |
---|
545 | String [] options = new String [16]; |
---|
546 | int current = 0; |
---|
547 | |
---|
548 | if (m_noCleanup) { |
---|
549 | options[current++] = "-L"; |
---|
550 | } |
---|
551 | if (!m_collapseTree) { |
---|
552 | options[current++] = "-O"; |
---|
553 | } |
---|
554 | if (m_unpruned) { |
---|
555 | options[current++] = "-U"; |
---|
556 | } else { |
---|
557 | if (!m_subtreeRaising) { |
---|
558 | options[current++] = "-S"; |
---|
559 | } |
---|
560 | if (m_reducedErrorPruning) { |
---|
561 | options[current++] = "-R"; |
---|
562 | options[current++] = "-N"; options[current++] = "" + m_numFolds; |
---|
563 | options[current++] = "-Q"; options[current++] = "" + m_Seed; |
---|
564 | } else { |
---|
565 | options[current++] = "-C"; options[current++] = "" + m_CF; |
---|
566 | } |
---|
567 | } |
---|
568 | if (m_binarySplits) { |
---|
569 | options[current++] = "-B"; |
---|
570 | } |
---|
571 | options[current++] = "-M"; options[current++] = "" + m_minNumObj; |
---|
572 | if (m_useLaplace) { |
---|
573 | options[current++] = "-A"; |
---|
574 | } |
---|
575 | if (!m_useMDLcorrection) { |
---|
576 | options[current++] = "-J"; |
---|
577 | } |
---|
578 | |
---|
579 | while (current < options.length) { |
---|
580 | options[current++] = ""; |
---|
581 | } |
---|
582 | return options; |
---|
583 | } |
---|
584 | |
---|
585 | /** |
---|
586 | * Returns the tip text for this property |
---|
587 | * @return tip text for this property suitable for |
---|
588 | * displaying in the explorer/experimenter gui |
---|
589 | */ |
---|
590 | public String seedTipText() { |
---|
591 | return "The seed used for randomizing the data " + |
---|
592 | "when reduced-error pruning is used."; |
---|
593 | } |
---|
594 | |
---|
595 | /** |
---|
596 | * Get the value of Seed. |
---|
597 | * |
---|
598 | * @return Value of Seed. |
---|
599 | */ |
---|
600 | public int getSeed() { |
---|
601 | |
---|
602 | return m_Seed; |
---|
603 | } |
---|
604 | |
---|
605 | /** |
---|
606 | * Set the value of Seed. |
---|
607 | * |
---|
608 | * @param newSeed Value to assign to Seed. |
---|
609 | */ |
---|
610 | public void setSeed(int newSeed) { |
---|
611 | |
---|
612 | m_Seed = newSeed; |
---|
613 | } |
---|
614 | |
---|
615 | /** |
---|
616 | * Returns the tip text for this property |
---|
617 | * @return tip text for this property suitable for |
---|
618 | * displaying in the explorer/experimenter gui |
---|
619 | */ |
---|
620 | public String useLaplaceTipText() { |
---|
621 | return "Whether counts at leaves are smoothed based on Laplace."; |
---|
622 | } |
---|
623 | |
---|
624 | /** |
---|
625 | * Get the value of useLaplace. |
---|
626 | * |
---|
627 | * @return Value of useLaplace. |
---|
628 | */ |
---|
629 | public boolean getUseLaplace() { |
---|
630 | |
---|
631 | return m_useLaplace; |
---|
632 | } |
---|
633 | |
---|
634 | /** |
---|
635 | * Set the value of useLaplace. |
---|
636 | * |
---|
637 | * @param newuseLaplace Value to assign to useLaplace. |
---|
638 | */ |
---|
639 | public void setUseLaplace(boolean newuseLaplace) { |
---|
640 | |
---|
641 | m_useLaplace = newuseLaplace; |
---|
642 | } |
---|
643 | |
---|
644 | /** |
---|
645 | * Returns the tip text for this property |
---|
646 | * @return tip text for this property suitable for |
---|
647 | * displaying in the explorer/experimenter gui |
---|
648 | */ |
---|
649 | public String useMDLcorrectionTipText() { |
---|
650 | return "Whether MDL correction is used when finding splits on numeric attributes."; |
---|
651 | } |
---|
652 | |
---|
653 | /** |
---|
654 | * Get the value of useMDLcorrection. |
---|
655 | * |
---|
656 | * @return Value of useMDLcorrection. |
---|
657 | */ |
---|
658 | public boolean getUseMDLcorrection() { |
---|
659 | |
---|
660 | return m_useMDLcorrection; |
---|
661 | } |
---|
662 | |
---|
663 | /** |
---|
664 | * Set the value of useMDLcorrection. |
---|
665 | * |
---|
666 | * @param newuseMDLcorrection Value to assign to useMDLcorrection. |
---|
667 | */ |
---|
668 | public void setUseMDLcorrection(boolean newuseMDLcorrection) { |
---|
669 | |
---|
670 | m_useMDLcorrection = newuseMDLcorrection; |
---|
671 | } |
---|
672 | |
---|
673 | /** |
---|
674 | * Returns a description of the classifier. |
---|
675 | * |
---|
676 | * @return a description of the classifier |
---|
677 | */ |
---|
678 | public String toString() { |
---|
679 | |
---|
680 | if (m_root == null) { |
---|
681 | return "No classifier built"; |
---|
682 | } |
---|
683 | if (m_unpruned) |
---|
684 | return "J48 unpruned tree\n------------------\n" + m_root.toString(); |
---|
685 | else |
---|
686 | return "J48 pruned tree\n------------------\n" + m_root.toString(); |
---|
687 | } |
---|
688 | |
---|
689 | /** |
---|
690 | * Returns a superconcise version of the model |
---|
691 | * |
---|
692 | * @return a summary of the model |
---|
693 | */ |
---|
694 | public String toSummaryString() { |
---|
695 | |
---|
696 | return "Number of leaves: " + m_root.numLeaves() + "\n" |
---|
697 | + "Size of the tree: " + m_root.numNodes() + "\n"; |
---|
698 | } |
---|
699 | |
---|
700 | /** |
---|
701 | * Returns the size of the tree |
---|
702 | * @return the size of the tree |
---|
703 | */ |
---|
704 | public double measureTreeSize() { |
---|
705 | return m_root.numNodes(); |
---|
706 | } |
---|
707 | |
---|
708 | /** |
---|
709 | * Returns the number of leaves |
---|
710 | * @return the number of leaves |
---|
711 | */ |
---|
712 | public double measureNumLeaves() { |
---|
713 | return m_root.numLeaves(); |
---|
714 | } |
---|
715 | |
---|
716 | /** |
---|
717 | * Returns the number of rules (same as number of leaves) |
---|
718 | * @return the number of rules |
---|
719 | */ |
---|
720 | public double measureNumRules() { |
---|
721 | return m_root.numLeaves(); |
---|
722 | } |
---|
723 | |
---|
724 | /** |
---|
725 | * Returns an enumeration of the additional measure names |
---|
726 | * @return an enumeration of the measure names |
---|
727 | */ |
---|
728 | public Enumeration enumerateMeasures() { |
---|
729 | Vector newVector = new Vector(3); |
---|
730 | newVector.addElement("measureTreeSize"); |
---|
731 | newVector.addElement("measureNumLeaves"); |
---|
732 | newVector.addElement("measureNumRules"); |
---|
733 | return newVector.elements(); |
---|
734 | } |
---|
735 | |
---|
736 | /** |
---|
737 | * Returns the value of the named measure |
---|
738 | * @param additionalMeasureName the name of the measure to query for its value |
---|
739 | * @return the value of the named measure |
---|
740 | * @throws IllegalArgumentException if the named measure is not supported |
---|
741 | */ |
---|
742 | public double getMeasure(String additionalMeasureName) { |
---|
743 | if (additionalMeasureName.compareToIgnoreCase("measureNumRules") == 0) { |
---|
744 | return measureNumRules(); |
---|
745 | } else if (additionalMeasureName.compareToIgnoreCase("measureTreeSize") == 0) { |
---|
746 | return measureTreeSize(); |
---|
747 | } else if (additionalMeasureName.compareToIgnoreCase("measureNumLeaves") == 0) { |
---|
748 | return measureNumLeaves(); |
---|
749 | } else { |
---|
750 | throw new IllegalArgumentException(additionalMeasureName |
---|
751 | + " not supported (j48)"); |
---|
752 | } |
---|
753 | } |
---|
754 | |
---|
755 | /** |
---|
756 | * Returns the tip text for this property |
---|
757 | * @return tip text for this property suitable for |
---|
758 | * displaying in the explorer/experimenter gui |
---|
759 | */ |
---|
760 | public String unprunedTipText() { |
---|
761 | return "Whether pruning is performed."; |
---|
762 | } |
---|
763 | |
---|
764 | /** |
---|
765 | * Get the value of unpruned. |
---|
766 | * |
---|
767 | * @return Value of unpruned. |
---|
768 | */ |
---|
769 | public boolean getUnpruned() { |
---|
770 | |
---|
771 | return m_unpruned; |
---|
772 | } |
---|
773 | |
---|
774 | /** |
---|
775 | * Set the value of unpruned. Turns reduced-error pruning |
---|
776 | * off if set. |
---|
777 | * @param v Value to assign to unpruned. |
---|
778 | */ |
---|
779 | public void setUnpruned(boolean v) { |
---|
780 | |
---|
781 | if (v) { |
---|
782 | m_reducedErrorPruning = false; |
---|
783 | } |
---|
784 | m_unpruned = v; |
---|
785 | } |
---|
786 | |
---|
787 | /** |
---|
788 | * Returns the tip text for this property |
---|
789 | * @return tip text for this property suitable for |
---|
790 | * displaying in the explorer/experimenter gui |
---|
791 | */ |
---|
792 | public String collapseTreeTipText() { |
---|
793 | return "Whether parts are removed that do not reduce training error."; |
---|
794 | } |
---|
795 | |
---|
796 | /** |
---|
797 | * Get the value of collapseTree. |
---|
798 | * |
---|
799 | * @return Value of collapseTree. |
---|
800 | */ |
---|
801 | public boolean getCollapseTree() { |
---|
802 | |
---|
803 | return m_collapseTree; |
---|
804 | } |
---|
805 | |
---|
806 | /** |
---|
807 | * Set the value of collapseTree. |
---|
808 | * @param v Value to assign to collapseTree. |
---|
809 | */ |
---|
810 | public void setCollapseTree(boolean v) { |
---|
811 | |
---|
812 | m_collapseTree = v; |
---|
813 | } |
---|
814 | |
---|
815 | /** |
---|
816 | * Returns the tip text for this property |
---|
817 | * @return tip text for this property suitable for |
---|
818 | * displaying in the explorer/experimenter gui |
---|
819 | */ |
---|
820 | public String confidenceFactorTipText() { |
---|
821 | return "The confidence factor used for pruning (smaller values incur " |
---|
822 | + "more pruning)."; |
---|
823 | } |
---|
824 | |
---|
825 | /** |
---|
826 | * Get the value of CF. |
---|
827 | * |
---|
828 | * @return Value of CF. |
---|
829 | */ |
---|
830 | public float getConfidenceFactor() { |
---|
831 | |
---|
832 | return m_CF; |
---|
833 | } |
---|
834 | |
---|
835 | /** |
---|
836 | * Set the value of CF. |
---|
837 | * |
---|
838 | * @param v Value to assign to CF. |
---|
839 | */ |
---|
840 | public void setConfidenceFactor(float v) { |
---|
841 | |
---|
842 | m_CF = v; |
---|
843 | } |
---|
844 | |
---|
845 | /** |
---|
846 | * Returns the tip text for this property |
---|
847 | * @return tip text for this property suitable for |
---|
848 | * displaying in the explorer/experimenter gui |
---|
849 | */ |
---|
850 | public String minNumObjTipText() { |
---|
851 | return "The minimum number of instances per leaf."; |
---|
852 | } |
---|
853 | |
---|
854 | /** |
---|
855 | * Get the value of minNumObj. |
---|
856 | * |
---|
857 | * @return Value of minNumObj. |
---|
858 | */ |
---|
859 | public int getMinNumObj() { |
---|
860 | |
---|
861 | return m_minNumObj; |
---|
862 | } |
---|
863 | |
---|
864 | /** |
---|
865 | * Set the value of minNumObj. |
---|
866 | * |
---|
867 | * @param v Value to assign to minNumObj. |
---|
868 | */ |
---|
869 | public void setMinNumObj(int v) { |
---|
870 | |
---|
871 | m_minNumObj = v; |
---|
872 | } |
---|
873 | |
---|
874 | /** |
---|
875 | * Returns the tip text for this property |
---|
876 | * @return tip text for this property suitable for |
---|
877 | * displaying in the explorer/experimenter gui |
---|
878 | */ |
---|
879 | public String reducedErrorPruningTipText() { |
---|
880 | return "Whether reduced-error pruning is used instead of C.4.5 pruning."; |
---|
881 | } |
---|
882 | |
---|
883 | /** |
---|
884 | * Get the value of reducedErrorPruning. |
---|
885 | * |
---|
886 | * @return Value of reducedErrorPruning. |
---|
887 | */ |
---|
888 | public boolean getReducedErrorPruning() { |
---|
889 | |
---|
890 | return m_reducedErrorPruning; |
---|
891 | } |
---|
892 | |
---|
893 | /** |
---|
894 | * Set the value of reducedErrorPruning. Turns |
---|
895 | * unpruned trees off if set. |
---|
896 | * |
---|
897 | * @param v Value to assign to reducedErrorPruning. |
---|
898 | */ |
---|
899 | public void setReducedErrorPruning(boolean v) { |
---|
900 | |
---|
901 | if (v) { |
---|
902 | m_unpruned = false; |
---|
903 | } |
---|
904 | m_reducedErrorPruning = v; |
---|
905 | } |
---|
906 | |
---|
907 | /** |
---|
908 | * Returns the tip text for this property |
---|
909 | * @return tip text for this property suitable for |
---|
910 | * displaying in the explorer/experimenter gui |
---|
911 | */ |
---|
912 | public String numFoldsTipText() { |
---|
913 | return "Determines the amount of data used for reduced-error pruning. " |
---|
914 | + " One fold is used for pruning, the rest for growing the tree."; |
---|
915 | } |
---|
916 | |
---|
917 | /** |
---|
918 | * Get the value of numFolds. |
---|
919 | * |
---|
920 | * @return Value of numFolds. |
---|
921 | */ |
---|
922 | public int getNumFolds() { |
---|
923 | |
---|
924 | return m_numFolds; |
---|
925 | } |
---|
926 | |
---|
927 | /** |
---|
928 | * Set the value of numFolds. |
---|
929 | * |
---|
930 | * @param v Value to assign to numFolds. |
---|
931 | */ |
---|
932 | public void setNumFolds(int v) { |
---|
933 | |
---|
934 | m_numFolds = v; |
---|
935 | } |
---|
936 | |
---|
937 | /** |
---|
938 | * Returns the tip text for this property |
---|
939 | * @return tip text for this property suitable for |
---|
940 | * displaying in the explorer/experimenter gui |
---|
941 | */ |
---|
942 | public String binarySplitsTipText() { |
---|
943 | return "Whether to use binary splits on nominal attributes when " |
---|
944 | + "building the trees."; |
---|
945 | } |
---|
946 | |
---|
947 | /** |
---|
948 | * Get the value of binarySplits. |
---|
949 | * |
---|
950 | * @return Value of binarySplits. |
---|
951 | */ |
---|
952 | public boolean getBinarySplits() { |
---|
953 | |
---|
954 | return m_binarySplits; |
---|
955 | } |
---|
956 | |
---|
957 | /** |
---|
958 | * Set the value of binarySplits. |
---|
959 | * |
---|
960 | * @param v Value to assign to binarySplits. |
---|
961 | */ |
---|
962 | public void setBinarySplits(boolean v) { |
---|
963 | |
---|
964 | m_binarySplits = v; |
---|
965 | } |
---|
966 | |
---|
967 | /** |
---|
968 | * Returns the tip text for this property |
---|
969 | * @return tip text for this property suitable for |
---|
970 | * displaying in the explorer/experimenter gui |
---|
971 | */ |
---|
972 | public String subtreeRaisingTipText() { |
---|
973 | return "Whether to consider the subtree raising operation when pruning."; |
---|
974 | } |
---|
975 | |
---|
976 | /** |
---|
977 | * Get the value of subtreeRaising. |
---|
978 | * |
---|
979 | * @return Value of subtreeRaising. |
---|
980 | */ |
---|
981 | public boolean getSubtreeRaising() { |
---|
982 | |
---|
983 | return m_subtreeRaising; |
---|
984 | } |
---|
985 | |
---|
986 | /** |
---|
987 | * Set the value of subtreeRaising. |
---|
988 | * |
---|
989 | * @param v Value to assign to subtreeRaising. |
---|
990 | */ |
---|
991 | public void setSubtreeRaising(boolean v) { |
---|
992 | |
---|
993 | m_subtreeRaising = v; |
---|
994 | } |
---|
995 | |
---|
996 | /** |
---|
997 | * Returns the tip text for this property |
---|
998 | * @return tip text for this property suitable for |
---|
999 | * displaying in the explorer/experimenter gui |
---|
1000 | */ |
---|
1001 | public String saveInstanceDataTipText() { |
---|
1002 | return "Whether to save the training data for visualization."; |
---|
1003 | } |
---|
1004 | |
---|
1005 | /** |
---|
1006 | * Check whether instance data is to be saved. |
---|
1007 | * |
---|
1008 | * @return true if instance data is saved |
---|
1009 | */ |
---|
1010 | public boolean getSaveInstanceData() { |
---|
1011 | |
---|
1012 | return m_noCleanup; |
---|
1013 | } |
---|
1014 | |
---|
1015 | /** |
---|
1016 | * Set whether instance data is to be saved. |
---|
1017 | * @param v true if instance data is to be saved |
---|
1018 | */ |
---|
1019 | public void setSaveInstanceData(boolean v) { |
---|
1020 | |
---|
1021 | m_noCleanup = v; |
---|
1022 | } |
---|
1023 | |
---|
1024 | /** |
---|
1025 | * Returns the revision string. |
---|
1026 | * |
---|
1027 | * @return the revision |
---|
1028 | */ |
---|
1029 | public String getRevision() { |
---|
1030 | return RevisionUtils.extract("$Revision: 6088 $"); |
---|
1031 | } |
---|
1032 | |
---|
1033 | /** |
---|
1034 | * Main method for testing this class |
---|
1035 | * |
---|
1036 | * @param argv the commandline options |
---|
1037 | */ |
---|
1038 | public static void main(String [] argv){ |
---|
1039 | runClassifier(new J48(), argv); |
---|
1040 | } |
---|
1041 | } |
---|
1042 | |
---|