[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 | * PredictiveApriori.java |
---|
| 19 | * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.associations; |
---|
| 24 | |
---|
| 25 | import weka.core.Capabilities; |
---|
| 26 | import weka.core.FastVector; |
---|
| 27 | import weka.core.Instances; |
---|
| 28 | import weka.core.Option; |
---|
| 29 | import weka.core.OptionHandler; |
---|
| 30 | import weka.core.RevisionUtils; |
---|
| 31 | import weka.core.TechnicalInformation; |
---|
| 32 | import weka.core.TechnicalInformationHandler; |
---|
| 33 | import weka.core.Utils; |
---|
| 34 | import weka.core.Capabilities.Capability; |
---|
| 35 | import weka.core.TechnicalInformation.Field; |
---|
| 36 | import weka.core.TechnicalInformation.Type; |
---|
| 37 | |
---|
| 38 | import java.util.Enumeration; |
---|
| 39 | import java.util.Hashtable; |
---|
| 40 | import java.util.TreeSet; |
---|
| 41 | import java.util.Vector; |
---|
| 42 | |
---|
| 43 | /** |
---|
| 44 | <!-- globalinfo-start --> |
---|
| 45 | * Class implementing the predictive apriori algorithm to mine association rules.<br/> |
---|
| 46 | * It searches with an increasing support threshold for the best 'n' rules concerning a support-based corrected confidence value.<br/> |
---|
| 47 | * <br/> |
---|
| 48 | * For more information see:<br/> |
---|
| 49 | * <br/> |
---|
| 50 | * Tobias Scheffer: Finding Association Rules That Trade Support Optimally against Confidence. In: 5th European Conference on Principles of Data Mining and Knowledge Discovery, 424-435, 2001.<br/> |
---|
| 51 | * <br/> |
---|
| 52 | * The implementation follows the paper expect for adding a rule to the output of the 'n' best rules. A rule is added if:<br/> |
---|
| 53 | * the expected predictive accuracy of this rule is among the 'n' best and it is not subsumed by a rule with at least the same expected predictive accuracy (out of an unpublished manuscript from T. Scheffer). |
---|
| 54 | * <p/> |
---|
| 55 | <!-- globalinfo-end --> |
---|
| 56 | * |
---|
| 57 | <!-- technical-bibtex-start --> |
---|
| 58 | * BibTeX: |
---|
| 59 | * <pre> |
---|
| 60 | * @inproceedings{Scheffer2001, |
---|
| 61 | * author = {Tobias Scheffer}, |
---|
| 62 | * booktitle = {5th European Conference on Principles of Data Mining and Knowledge Discovery}, |
---|
| 63 | * pages = {424-435}, |
---|
| 64 | * publisher = {Springer}, |
---|
| 65 | * title = {Finding Association Rules That Trade Support Optimally against Confidence}, |
---|
| 66 | * year = {2001} |
---|
| 67 | * } |
---|
| 68 | * </pre> |
---|
| 69 | * <p/> |
---|
| 70 | <!-- technical-bibtex-end --> |
---|
| 71 | * |
---|
| 72 | <!-- options-start --> |
---|
| 73 | * Valid options are: <p/> |
---|
| 74 | * |
---|
| 75 | * <pre> -N <required number of rules output> |
---|
| 76 | * The required number of rules. (default = 100)</pre> |
---|
| 77 | * |
---|
| 78 | * <pre> -A |
---|
| 79 | * If set class association rules are mined. (default = no)</pre> |
---|
| 80 | * |
---|
| 81 | * <pre> -c <the class index> |
---|
| 82 | * The class index. (default = last)</pre> |
---|
| 83 | * |
---|
| 84 | <!-- options-end --> |
---|
| 85 | * |
---|
| 86 | * @author Stefan Mutter (mutter@cs.waikato.ac.nz) |
---|
| 87 | * @version $Revision: 5444 $ */ |
---|
| 88 | |
---|
| 89 | public class PredictiveApriori |
---|
| 90 | extends AbstractAssociator |
---|
| 91 | implements OptionHandler, CARuleMiner, TechnicalInformationHandler { |
---|
| 92 | |
---|
| 93 | /** for serialization */ |
---|
| 94 | static final long serialVersionUID = 8109088846865075341L; |
---|
| 95 | |
---|
| 96 | /** The minimum support. */ |
---|
| 97 | protected int m_premiseCount; |
---|
| 98 | |
---|
| 99 | /** The maximum number of rules that are output. */ |
---|
| 100 | protected int m_numRules; |
---|
| 101 | |
---|
| 102 | /** The number of rules created for the prior estimation. */ |
---|
| 103 | protected static final int m_numRandRules = 1000; |
---|
| 104 | |
---|
| 105 | /** The number of intervals used for the prior estimation. */ |
---|
| 106 | protected static final int m_numIntervals = 100; |
---|
| 107 | |
---|
| 108 | /** The set of all sets of itemsets. */ |
---|
| 109 | protected FastVector m_Ls; |
---|
| 110 | |
---|
| 111 | /** The same information stored in hash tables. */ |
---|
| 112 | protected FastVector m_hashtables; |
---|
| 113 | |
---|
| 114 | /** The list of all generated rules. */ |
---|
| 115 | protected FastVector[] m_allTheRules; |
---|
| 116 | |
---|
| 117 | /** The instances (transactions) to be used for generating |
---|
| 118 | the association rules. */ |
---|
| 119 | protected Instances m_instances; |
---|
| 120 | |
---|
| 121 | /** The hashtable containing the prior probabilities. */ |
---|
| 122 | protected Hashtable m_priors; |
---|
| 123 | |
---|
| 124 | /** The mid points of the intervals used for the prior estimation. */ |
---|
| 125 | protected double[] m_midPoints; |
---|
| 126 | |
---|
| 127 | /** The expected predictive accuracy a rule needs to be a candidate for the output. */ |
---|
| 128 | protected double m_expectation; |
---|
| 129 | |
---|
| 130 | /** The n best rules. */ |
---|
| 131 | protected TreeSet m_best; |
---|
| 132 | |
---|
| 133 | /** Flag keeping track if the list of the n best rules has changed. */ |
---|
| 134 | protected boolean m_bestChanged; |
---|
| 135 | |
---|
| 136 | /** Counter for the time of generation for an association rule. */ |
---|
| 137 | protected int m_count; |
---|
| 138 | |
---|
| 139 | /** The prior estimator. */ |
---|
| 140 | protected PriorEstimation m_priorEstimator; |
---|
| 141 | |
---|
| 142 | /** The class index. */ |
---|
| 143 | protected int m_classIndex; |
---|
| 144 | |
---|
| 145 | /** Flag indicating whether class association rules are mined. */ |
---|
| 146 | protected boolean m_car; |
---|
| 147 | |
---|
| 148 | /** |
---|
| 149 | * Returns a string describing this associator |
---|
| 150 | * @return a description of the evaluator suitable for |
---|
| 151 | * displaying in the explorer/experimenter gui |
---|
| 152 | */ |
---|
| 153 | public String globalInfo() { |
---|
| 154 | return |
---|
| 155 | "Class implementing the predictive apriori algorithm to mine " |
---|
| 156 | + "association rules.\n" |
---|
| 157 | + "It searches with an increasing support threshold for the best 'n' " |
---|
| 158 | + "rules concerning a support-based corrected confidence value.\n\n" |
---|
| 159 | + "For more information see:\n\n" |
---|
| 160 | + getTechnicalInformation().toString() + "\n\n" |
---|
| 161 | + "The implementation follows the paper expect for adding a rule to the " |
---|
| 162 | + "output of the 'n' best rules. A rule is added if:\n" |
---|
| 163 | + "the expected predictive accuracy of this rule is among the 'n' best " |
---|
| 164 | + "and it is not subsumed by a rule with at least the same expected " |
---|
| 165 | + "predictive accuracy (out of an unpublished manuscript from T. " |
---|
| 166 | + "Scheffer)."; |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | /** |
---|
| 170 | * Returns an instance of a TechnicalInformation object, containing |
---|
| 171 | * detailed information about the technical background of this class, |
---|
| 172 | * e.g., paper reference or book this class is based on. |
---|
| 173 | * |
---|
| 174 | * @return the technical information about this class |
---|
| 175 | */ |
---|
| 176 | public TechnicalInformation getTechnicalInformation() { |
---|
| 177 | TechnicalInformation result; |
---|
| 178 | |
---|
| 179 | result = new TechnicalInformation(Type.INPROCEEDINGS); |
---|
| 180 | result.setValue(Field.AUTHOR, "Tobias Scheffer"); |
---|
| 181 | result.setValue(Field.TITLE, "Finding Association Rules That Trade Support Optimally against Confidence"); |
---|
| 182 | result.setValue(Field.BOOKTITLE, "5th European Conference on Principles of Data Mining and Knowledge Discovery"); |
---|
| 183 | result.setValue(Field.YEAR, "2001"); |
---|
| 184 | result.setValue(Field.PAGES, "424-435"); |
---|
| 185 | result.setValue(Field.PUBLISHER, "Springer"); |
---|
| 186 | |
---|
| 187 | return result; |
---|
| 188 | } |
---|
| 189 | |
---|
| 190 | /** |
---|
| 191 | * Constructor that allows to sets default values for the |
---|
| 192 | * minimum confidence and the maximum number of rules |
---|
| 193 | * the minimum confidence. |
---|
| 194 | */ |
---|
| 195 | public PredictiveApriori() { |
---|
| 196 | |
---|
| 197 | resetOptions(); |
---|
| 198 | } |
---|
| 199 | |
---|
| 200 | /** |
---|
| 201 | * Resets the options to the default values. |
---|
| 202 | */ |
---|
| 203 | public void resetOptions() { |
---|
| 204 | |
---|
| 205 | m_numRules = 105; |
---|
| 206 | m_premiseCount = 1; |
---|
| 207 | m_best = new TreeSet(); |
---|
| 208 | m_bestChanged = false; |
---|
| 209 | m_expectation = 0; |
---|
| 210 | m_count = 1; |
---|
| 211 | m_car = false; |
---|
| 212 | m_classIndex = -1; |
---|
| 213 | m_priors = new Hashtable(); |
---|
| 214 | |
---|
| 215 | |
---|
| 216 | |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | /** |
---|
| 220 | * Returns default capabilities of the classifier. |
---|
| 221 | * |
---|
| 222 | * @return the capabilities of this classifier |
---|
| 223 | */ |
---|
| 224 | public Capabilities getCapabilities() { |
---|
| 225 | Capabilities result = super.getCapabilities(); |
---|
| 226 | result.disableAll(); |
---|
| 227 | |
---|
| 228 | // attributes |
---|
| 229 | result.enable(Capability.NOMINAL_ATTRIBUTES); |
---|
| 230 | result.enable(Capability.MISSING_VALUES); |
---|
| 231 | |
---|
| 232 | // class |
---|
| 233 | result.enable(Capability.NOMINAL_CLASS); |
---|
| 234 | result.enable(Capability.MISSING_CLASS_VALUES); |
---|
| 235 | |
---|
| 236 | return result; |
---|
| 237 | } |
---|
| 238 | |
---|
| 239 | /** |
---|
| 240 | * Method that generates all large itemsets with a minimum support, and from |
---|
| 241 | * these all association rules. |
---|
| 242 | * |
---|
| 243 | * @param instances the instances to be used for generating the associations |
---|
| 244 | * @throws Exception if rules can't be built successfully |
---|
| 245 | */ |
---|
| 246 | public void buildAssociations(Instances instances) throws Exception { |
---|
| 247 | |
---|
| 248 | int temp = m_premiseCount, exactNumber = m_numRules-5; |
---|
| 249 | |
---|
| 250 | m_premiseCount = 1; |
---|
| 251 | m_best = new TreeSet(); |
---|
| 252 | m_bestChanged = false; |
---|
| 253 | m_expectation = 0; |
---|
| 254 | m_count = 1; |
---|
| 255 | m_instances = new Instances(instances); |
---|
| 256 | |
---|
| 257 | if (m_classIndex == -1) |
---|
| 258 | m_instances.setClassIndex(m_instances.numAttributes()-1); |
---|
| 259 | else if (m_classIndex < m_instances.numAttributes() && m_classIndex >= 0) |
---|
| 260 | m_instances.setClassIndex(m_classIndex); |
---|
| 261 | else |
---|
| 262 | throw new Exception("Invalid class index."); |
---|
| 263 | |
---|
| 264 | // can associator handle the data? |
---|
| 265 | getCapabilities().testWithFail(m_instances); |
---|
| 266 | |
---|
| 267 | //prior estimation |
---|
| 268 | m_priorEstimator = new PriorEstimation(m_instances,m_numRandRules,m_numIntervals,m_car); |
---|
| 269 | m_priors = m_priorEstimator.estimatePrior(); |
---|
| 270 | m_midPoints = m_priorEstimator.getMidPoints(); |
---|
| 271 | |
---|
| 272 | m_Ls = new FastVector(); |
---|
| 273 | m_hashtables = new FastVector(); |
---|
| 274 | |
---|
| 275 | for(int i =1; i < m_instances.numAttributes();i++){ |
---|
| 276 | m_bestChanged = false; |
---|
| 277 | if(!m_car){ |
---|
| 278 | // find large item sets |
---|
| 279 | findLargeItemSets(i); |
---|
| 280 | |
---|
| 281 | //find association rules (rule generation procedure) |
---|
| 282 | findRulesQuickly(); |
---|
| 283 | } |
---|
| 284 | else{ |
---|
| 285 | findLargeCarItemSets(i); |
---|
| 286 | findCaRulesQuickly(); |
---|
| 287 | } |
---|
| 288 | |
---|
| 289 | if(m_bestChanged){ |
---|
| 290 | temp =m_premiseCount; |
---|
| 291 | while(RuleGeneration.expectation(m_premiseCount, m_premiseCount,m_midPoints,m_priors) <= m_expectation){ |
---|
| 292 | m_premiseCount++; |
---|
| 293 | if(m_premiseCount > m_instances.numInstances()) |
---|
| 294 | break; |
---|
| 295 | } |
---|
| 296 | } |
---|
| 297 | if(m_premiseCount > m_instances.numInstances()){ |
---|
| 298 | |
---|
| 299 | // Reserve space for variables |
---|
| 300 | m_allTheRules = new FastVector[3]; |
---|
| 301 | m_allTheRules[0] = new FastVector(); |
---|
| 302 | m_allTheRules[1] = new FastVector(); |
---|
| 303 | m_allTheRules[2] = new FastVector(); |
---|
| 304 | |
---|
| 305 | int k = 0; |
---|
| 306 | while(m_best.size()>0 && exactNumber > 0){ |
---|
| 307 | m_allTheRules[0].insertElementAt((ItemSet)((RuleItem)m_best.last()).premise(),k); |
---|
| 308 | m_allTheRules[1].insertElementAt((ItemSet)((RuleItem)m_best.last()).consequence(),k); |
---|
| 309 | m_allTheRules[2].insertElementAt(new Double(((RuleItem)m_best.last()).accuracy()),k); |
---|
| 310 | m_best.remove(m_best.last()); |
---|
| 311 | k++; |
---|
| 312 | exactNumber--; |
---|
| 313 | } |
---|
| 314 | return; |
---|
| 315 | } |
---|
| 316 | |
---|
| 317 | if(temp != m_premiseCount && m_Ls.size() > 0){ |
---|
| 318 | FastVector kSets = (FastVector)m_Ls.lastElement(); |
---|
| 319 | m_Ls.removeElementAt(m_Ls.size()-1); |
---|
| 320 | kSets = ItemSet.deleteItemSets(kSets, m_premiseCount,Integer.MAX_VALUE); |
---|
| 321 | m_Ls.addElement(kSets); |
---|
| 322 | } |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | // Reserve space for variables |
---|
| 326 | m_allTheRules = new FastVector[3]; |
---|
| 327 | m_allTheRules[0] = new FastVector(); |
---|
| 328 | m_allTheRules[1] = new FastVector(); |
---|
| 329 | m_allTheRules[2] = new FastVector(); |
---|
| 330 | |
---|
| 331 | int k = 0; |
---|
| 332 | while(m_best.size()>0 && exactNumber > 0){ |
---|
| 333 | m_allTheRules[0].insertElementAt((ItemSet)((RuleItem)m_best.last()).premise(),k); |
---|
| 334 | m_allTheRules[1].insertElementAt((ItemSet)((RuleItem)m_best.last()).consequence(),k); |
---|
| 335 | m_allTheRules[2].insertElementAt(new Double(((RuleItem)m_best.last()).accuracy()),k); |
---|
| 336 | m_best.remove(m_best.last()); |
---|
| 337 | k++; |
---|
| 338 | exactNumber--; |
---|
| 339 | } |
---|
| 340 | } |
---|
| 341 | |
---|
| 342 | /** |
---|
| 343 | * Method that mines the n best class association rules. |
---|
| 344 | * @return an sorted array of FastVector (depending on the expected predictive accuracy) containing the rules and metric information |
---|
| 345 | * @param data the instances for which class association rules should be mined |
---|
| 346 | * @throws Exception if rules can't be built successfully |
---|
| 347 | */ |
---|
| 348 | public FastVector[] mineCARs(Instances data) throws Exception{ |
---|
| 349 | |
---|
| 350 | m_car = true; |
---|
| 351 | m_best = new TreeSet(); |
---|
| 352 | m_premiseCount = 1; |
---|
| 353 | m_bestChanged = false; |
---|
| 354 | m_expectation = 0; |
---|
| 355 | m_count = 1; |
---|
| 356 | buildAssociations(data); |
---|
| 357 | FastVector[] allCARRules = new FastVector[3]; |
---|
| 358 | allCARRules[0] = new FastVector(); |
---|
| 359 | allCARRules[1] = new FastVector(); |
---|
| 360 | allCARRules[2] = new FastVector(); |
---|
| 361 | for(int k =0; k < m_allTheRules[0].size();k++){ |
---|
| 362 | int[] newPremiseArray = new int[m_instances.numAttributes()-1]; |
---|
| 363 | int help = 0; |
---|
| 364 | for(int j = 0;j < m_instances.numAttributes();j++){ |
---|
| 365 | if(j != m_instances.classIndex()){ |
---|
| 366 | newPremiseArray[help] = ((ItemSet)m_allTheRules[0].elementAt(k)).itemAt(j); |
---|
| 367 | help++; |
---|
| 368 | } |
---|
| 369 | } |
---|
| 370 | ItemSet newPremise = new ItemSet(m_instances.numInstances(), newPremiseArray); |
---|
| 371 | newPremise.setCounter (((ItemSet)m_allTheRules[0].elementAt(k)).counter()); |
---|
| 372 | allCARRules[0].addElement(newPremise); |
---|
| 373 | int[] newConsArray = new int[1]; |
---|
| 374 | newConsArray[0] =((ItemSet)m_allTheRules[1].elementAt(k)).itemAt(m_instances.classIndex()); |
---|
| 375 | ItemSet newCons = new ItemSet(m_instances.numInstances(), newConsArray); |
---|
| 376 | newCons.setCounter(((ItemSet)m_allTheRules[1].elementAt(k)).counter()); |
---|
| 377 | allCARRules[1].addElement(newCons); |
---|
| 378 | allCARRules[2].addElement(m_allTheRules[2].elementAt(k)); |
---|
| 379 | } |
---|
| 380 | |
---|
| 381 | return allCARRules; |
---|
| 382 | } |
---|
| 383 | |
---|
| 384 | /** |
---|
| 385 | * Gets the instances without the class attribute |
---|
| 386 | * @return instances without class attribute |
---|
| 387 | */ |
---|
| 388 | public Instances getInstancesNoClass() { |
---|
| 389 | |
---|
| 390 | Instances noClass = null; |
---|
| 391 | try{ |
---|
| 392 | noClass = LabeledItemSet.divide(m_instances,false); |
---|
| 393 | } |
---|
| 394 | catch(Exception e){ |
---|
| 395 | e.printStackTrace(); |
---|
| 396 | System.out.println("\n"+e.getMessage()); |
---|
| 397 | } |
---|
| 398 | //System.out.println(noClass); |
---|
| 399 | return noClass; |
---|
| 400 | } |
---|
| 401 | |
---|
| 402 | /** |
---|
| 403 | * Gets the class attribute of all instances |
---|
| 404 | * @return Instances containing only the class attribute |
---|
| 405 | */ |
---|
| 406 | public Instances getInstancesOnlyClass() { |
---|
| 407 | |
---|
| 408 | Instances onlyClass = null; |
---|
| 409 | try{ |
---|
| 410 | onlyClass = LabeledItemSet.divide(m_instances,true); |
---|
| 411 | } |
---|
| 412 | catch(Exception e){ |
---|
| 413 | e.printStackTrace(); |
---|
| 414 | System.out.println("\n"+e.getMessage()); |
---|
| 415 | } |
---|
| 416 | return onlyClass; |
---|
| 417 | |
---|
| 418 | } |
---|
| 419 | |
---|
| 420 | /** |
---|
| 421 | * Returns an enumeration describing the available options. |
---|
| 422 | * |
---|
| 423 | * @return an enumeration of all the available options. |
---|
| 424 | */ |
---|
| 425 | public Enumeration listOptions() { |
---|
| 426 | |
---|
| 427 | String string1 = "\tThe required number of rules. (default = " + (m_numRules-5) + ")", |
---|
| 428 | string2 = "\tIf set class association rules are mined. (default = no)", |
---|
| 429 | string3 = "\tThe class index. (default = last)"; |
---|
| 430 | FastVector newVector = new FastVector(3); |
---|
| 431 | |
---|
| 432 | newVector.addElement(new Option(string1, "N", 1, |
---|
| 433 | "-N <required number of rules output>")); |
---|
| 434 | newVector.addElement(new Option(string2, "A", 0, |
---|
| 435 | "-A")); |
---|
| 436 | newVector.addElement(new Option(string3, "c", 1, |
---|
| 437 | "-c <the class index>")); |
---|
| 438 | return newVector.elements(); |
---|
| 439 | } |
---|
| 440 | |
---|
| 441 | |
---|
| 442 | /** |
---|
| 443 | * Parses a given list of options. <p/> |
---|
| 444 | * |
---|
| 445 | <!-- options-start --> |
---|
| 446 | * Valid options are: <p/> |
---|
| 447 | * |
---|
| 448 | * <pre> -N <required number of rules output> |
---|
| 449 | * The required number of rules. (default = 100)</pre> |
---|
| 450 | * |
---|
| 451 | * <pre> -A |
---|
| 452 | * If set class association rules are mined. (default = no)</pre> |
---|
| 453 | * |
---|
| 454 | * <pre> -c <the class index> |
---|
| 455 | * The class index. (default = last)</pre> |
---|
| 456 | * |
---|
| 457 | <!-- options-end --> |
---|
| 458 | * |
---|
| 459 | * @param options the list of options as an array of strings |
---|
| 460 | * @throws Exception if an option is not supported |
---|
| 461 | */ |
---|
| 462 | public void setOptions(String[] options) throws Exception { |
---|
| 463 | |
---|
| 464 | resetOptions(); |
---|
| 465 | |
---|
| 466 | String numRulesString = Utils.getOption('N', options); |
---|
| 467 | if (numRulesString.length() != 0) |
---|
| 468 | m_numRules = Integer.parseInt(numRulesString)+5; |
---|
| 469 | else |
---|
| 470 | m_numRules = Integer.MAX_VALUE; |
---|
| 471 | |
---|
| 472 | String classIndexString = Utils.getOption('c',options); |
---|
| 473 | if (classIndexString.length() != 0) |
---|
| 474 | m_classIndex = Integer.parseInt(classIndexString); |
---|
| 475 | |
---|
| 476 | m_car = Utils.getFlag('A', options); |
---|
| 477 | } |
---|
| 478 | |
---|
| 479 | /** |
---|
| 480 | * Gets the current settings of the PredictiveApriori object. |
---|
| 481 | * |
---|
| 482 | * @return an array of strings suitable for passing to setOptions |
---|
| 483 | */ |
---|
| 484 | public String [] getOptions() { |
---|
| 485 | Vector result; |
---|
| 486 | |
---|
| 487 | result = new Vector(); |
---|
| 488 | |
---|
| 489 | result.add("-N"); |
---|
| 490 | result.add("" + (m_numRules-5)); |
---|
| 491 | |
---|
| 492 | if (m_car) |
---|
| 493 | result.add("-A"); |
---|
| 494 | |
---|
| 495 | result.add("-c"); |
---|
| 496 | result.add("" + m_classIndex); |
---|
| 497 | |
---|
| 498 | return (String[]) result.toArray(new String[result.size()]); |
---|
| 499 | } |
---|
| 500 | |
---|
| 501 | |
---|
| 502 | /** |
---|
| 503 | * Outputs the association rules. |
---|
| 504 | * |
---|
| 505 | * @return a string representation of the model |
---|
| 506 | */ |
---|
| 507 | public String toString() { |
---|
| 508 | |
---|
| 509 | StringBuffer text = new StringBuffer(); |
---|
| 510 | |
---|
| 511 | if (m_allTheRules[0].size() == 0) |
---|
| 512 | return "\nNo large itemsets and rules found!\n"; |
---|
| 513 | text.append("\nPredictiveApriori\n===================\n\n"); |
---|
| 514 | text.append("\nBest rules found:\n\n"); |
---|
| 515 | |
---|
| 516 | for (int i = 0; i < m_allTheRules[0].size(); i++) { |
---|
| 517 | text.append(Utils.doubleToString((double)i+1, |
---|
| 518 | (int)(Math.log(m_numRules)/Math.log(10)+1),0)+ |
---|
| 519 | ". " + ((ItemSet)m_allTheRules[0].elementAt(i)). |
---|
| 520 | toString(m_instances) |
---|
| 521 | + " ==> " + ((ItemSet)m_allTheRules[1].elementAt(i)). |
---|
| 522 | toString(m_instances) +" acc:("+ |
---|
| 523 | Utils.doubleToString(((Double)m_allTheRules[2]. |
---|
| 524 | elementAt(i)).doubleValue(),5)+")"); |
---|
| 525 | |
---|
| 526 | text.append('\n'); |
---|
| 527 | } |
---|
| 528 | |
---|
| 529 | |
---|
| 530 | return text.toString(); |
---|
| 531 | } |
---|
| 532 | |
---|
| 533 | |
---|
| 534 | /** |
---|
| 535 | * Returns the tip text for this property |
---|
| 536 | * @return tip text for this property suitable for |
---|
| 537 | * displaying in the explorer/experimenter gui |
---|
| 538 | */ |
---|
| 539 | public String numRulesTipText() { |
---|
| 540 | return "Number of rules to find."; |
---|
| 541 | } |
---|
| 542 | |
---|
| 543 | /** |
---|
| 544 | * Get the value of the number of required rules. |
---|
| 545 | * |
---|
| 546 | * @return Value of the number of required rules. |
---|
| 547 | */ |
---|
| 548 | public int getNumRules() { |
---|
| 549 | |
---|
| 550 | return m_numRules-5; |
---|
| 551 | } |
---|
| 552 | |
---|
| 553 | /** |
---|
| 554 | * Set the value of required rules. |
---|
| 555 | * |
---|
| 556 | * @param v Value to assign to number of required rules. |
---|
| 557 | */ |
---|
| 558 | public void setNumRules(int v) { |
---|
| 559 | |
---|
| 560 | m_numRules = v+5; |
---|
| 561 | } |
---|
| 562 | |
---|
| 563 | /** |
---|
| 564 | * Sets the class index |
---|
| 565 | * @param index the index of the class attribute |
---|
| 566 | */ |
---|
| 567 | public void setClassIndex(int index){ |
---|
| 568 | |
---|
| 569 | m_classIndex = index; |
---|
| 570 | } |
---|
| 571 | |
---|
| 572 | /** |
---|
| 573 | * Gets the index of the class attribute |
---|
| 574 | * @return the index of the class attribute |
---|
| 575 | */ |
---|
| 576 | public int getClassIndex(){ |
---|
| 577 | |
---|
| 578 | return m_classIndex; |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | /** |
---|
| 582 | * Returns the tip text for this property |
---|
| 583 | * @return tip text for this property suitable for |
---|
| 584 | * displaying in the explorer/experimenter gui |
---|
| 585 | */ |
---|
| 586 | public String classIndexTipText() { |
---|
| 587 | return "Index of the class attribute.\n If set to -1, the last attribute will be taken as the class attribute."; |
---|
| 588 | } |
---|
| 589 | |
---|
| 590 | /** |
---|
| 591 | * Sets class association rule mining |
---|
| 592 | * @param flag if class association rules are mined, false otherwise |
---|
| 593 | */ |
---|
| 594 | public void setCar(boolean flag){ |
---|
| 595 | |
---|
| 596 | m_car = flag; |
---|
| 597 | } |
---|
| 598 | |
---|
| 599 | /** |
---|
| 600 | * Gets whether class association ruels are mined |
---|
| 601 | * @return true if class association rules are mined, false otherwise |
---|
| 602 | */ |
---|
| 603 | public boolean getCar(){ |
---|
| 604 | |
---|
| 605 | return m_car; |
---|
| 606 | } |
---|
| 607 | |
---|
| 608 | /** |
---|
| 609 | * Returns the tip text for this property |
---|
| 610 | * @return tip text for this property suitable for |
---|
| 611 | * displaying in the explorer/experimenter gui |
---|
| 612 | */ |
---|
| 613 | public String carTipText() { |
---|
| 614 | return "If enabled class association rules are mined instead of (general) association rules."; |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | /** |
---|
| 618 | * Returns the metric string for the chosen metric type. |
---|
| 619 | * Predictive apriori uses the estimated predictive accuracy. |
---|
| 620 | * Therefore the metric string is "acc". |
---|
| 621 | * @return string "acc" |
---|
| 622 | */ |
---|
| 623 | public String metricString() { |
---|
| 624 | |
---|
| 625 | return "acc"; |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | |
---|
| 629 | /** |
---|
| 630 | * Method that finds all large itemsets for the given set of instances. |
---|
| 631 | * |
---|
| 632 | * @param index the instances to be used |
---|
| 633 | * @throws Exception if an attribute is numeric |
---|
| 634 | */ |
---|
| 635 | private void findLargeItemSets(int index) throws Exception { |
---|
| 636 | |
---|
| 637 | FastVector kMinusOneSets, kSets = new FastVector(); |
---|
| 638 | Hashtable hashtable; |
---|
| 639 | int i = 0; |
---|
| 640 | // Find large itemsets |
---|
| 641 | //of length 1 |
---|
| 642 | if(index == 1){ |
---|
| 643 | kSets = ItemSet.singletons(m_instances); |
---|
| 644 | ItemSet.upDateCounters(kSets, m_instances); |
---|
| 645 | kSets = ItemSet.deleteItemSets(kSets, m_premiseCount,Integer.MAX_VALUE); |
---|
| 646 | if (kSets.size() == 0) |
---|
| 647 | return; |
---|
| 648 | m_Ls.addElement(kSets); |
---|
| 649 | } |
---|
| 650 | //of length > 1 |
---|
| 651 | if(index >1){ |
---|
| 652 | if(m_Ls.size() > 0) |
---|
| 653 | kSets = (FastVector)m_Ls.lastElement(); |
---|
| 654 | m_Ls.removeAllElements(); |
---|
| 655 | i = index-2; |
---|
| 656 | kMinusOneSets = kSets; |
---|
| 657 | kSets = ItemSet.mergeAllItemSets(kMinusOneSets, i, m_instances.numInstances()); |
---|
| 658 | hashtable = ItemSet.getHashtable(kMinusOneSets, kMinusOneSets.size()); |
---|
| 659 | m_hashtables.addElement(hashtable); |
---|
| 660 | kSets = ItemSet.pruneItemSets(kSets, hashtable); |
---|
| 661 | ItemSet.upDateCounters(kSets, m_instances); |
---|
| 662 | kSets = ItemSet.deleteItemSets(kSets, m_premiseCount,Integer.MAX_VALUE); |
---|
| 663 | if(kSets.size() == 0) |
---|
| 664 | return; |
---|
| 665 | m_Ls.addElement(kSets); |
---|
| 666 | } |
---|
| 667 | } |
---|
| 668 | |
---|
| 669 | |
---|
| 670 | |
---|
| 671 | |
---|
| 672 | /** |
---|
| 673 | * Method that finds all association rules. |
---|
| 674 | * |
---|
| 675 | * @throws Exception if an attribute is numeric |
---|
| 676 | */ |
---|
| 677 | private void findRulesQuickly() throws Exception { |
---|
| 678 | |
---|
| 679 | RuleGeneration currentItemSet; |
---|
| 680 | |
---|
| 681 | // Build rules |
---|
| 682 | for (int j = 0; j < m_Ls.size(); j++) { |
---|
| 683 | FastVector currentItemSets = (FastVector)m_Ls.elementAt(j); |
---|
| 684 | Enumeration enumItemSets = currentItemSets.elements(); |
---|
| 685 | while (enumItemSets.hasMoreElements()) { |
---|
| 686 | currentItemSet = new RuleGeneration((ItemSet)enumItemSets.nextElement()); |
---|
| 687 | m_best = currentItemSet.generateRules(m_numRules-5, m_midPoints,m_priors,m_expectation, |
---|
| 688 | m_instances,m_best,m_count); |
---|
| 689 | |
---|
| 690 | m_count = currentItemSet.m_count; |
---|
| 691 | if(!m_bestChanged && currentItemSet.m_change) |
---|
| 692 | m_bestChanged = true; |
---|
| 693 | //update minimum expected predictive accuracy to get into the n best |
---|
| 694 | if(m_best.size() >= m_numRules-5) |
---|
| 695 | m_expectation = ((RuleItem)m_best.first()).accuracy(); |
---|
| 696 | else m_expectation =0; |
---|
| 697 | } |
---|
| 698 | } |
---|
| 699 | } |
---|
| 700 | |
---|
| 701 | |
---|
| 702 | /** |
---|
| 703 | * Method that finds all large itemsets for class association rule mining for the given set of instances. |
---|
| 704 | * @param index the size of the large item sets |
---|
| 705 | * @throws Exception if an attribute is numeric |
---|
| 706 | */ |
---|
| 707 | private void findLargeCarItemSets(int index) throws Exception { |
---|
| 708 | |
---|
| 709 | FastVector kMinusOneSets, kSets = new FastVector(); |
---|
| 710 | Hashtable hashtable; |
---|
| 711 | int i = 0; |
---|
| 712 | // Find large itemsets |
---|
| 713 | if(index == 1){ |
---|
| 714 | kSets = CaRuleGeneration.singletons(m_instances); |
---|
| 715 | ItemSet.upDateCounters(kSets, m_instances); |
---|
| 716 | kSets = ItemSet.deleteItemSets(kSets, m_premiseCount,Integer.MAX_VALUE); |
---|
| 717 | if (kSets.size() == 0) |
---|
| 718 | return; |
---|
| 719 | m_Ls.addElement(kSets); |
---|
| 720 | } |
---|
| 721 | |
---|
| 722 | if(index >1){ |
---|
| 723 | if(m_Ls.size() > 0) |
---|
| 724 | kSets = (FastVector)m_Ls.lastElement(); |
---|
| 725 | m_Ls.removeAllElements(); |
---|
| 726 | i = index-2; |
---|
| 727 | kMinusOneSets = kSets; |
---|
| 728 | kSets = ItemSet.mergeAllItemSets(kMinusOneSets, i, m_instances.numInstances()); |
---|
| 729 | hashtable = ItemSet.getHashtable(kMinusOneSets, kMinusOneSets.size()); |
---|
| 730 | m_hashtables.addElement(hashtable); |
---|
| 731 | kSets = ItemSet.pruneItemSets(kSets, hashtable); |
---|
| 732 | ItemSet.upDateCounters(kSets, m_instances); |
---|
| 733 | kSets = ItemSet.deleteItemSets(kSets, m_premiseCount,Integer.MAX_VALUE); |
---|
| 734 | if(kSets.size() == 0) |
---|
| 735 | return; |
---|
| 736 | m_Ls.addElement(kSets); |
---|
| 737 | } |
---|
| 738 | } |
---|
| 739 | |
---|
| 740 | /** |
---|
| 741 | * Method that finds all class association rules. |
---|
| 742 | * |
---|
| 743 | * @throws Exception if an attribute is numeric |
---|
| 744 | */ |
---|
| 745 | private void findCaRulesQuickly() throws Exception { |
---|
| 746 | |
---|
| 747 | CaRuleGeneration currentLItemSet; |
---|
| 748 | // Build rules |
---|
| 749 | for (int j = 0; j < m_Ls.size(); j++) { |
---|
| 750 | FastVector currentItemSets = (FastVector)m_Ls.elementAt(j); |
---|
| 751 | Enumeration enumItemSets = currentItemSets.elements(); |
---|
| 752 | while (enumItemSets.hasMoreElements()) { |
---|
| 753 | currentLItemSet = new CaRuleGeneration((ItemSet)enumItemSets.nextElement()); |
---|
| 754 | m_best = currentLItemSet.generateRules(m_numRules-5, m_midPoints,m_priors,m_expectation, |
---|
| 755 | m_instances,m_best,m_count); |
---|
| 756 | m_count = currentLItemSet.count(); |
---|
| 757 | if(!m_bestChanged && currentLItemSet.change()) |
---|
| 758 | m_bestChanged = true; |
---|
| 759 | if(m_best.size() == m_numRules-5) |
---|
| 760 | m_expectation = ((RuleItem)m_best.first()).accuracy(); |
---|
| 761 | else |
---|
| 762 | m_expectation = 0; |
---|
| 763 | } |
---|
| 764 | } |
---|
| 765 | } |
---|
| 766 | |
---|
| 767 | /** |
---|
| 768 | * returns all the rules |
---|
| 769 | * |
---|
| 770 | * @return all the rules |
---|
| 771 | * @see #m_allTheRules |
---|
| 772 | */ |
---|
| 773 | public FastVector[] getAllTheRules() { |
---|
| 774 | return m_allTheRules; |
---|
| 775 | } |
---|
| 776 | |
---|
| 777 | /** |
---|
| 778 | * Returns the revision string. |
---|
| 779 | * |
---|
| 780 | * @return the revision |
---|
| 781 | */ |
---|
| 782 | public String getRevision() { |
---|
| 783 | return RevisionUtils.extract("$Revision: 5444 $"); |
---|
| 784 | } |
---|
| 785 | |
---|
| 786 | /** |
---|
| 787 | * Main method. |
---|
| 788 | * |
---|
| 789 | * @param args the commandline parameters |
---|
| 790 | */ |
---|
| 791 | public static void main(String[] args) { |
---|
| 792 | runAssociator(new PredictiveApriori(), args); |
---|
| 793 | } |
---|
| 794 | } |
---|
| 795 | |
---|