| [4] | 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 | * NBTree.java |
|---|
| 19 | * Copyright (C) 2004 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.trees.j48.NBTreeClassifierTree; |
|---|
| 28 | import weka.classifiers.trees.j48.NBTreeModelSelection; |
|---|
| 29 | import weka.core.AdditionalMeasureProducer; |
|---|
| 30 | import weka.core.Capabilities; |
|---|
| 31 | import weka.core.Drawable; |
|---|
| 32 | import weka.core.Instance; |
|---|
| 33 | import weka.core.Instances; |
|---|
| 34 | import weka.core.RevisionUtils; |
|---|
| 35 | import weka.core.Summarizable; |
|---|
| 36 | import weka.core.TechnicalInformation; |
|---|
| 37 | import weka.core.TechnicalInformationHandler; |
|---|
| 38 | import weka.core.WeightedInstancesHandler; |
|---|
| 39 | import weka.core.TechnicalInformation.Field; |
|---|
| 40 | import weka.core.TechnicalInformation.Type; |
|---|
| 41 | |
|---|
| 42 | import java.util.Enumeration; |
|---|
| 43 | import java.util.Vector; |
|---|
| 44 | |
|---|
| 45 | /** |
|---|
| 46 | <!-- globalinfo-start --> |
|---|
| 47 | * Class for generating a decision tree with naive Bayes classifiers at the leaves.<br/> |
|---|
| 48 | * <br/> |
|---|
| 49 | * For more information, see<br/> |
|---|
| 50 | * <br/> |
|---|
| 51 | * Ron Kohavi: Scaling Up the Accuracy of Naive-Bayes Classifiers: A Decision-Tree Hybrid. In: Second International Conference on Knoledge Discovery and Data Mining, 202-207, 1996. |
|---|
| 52 | * <p/> |
|---|
| 53 | <!-- globalinfo-end --> |
|---|
| 54 | * |
|---|
| 55 | <!-- technical-bibtex-start --> |
|---|
| 56 | * BibTeX: |
|---|
| 57 | * <pre> |
|---|
| 58 | * @inproceedings{Kohavi1996, |
|---|
| 59 | * author = {Ron Kohavi}, |
|---|
| 60 | * booktitle = {Second International Conference on Knoledge Discovery and Data Mining}, |
|---|
| 61 | * pages = {202-207}, |
|---|
| 62 | * title = {Scaling Up the Accuracy of Naive-Bayes Classifiers: A Decision-Tree Hybrid}, |
|---|
| 63 | * year = {1996} |
|---|
| 64 | * } |
|---|
| 65 | * </pre> |
|---|
| 66 | * <p/> |
|---|
| 67 | <!-- technical-bibtex-end --> |
|---|
| 68 | * |
|---|
| 69 | <!-- options-start --> |
|---|
| 70 | * Valid options are: <p/> |
|---|
| 71 | * |
|---|
| 72 | * <pre> -D |
|---|
| 73 | * If set, classifier is run in debug mode and |
|---|
| 74 | * may output additional info to the console</pre> |
|---|
| 75 | * |
|---|
| 76 | <!-- options-end --> |
|---|
| 77 | * |
|---|
| 78 | * @author Mark Hall |
|---|
| 79 | * @version $Revision: 5928 $ |
|---|
| 80 | */ |
|---|
| 81 | public class NBTree |
|---|
| 82 | extends AbstractClassifier |
|---|
| 83 | implements WeightedInstancesHandler, Drawable, Summarizable, |
|---|
| 84 | AdditionalMeasureProducer, TechnicalInformationHandler { |
|---|
| 85 | |
|---|
| 86 | /** for serialization */ |
|---|
| 87 | static final long serialVersionUID = -4716005707058256086L; |
|---|
| 88 | |
|---|
| 89 | /** Minimum number of instances */ |
|---|
| 90 | private int m_minNumObj = 30; |
|---|
| 91 | |
|---|
| 92 | /** The root of the tree */ |
|---|
| 93 | private NBTreeClassifierTree m_root; |
|---|
| 94 | |
|---|
| 95 | /** |
|---|
| 96 | * Returns a string describing classifier |
|---|
| 97 | * @return a description suitable for |
|---|
| 98 | * displaying in the explorer/experimenter gui |
|---|
| 99 | */ |
|---|
| 100 | public String globalInfo() { |
|---|
| 101 | return "Class for generating a decision tree with naive Bayes classifiers at " |
|---|
| 102 | + "the leaves.\n\n" |
|---|
| 103 | + "For more information, see\n\n" |
|---|
| 104 | + getTechnicalInformation().toString(); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | /** |
|---|
| 108 | * Returns an instance of a TechnicalInformation object, containing |
|---|
| 109 | * detailed information about the technical background of this class, |
|---|
| 110 | * e.g., paper reference or book this class is based on. |
|---|
| 111 | * |
|---|
| 112 | * @return the technical information about this class |
|---|
| 113 | */ |
|---|
| 114 | public TechnicalInformation getTechnicalInformation() { |
|---|
| 115 | TechnicalInformation result; |
|---|
| 116 | |
|---|
| 117 | result = new TechnicalInformation(Type.INPROCEEDINGS); |
|---|
| 118 | result.setValue(Field.AUTHOR, "Ron Kohavi"); |
|---|
| 119 | result.setValue(Field.TITLE, "Scaling Up the Accuracy of Naive-Bayes Classifiers: A Decision-Tree Hybrid"); |
|---|
| 120 | result.setValue(Field.BOOKTITLE, "Second International Conference on Knoledge Discovery and Data Mining"); |
|---|
| 121 | result.setValue(Field.YEAR, "1996"); |
|---|
| 122 | result.setValue(Field.PAGES, "202-207"); |
|---|
| 123 | |
|---|
| 124 | return result; |
|---|
| 125 | } |
|---|
| 126 | |
|---|
| 127 | /** |
|---|
| 128 | * Returns default capabilities of the classifier. |
|---|
| 129 | * |
|---|
| 130 | * @return the capabilities of this classifier |
|---|
| 131 | */ |
|---|
| 132 | public Capabilities getCapabilities() { |
|---|
| 133 | return new NBTreeClassifierTree(null).getCapabilities(); |
|---|
| 134 | } |
|---|
| 135 | |
|---|
| 136 | /** |
|---|
| 137 | * Generates the classifier. |
|---|
| 138 | * |
|---|
| 139 | * @param instances the data to train with |
|---|
| 140 | * @throws Exception if classifier can't be built successfully |
|---|
| 141 | */ |
|---|
| 142 | public void buildClassifier(Instances instances) throws Exception { |
|---|
| 143 | |
|---|
| 144 | NBTreeModelSelection modSelection = |
|---|
| 145 | new NBTreeModelSelection(m_minNumObj, instances); |
|---|
| 146 | |
|---|
| 147 | m_root = new NBTreeClassifierTree(modSelection); |
|---|
| 148 | m_root.buildClassifier(instances); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | /** |
|---|
| 152 | * Classifies an instance. |
|---|
| 153 | * |
|---|
| 154 | * @param instance the instance to classify |
|---|
| 155 | * @return the classification |
|---|
| 156 | * @throws Exception if instance can't be classified successfully |
|---|
| 157 | */ |
|---|
| 158 | public double classifyInstance(Instance instance) throws Exception { |
|---|
| 159 | |
|---|
| 160 | return m_root.classifyInstance(instance); |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | /** |
|---|
| 164 | * Returns class probabilities for an instance. |
|---|
| 165 | * |
|---|
| 166 | * @param instance the instance to get the distribution for |
|---|
| 167 | * @return the class probabilities |
|---|
| 168 | * @throws Exception if distribution can't be computed successfully |
|---|
| 169 | */ |
|---|
| 170 | public final double[] distributionForInstance(Instance instance) |
|---|
| 171 | throws Exception { |
|---|
| 172 | |
|---|
| 173 | return m_root.distributionForInstance(instance, false); |
|---|
| 174 | } |
|---|
| 175 | |
|---|
| 176 | /** |
|---|
| 177 | * Returns a description of the classifier. |
|---|
| 178 | * |
|---|
| 179 | * @return a string representation of the classifier |
|---|
| 180 | */ |
|---|
| 181 | public String toString() { |
|---|
| 182 | |
|---|
| 183 | if (m_root == null) { |
|---|
| 184 | return "No classifier built"; |
|---|
| 185 | } |
|---|
| 186 | return "NBTree\n------------------\n" + m_root.toString(); |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /** |
|---|
| 190 | * Returns the type of graph this classifier |
|---|
| 191 | * represents. |
|---|
| 192 | * @return Drawable.TREE |
|---|
| 193 | */ |
|---|
| 194 | public int graphType() { |
|---|
| 195 | return Drawable.TREE; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | /** |
|---|
| 199 | * Returns graph describing the tree. |
|---|
| 200 | * |
|---|
| 201 | * @return the graph describing the tree |
|---|
| 202 | * @throws Exception if graph can't be computed |
|---|
| 203 | */ |
|---|
| 204 | public String graph() throws Exception { |
|---|
| 205 | |
|---|
| 206 | return m_root.graph(); |
|---|
| 207 | } |
|---|
| 208 | |
|---|
| 209 | /** |
|---|
| 210 | * Returns a superconcise version of the model |
|---|
| 211 | * |
|---|
| 212 | * @return a description of the model |
|---|
| 213 | */ |
|---|
| 214 | public String toSummaryString() { |
|---|
| 215 | |
|---|
| 216 | return "Number of leaves: " + m_root.numLeaves() + "\n" |
|---|
| 217 | + "Size of the tree: " + m_root.numNodes() + "\n"; |
|---|
| 218 | } |
|---|
| 219 | |
|---|
| 220 | /** |
|---|
| 221 | * Returns the size of the tree |
|---|
| 222 | * @return the size of the tree |
|---|
| 223 | */ |
|---|
| 224 | public double measureTreeSize() { |
|---|
| 225 | return m_root.numNodes(); |
|---|
| 226 | } |
|---|
| 227 | |
|---|
| 228 | /** |
|---|
| 229 | * Returns the number of leaves |
|---|
| 230 | * @return the number of leaves |
|---|
| 231 | */ |
|---|
| 232 | public double measureNumLeaves() { |
|---|
| 233 | return m_root.numLeaves(); |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | /** |
|---|
| 237 | * Returns the number of rules (same as number of leaves) |
|---|
| 238 | * @return the number of rules |
|---|
| 239 | */ |
|---|
| 240 | public double measureNumRules() { |
|---|
| 241 | return m_root.numLeaves(); |
|---|
| 242 | } |
|---|
| 243 | |
|---|
| 244 | /** |
|---|
| 245 | * Returns the value of the named measure |
|---|
| 246 | * @param additionalMeasureName the name of the measure to query for its value |
|---|
| 247 | * @return the value of the named measure |
|---|
| 248 | * @throws IllegalArgumentException if the named measure is not supported |
|---|
| 249 | */ |
|---|
| 250 | public double getMeasure(String additionalMeasureName) { |
|---|
| 251 | if (additionalMeasureName.compareToIgnoreCase("measureNumRules") == 0) { |
|---|
| 252 | return measureNumRules(); |
|---|
| 253 | } else if (additionalMeasureName.compareToIgnoreCase("measureTreeSize") == 0) { |
|---|
| 254 | return measureTreeSize(); |
|---|
| 255 | } else if (additionalMeasureName.compareToIgnoreCase("measureNumLeaves") == 0) { |
|---|
| 256 | return measureNumLeaves(); |
|---|
| 257 | } else { |
|---|
| 258 | throw new IllegalArgumentException(additionalMeasureName |
|---|
| 259 | + " not supported (j48)"); |
|---|
| 260 | } |
|---|
| 261 | } |
|---|
| 262 | |
|---|
| 263 | /** |
|---|
| 264 | * Returns an enumeration of the additional measure names |
|---|
| 265 | * @return an enumeration of the measure names |
|---|
| 266 | */ |
|---|
| 267 | public Enumeration enumerateMeasures() { |
|---|
| 268 | Vector newVector = new Vector(3); |
|---|
| 269 | newVector.addElement("measureTreeSize"); |
|---|
| 270 | newVector.addElement("measureNumLeaves"); |
|---|
| 271 | newVector.addElement("measureNumRules"); |
|---|
| 272 | return newVector.elements(); |
|---|
| 273 | } |
|---|
| 274 | |
|---|
| 275 | /** |
|---|
| 276 | * Returns the revision string. |
|---|
| 277 | * |
|---|
| 278 | * @return the revision |
|---|
| 279 | */ |
|---|
| 280 | public String getRevision() { |
|---|
| 281 | return RevisionUtils.extract("$Revision: 5928 $"); |
|---|
| 282 | } |
|---|
| 283 | |
|---|
| 284 | /** |
|---|
| 285 | * Main method for testing this class |
|---|
| 286 | * |
|---|
| 287 | * @param argv the commandline options |
|---|
| 288 | */ |
|---|
| 289 | public static void main(String[] argv){ |
|---|
| 290 | runClassifier(new NBTree(), argv); |
|---|
| 291 | } |
|---|
| 292 | } |
|---|