/* Returns C(1), the candidate item sets with size 1 */ public ItemSetTable FindFirstCandidateItemSetTable() { ItemSetTable candidateItemSets = new ItemSetTable(); candidateItemSets.order = 1; SortedSet <int> set = new SortedSet <int>(); foreach (Transaction t in this) { foreach (int item in t) { set.Add(item); } } foreach (int item in set) { ItemSet itemSet = new ItemSet(); itemSet.Add(item); candidateItemSets.Add(itemSet, 0); } IncrementCandidatesIn(candidateItemSets); return(candidateItemSets); }
/* Returns L(k+1), candidates in C(k+1) with min_support */ public ItemSetTable BuildFrequentItemSet() { int count = 0; ItemSetTable frequentTable = new ItemSetTable(this.order); ICollection <ItemSet> keys = new List <ItemSet>(this.Keys); foreach (ItemSet itemSet in keys) { int support = this[itemSet]; if (support >= minSupport) { frequentTable.Add(itemSet, support); ++count; } } // If no item set has higher support than min_support, return null. if (count == 0) { return(null); } else { return(frequentTable); } }
/* Print all item sets in input item set table */ public static void Print(ItemSetTable ist) { if (ist != null) { Console.WriteLine("Order: {0}", ist.order); Console.WriteLine(ist.ToString()); } else { Console.WriteLine("don't exist"); } }
/* Each candidates in input candidate item set table is incre * */ public void IncrementCandidatesIn(ItemSetTable candidateTable) { foreach (Transaction t in this) { ICollection <ItemSet> keys = new List <ItemSet>(candidateTable.Keys); foreach (ItemSet itemSet in keys) { if (t.IsSupersetOf(itemSet) == true) { candidateTable.Increment(itemSet); } } } }
/* Returns the candidates C(k+1) generated from L(k) */ public ItemSetTable BuildCandidateItemSet() { ItemSetTable candidateTable = new ItemSetTable(this.order + 1); foreach (ItemSet I1 in this.Keys) // this = L(k) { foreach (ItemSet I2 in this.Keys) { ItemSet c = null; IList <int> iList1 = new List <int>(I1); IList <int> iList2 = new List <int>(I2); bool joinCondition = false; for (int i = 0; i < this.order; ++i) { // last element if (i == this.order - 1) { if (iList1[i] < iList2[i]) { joinCondition = true; } } else { if (iList1[i] != iList2[i]) { joinCondition = false; break; } } } if (joinCondition == true) { c = ItemSet.UnionBetween(I1, I2); // join step: generate candidates if (hasInfrequentSubSet(c) == false) { candidateTable.Add(c, 0); } } } } return(candidateTable); }
public static void Main(string[] args) { Console.WriteLine(DateTime.Now.ToString("O")); string inputFile = null; string outputFile = null; int minSupPercent = 0; int minSupport = 0; if (args.Length != 3) { PrintUsage(); } try { minSupPercent = Convert.ToInt32(args[0]); inputFile = String.Copy(args[1]); outputFile = String.Copy(args[2]); // If input file don't exist, throw exception. if (File.Exists(inputFile) == false) { throw new FileNotFoundException(); } // If output file already exists, the file will be deleted. if (File.Exists(outputFile) == true) { File.Delete(outputFile); } } catch (Exception e) { Console.WriteLine(e); PrintUsage(); } TransactionDatabase database = new TransactionDatabase(inputFile, outputFile); ItemSetTable candidateTable = new ItemSetTable(); ItemSetTable frequentTable = null; /* Printing input arguments.. */ minSupport = minSupPercent * database.Count / 100; Console.WriteLine("minimum support: {0} %", minSupPercent); Console.WriteLine("minimum support: {0}", minSupport); ItemSetTable.SetMinSupport(minSupport); /* Build C(1), the candidate item sets with size 1 */ candidateTable = database.FindFirstCandidateItemSetTable(); /* Build L(1), the frequent item sets with size 1 */ if (candidateTable != null) { frequentTable = candidateTable.BuildFrequentItemSet(); } for (int k = 1; frequentTable != null; ++k) { // C(k+1) = candidates generated from L(k) candidateTable = frequentTable.BuildCandidateItemSet(); // for each transaction t in database do increment the count of // all candidates in C(k+1) that are contained in t database.IncrementCandidatesIn(candidateTable); // L(k+1) = candidates in C(K+1) with min_support frequentTable = candidateTable.BuildFrequentItemSet(); if (frequentTable != null) { foreach (ItemSet frequentItemSet in frequentTable.Keys) { database.FindAssociationRules(frequentItemSet); } } } Console.WriteLine("Finding Association Rules is finished."); Console.WriteLine(DateTime.Now.ToString("O")); }