📄 aprioriitemset.java
字号:
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
/*
* AprioriItemSet.java
* Copyright (C) 2004 Eibe Frank, Stefan Mutter
*
*/
package weka.associations;
import java.io.*;
import java.util.*;
import weka.core.*;
/**
* Class for storing a set of items. Item sets are stored in a lexicographic
* order, which is determined by the header information of the set of instances
* used for generating the set of items. All methods in this class assume that
* item sets are stored in lexicographic order.
* The class provides methods that are used in the Apriori algorithm to construct
* association rules.
*
* @author Eibe Frank (eibe@cs.waikato.ac.nz), Stefan Mutter (mutter@cs.waikato.ac.nz)
* @version $Revision: 1.1 $
*/
public class AprioriItemSet extends ItemSet implements Serializable {
/**
* Constructor
* @param totalTrans the total number of transactions in the data
*/
public AprioriItemSet(int totalTrans) {
super(totalTrans);
}
/**
* Outputs the confidence for a rule.
*
* @param premise the premise of the rule
* @param consequence the consequence of the rule
* @return the confidence on the training data
*/
public static double confidenceForRule(AprioriItemSet premise,
AprioriItemSet consequence) {
return (double)consequence.m_counter/(double)premise.m_counter;
}
/**
* Outputs the lift for a rule. Lift is defined as:<br>
* confidence / prob(consequence)
*
* @param premise the premise of the rule
* @param consequence the consequence of the rule
* @param consequenceCount how many times the consequence occurs independent
* of the premise
* @return the lift on the training data
*/
public double liftForRule(AprioriItemSet premise,
AprioriItemSet consequence,
int consequenceCount) {
double confidence = confidenceForRule(premise, consequence);
return confidence / ((double)consequenceCount /
(double)m_totalTransactions);
}
/**
* Outputs the leverage for a rule. Leverage is defined as: <br>
* prob(premise & consequence) - (prob(premise) * prob(consequence))
*
* @param premise the premise of the rule
* @param consequence the consequence of the rule
* @param premiseCount how many times the premise occurs independent
* of the consequent
* @param consequenceCount how many times the consequence occurs independent
* of the premise
* @return the leverage on the training data
*/
public double leverageForRule(AprioriItemSet premise,
AprioriItemSet consequence,
int premiseCount,
int consequenceCount) {
double coverageForItemSet = (double)consequence.m_counter /
(double)m_totalTransactions;
double expectedCoverageIfIndependent =
((double)premiseCount / (double)m_totalTransactions) *
((double)consequenceCount / (double)m_totalTransactions);
double lev = coverageForItemSet - expectedCoverageIfIndependent;
return lev;
}
/**
* Outputs the conviction for a rule. Conviction is defined as: <br>
* prob(premise) * prob(!consequence) / prob(premise & !consequence)
*
* @param premise the premise of the rule
* @param consequence the consequence of the rule
* @param premiseCount how many times the premise occurs independent
* of the consequent
* @param consequenceCount how many times the consequence occurs independent
* of the premise
* @return the conviction on the training data
*/
public double convictionForRule(AprioriItemSet premise,
AprioriItemSet consequence,
int premiseCount,
int consequenceCount) {
double num =
(double)premiseCount * (double)(m_totalTransactions - consequenceCount) /
(double)m_totalTransactions;
double denom =
((premiseCount - consequence.m_counter)+1);
if (num < 0 || denom < 0) {
System.err.println("*** "+num+" "+denom);
System.err.println("premis count: "+premiseCount+" consequence count "+consequenceCount+" total trans "+m_totalTransactions);
}
return num / denom;
}
/**
* Generates all rules for an item set.
*
* @param minConfidence the minimum confidence the rules have to have
* @param hashtables containing all(!) previously generated
* item sets
* @param numItemsInSet the size of the item set for which the rules
* are to be generated
* @return all the rules with minimum confidence for the given item set
*/
public FastVector[] generateRules(double minConfidence,
FastVector hashtables,
int numItemsInSet) {
FastVector premises = new FastVector(),consequences = new FastVector(),
conf = new FastVector();
FastVector[] rules = new FastVector[3], moreResults;
AprioriItemSet premise, consequence;
Hashtable hashtable = (Hashtable)hashtables.elementAt(numItemsInSet - 2);
// Generate all rules with one item in the consequence.
for (int i = 0; i < m_items.length; i++)
if (m_items[i] != -1) {
premise = new AprioriItemSet(m_totalTransactions);
consequence = new AprioriItemSet(m_totalTransactions);
premise.m_items = new int[m_items.length];
consequence.m_items = new int[m_items.length];
consequence.m_counter = m_counter;
for (int j = 0; j < m_items.length; j++)
consequence.m_items[j] = -1;
System.arraycopy(m_items, 0, premise.m_items, 0, m_items.length);
premise.m_items[i] = -1;
consequence.m_items[i] = m_items[i];
premise.m_counter = ((Integer)hashtable.get(premise)).intValue();
premises.addElement(premise);
consequences.addElement(consequence);
conf.addElement(new Double(confidenceForRule(premise, consequence)));
}
rules[0] = premises;
rules[1] = consequences;
rules[2] = conf;
pruneRules(rules, minConfidence);
// Generate all the other rules
moreResults = moreComplexRules(rules, numItemsInSet, 1, minConfidence,
hashtables);
if (moreResults != null)
for (int i = 0; i < moreResults[0].size(); i++) {
rules[0].addElement(moreResults[0].elementAt(i));
rules[1].addElement(moreResults[1].elementAt(i));
rules[2].addElement(moreResults[2].elementAt(i));
}
return rules;
}
/**
* Generates all significant rules for an item set.
*
* @param minMetric the minimum metric (confidence, lift, leverage,
* improvement) the rules have to have
* @param metricType (confidence=0, lift, leverage, improvement)
* @param hashtables containing all(!) previously generated
* item sets
* @param numItemsInSet the size of the item set for which the rules
* are to be generated
* @param the significance level for testing the rules
* @return all the rules with minimum metric for the given item set
* @exception Exception if something goes wrong
*/
public final FastVector[] generateRulesBruteForce(double minMetric,
int metricType,
FastVector hashtables,
int numItemsInSet,
int numTransactions,
double significanceLevel)
throws Exception {
FastVector premises = new FastVector(),consequences = new FastVector(),
conf = new FastVector(), lift = new FastVector(), lev = new FastVector(),
conv = new FastVector();
FastVector[] rules = new FastVector[6];
AprioriItemSet premise, consequence;
Hashtable hashtableForPremise, hashtableForConsequence;
int numItemsInPremise, help, max, consequenceUnconditionedCounter;
double[][] contingencyTable = new double[2][2];
double metric, chiSquared;
// Generate all possible rules for this item set and test their
// significance.
max = (int)Math.pow(2, numItemsInSet);
for (int j = 1; j < max; j++) {
numItemsInPremise = 0;
help = j;
while (help > 0) {
if (help % 2 == 1)
numItemsInPremise++;
help /= 2;
}
if (numItemsInPremise < numItemsInSet) {
hashtableForPremise =
(Hashtable)hashtables.elementAt(numItemsInPremise-1);
hashtableForConsequence =
(Hashtable)hashtables.elementAt(numItemsInSet-numItemsInPremise-1);
premise = new AprioriItemSet(m_totalTransactions);
consequence = new AprioriItemSet(m_totalTransactions);
premise.m_items = new int[m_items.length];
consequence.m_items = new int[m_items.length];
consequence.m_counter = m_counter;
help = j;
for (int i = 0; i < m_items.length; i++)
if (m_items[i] != -1) {
if (help % 2 == 1) {
premise.m_items[i] = m_items[i];
consequence.m_items[i] = -1;
} else {
premise.m_items[i] = -1;
consequence.m_items[i] = m_items[i];
}
help /= 2;
} else {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -