[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 | * RuleItem.java |
---|
| 19 | * Copyright (C) 2004 University of Waikato, Hamilton, New Zealand |
---|
| 20 | * |
---|
| 21 | */ |
---|
| 22 | |
---|
| 23 | package weka.associations; |
---|
| 24 | |
---|
| 25 | import weka.core.Instances; |
---|
| 26 | import weka.core.RevisionHandler; |
---|
| 27 | import weka.core.RevisionUtils; |
---|
| 28 | |
---|
| 29 | import java.io.Serializable; |
---|
| 30 | import java.util.Hashtable; |
---|
| 31 | |
---|
| 32 | /** |
---|
| 33 | * Class for storing an (class) association rule. |
---|
| 34 | * The premise and the consequence are stored each as separate item sets. |
---|
| 35 | * For every rule their expected predictive accuracy and the time of generation is stored. |
---|
| 36 | * These two measures allow to introduce a sort order for rules. |
---|
| 37 | * |
---|
| 38 | * @author Stefan Mutter |
---|
| 39 | * @version $Revision: 1.5 $ |
---|
| 40 | */ |
---|
| 41 | public class RuleItem |
---|
| 42 | implements Comparable, Serializable, RevisionHandler { |
---|
| 43 | |
---|
| 44 | /** for serialization */ |
---|
| 45 | private static final long serialVersionUID = -3761299128347476534L; |
---|
| 46 | |
---|
| 47 | /** The premise of a rule. */ |
---|
| 48 | protected ItemSet m_premise; |
---|
| 49 | |
---|
| 50 | /** The consequence of a rule. */ |
---|
| 51 | protected ItemSet m_consequence; |
---|
| 52 | |
---|
| 53 | /** The expected predictive accuracy of a rule. */ |
---|
| 54 | protected double m_accuracy; |
---|
| 55 | |
---|
| 56 | /** The generation time of a rule. */ |
---|
| 57 | protected int m_genTime; |
---|
| 58 | |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Constructor for an empty RuleItem |
---|
| 62 | */ |
---|
| 63 | public RuleItem(){ |
---|
| 64 | |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | * Constructor that generates a RuleItem out of a given one |
---|
| 69 | * @param toCopy RuleItem to copy |
---|
| 70 | */ |
---|
| 71 | public RuleItem(RuleItem toCopy){ |
---|
| 72 | |
---|
| 73 | m_premise = toCopy.m_premise; |
---|
| 74 | m_consequence = toCopy.m_consequence; |
---|
| 75 | m_accuracy = toCopy.m_accuracy; |
---|
| 76 | m_genTime = toCopy.m_genTime; |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | * Constructor |
---|
| 81 | * @param premise the premise of the future RuleItem |
---|
| 82 | * @param consequence the consequence of the future RuleItem |
---|
| 83 | * @param genTime the time of generation of the future RuleItem |
---|
| 84 | * @param ruleSupport support of the rule |
---|
| 85 | * @param m_midPoints the mid poitns of the intervals |
---|
| 86 | * @param m_priors Hashtable containing the estimated prior probablilities |
---|
| 87 | */ |
---|
| 88 | public RuleItem(ItemSet premise, ItemSet consequence, int genTime,int ruleSupport,double [] m_midPoints, Hashtable m_priors){ |
---|
| 89 | |
---|
| 90 | |
---|
| 91 | m_premise = premise; |
---|
| 92 | m_consequence = consequence; |
---|
| 93 | m_accuracy = RuleGeneration.expectation((double)ruleSupport,m_premise.m_counter,m_midPoints,m_priors); |
---|
| 94 | //overflow, underflow |
---|
| 95 | if(Double.isNaN(m_accuracy) || m_accuracy < 0){ |
---|
| 96 | m_accuracy = Double.MIN_VALUE; |
---|
| 97 | } |
---|
| 98 | m_consequence.m_counter = ruleSupport; |
---|
| 99 | m_genTime = genTime; |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | /** |
---|
| 103 | * Constructs a new RuleItem if the support of the given rule is above the support threshold. |
---|
| 104 | * @param premise the premise |
---|
| 105 | * @param consequence the consequence |
---|
| 106 | * @param instances the instances |
---|
| 107 | * @param genTime the time of generation of the current premise and consequence |
---|
| 108 | * @param minRuleCount the support threshold |
---|
| 109 | * @param m_midPoints the mid points of the intervals |
---|
| 110 | * @param m_priors the estimated priori probabilities (in a hashtable) |
---|
| 111 | * @return a RuleItem if its support is above the threshold, null otherwise |
---|
| 112 | */ |
---|
| 113 | public RuleItem generateRuleItem(ItemSet premise, ItemSet consequence, Instances instances,int genTime, int minRuleCount,double[] m_midPoints, Hashtable m_priors){ |
---|
| 114 | ItemSet rule = new ItemSet(instances.numInstances()); |
---|
| 115 | rule.m_items = new int[(consequence.m_items).length]; |
---|
| 116 | System.arraycopy(premise.m_items, 0, rule.m_items, 0, (premise.m_items).length); |
---|
| 117 | for(int k = 0;k < consequence.m_items.length; k++){ |
---|
| 118 | if(consequence.m_items[k] != -1) |
---|
| 119 | rule.m_items[k] = consequence.m_items[k]; |
---|
| 120 | } |
---|
| 121 | for (int i = 0; i < instances.numInstances(); i++) |
---|
| 122 | rule.upDateCounter(instances.instance(i)); |
---|
| 123 | int ruleSupport = rule.support(); |
---|
| 124 | if(ruleSupport > minRuleCount){ |
---|
| 125 | RuleItem newRule = new RuleItem(premise,consequence,genTime,ruleSupport,m_midPoints,m_priors); |
---|
| 126 | return newRule; |
---|
| 127 | } |
---|
| 128 | return null; |
---|
| 129 | } |
---|
| 130 | |
---|
| 131 | //Note: this class has a natural ordering that is inconsistent with equals |
---|
| 132 | /** |
---|
| 133 | * compares two RuleItems and allows an ordering concerning |
---|
| 134 | * expected predictive accuracy and time of generation |
---|
| 135 | * Note: this class has a natural ordering that is inconsistent with equals |
---|
| 136 | * @param o RuleItem to compare |
---|
| 137 | * @return integer indicating the sort oder of the two RuleItems |
---|
| 138 | */ |
---|
| 139 | public int compareTo(Object o) { |
---|
| 140 | |
---|
| 141 | if(this.m_accuracy == ((RuleItem)o).m_accuracy){ |
---|
| 142 | if((this.m_genTime == ((RuleItem)o).m_genTime)) |
---|
| 143 | return 0; |
---|
| 144 | if(this.m_genTime > ((RuleItem)o).m_genTime) |
---|
| 145 | return -1; |
---|
| 146 | if(this.m_genTime < ((RuleItem)o).m_genTime) |
---|
| 147 | return 1; |
---|
| 148 | } |
---|
| 149 | if(this.m_accuracy < ((RuleItem)o).m_accuracy) |
---|
| 150 | return -1; |
---|
| 151 | return 1; |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /** |
---|
| 155 | * returns whether two RuleItems are equal |
---|
| 156 | * @param o RuleItem to compare |
---|
| 157 | * @return true if the rules are equal, false otherwise |
---|
| 158 | */ |
---|
| 159 | public boolean equals(Object o){ |
---|
| 160 | |
---|
| 161 | if(o == null) |
---|
| 162 | return false; |
---|
| 163 | if(m_premise.equals(((RuleItem)o).m_premise) && m_consequence.equals(((RuleItem)o).m_consequence)) |
---|
| 164 | return true; |
---|
| 165 | return false; |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | * Gets the expected predictive accuracy of a rule |
---|
| 170 | * @return the expected predictive accuracy of a rule stored as a RuleItem |
---|
| 171 | */ |
---|
| 172 | public double accuracy(){ |
---|
| 173 | |
---|
| 174 | return m_accuracy; |
---|
| 175 | } |
---|
| 176 | |
---|
| 177 | /** |
---|
| 178 | * Gets the premise of a rule |
---|
| 179 | * @return the premise of a rule stored as a RuleItem |
---|
| 180 | */ |
---|
| 181 | public ItemSet premise(){ |
---|
| 182 | |
---|
| 183 | return m_premise; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | /** |
---|
| 187 | * Gets the consequence of a rule |
---|
| 188 | * @return the consequence of a rule stored as a RuleItem |
---|
| 189 | */ |
---|
| 190 | public ItemSet consequence(){ |
---|
| 191 | |
---|
| 192 | return m_consequence; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /** |
---|
| 196 | * Returns the revision string. |
---|
| 197 | * |
---|
| 198 | * @return the revision |
---|
| 199 | */ |
---|
| 200 | public String getRevision() { |
---|
| 201 | return RevisionUtils.extract("$Revision: 1.5 $"); |
---|
| 202 | } |
---|
| 203 | } |
---|