public void TestMaxHeapFPGrowth() { FPGrowth<string> fp = new FPGrowth<string>(); List<KeyValuePair<List<string>, long>> transactions = new List<KeyValuePair<List<string>, long>>(); transactions.Add(new KeyValuePair<List<string>, long>(Arrays.asList("E", "A", "D", "B"), 1L)); transactions.Add(new KeyValuePair<List<string>, long>(Arrays.asList("D", "A", "C", "E", "B"), 1L)); transactions.Add(new KeyValuePair<List<string>, long>(Arrays.asList("C", "A", "B", "E"), 1L)); transactions.Add(new KeyValuePair<List<string>, long>(Arrays.asList("B", "A", "D"), 1L)); transactions.Add(new KeyValuePair<List<string>, long>(Arrays.asList("D"), 1L)); transactions.Add(new KeyValuePair<List<string>, long>(Arrays.asList("D", "B"), 1L)); transactions.Add(new KeyValuePair<List<string>, long>(Arrays.asList("A", "D", "E"), 1L)); transactions.Add(new KeyValuePair<List<string>, long>(Arrays.asList("B", "C"), 1L)); using (var outWriter = new StringWriter()) { fp.GenerateTopKFrequentPatterns( transactions, fp.GenerateFList(transactions, 3), 3, 100, new HashSet<string>(), new StringOutputConverter(new SequenceFileOutputCollector<string, TopKStringPatterns>(outWriter, "{0}\t{1},")), new ContextStatusUpdater()); var res = @"C ([B,C],3)," + @"E ([A,E],4), ([A,B,E],3), ([A,D,E],3)," + @"A ([A],5), ([A,D],4), ([A,E],4), ([A,B],4), ([A,B,E],3), ([A,D,E],3), ([A,B,D],3)," + @"D ([D],6), ([B,D],4), ([A,D],4), ([A,D,E],3), ([A,B,D],3)," + @"B ([B],6), ([A,B],4), ([B,D],4), ([A,B,D],3), ([A,B,E],3), ([B,C],3),"; Assert.AreEqual(outWriter.ToString(), res); } }
static void Main(string[] args) { /* This sample demonstrate how-to use the FP Growth algorithm. As a test data I use a data from an imagine book store: 1) We have a list of authors, which the store sells. 2) We have an information about shopping cart of some users. As a result we get the following information: 1. Order by popularity of the authors/set of authors. 2. What we can advise the user, basing on his shopping cart. It is ordered by probability (frequent). */ TypeRegister.Register<BookAuthor>((a, b) => a.Name.CompareTo(b.Name)); var itemsets = new List<Itemset<BookAuthor>>(); var transactions = new SampleHelper().Transactions.ToList(); var fpGrowth = new FPGrowth<BookAuthor> { MinSupport = (double) 1/9, SaveItemset = itemset => itemsets.Add(itemset), GetTransactions = () => transactions }; fpGrowth.ProcessTransactions(); var rules = new List<Rule<BookAuthor>>(); var agrawal = new AgrawalFaster<BookAuthor> { MinLift = 0.01, MinConfidence = 0.01, TransactionsCount = transactions.Count(), GetItemsets = () => itemsets, SaveRule = rule => rules.Add(rule) }; agrawal.Run(); foreach (var item in itemsets.OrderByDescending(v => v.Support)) { Console.WriteLine(string.Join("; ", item.Value.OrderBy(i => i.Name)) + " #SUP: " + item.Support); } Console.WriteLine("===="); foreach (var item in rules.OrderByDescending(r => r.Confidence)) { Console.WriteLine(string.Join("; ", item.Combination) + " => " + string.Join("; ", item.Remaining) + " ===> Confidence: " + item.Confidence + " Lift: " + item.Lift); } Console.ReadLine(); }
public override void ViewDidLoad() { TypeRegister.Register<BookAuthor>((a, b) => a.Name.CompareTo(b.Name)); var itemsets = new List<Itemset<BookAuthor>>(); var transactions = new SampleHelper().Transactions.ToList(); var fpGrowth = new FPGrowth<BookAuthor> { MinSupport = (double) 1/9, SaveItemset = itemset => itemsets.Add(itemset), GetTransactions = () => transactions }; fpGrowth.ProcessTransactions(); var rules = new List<Rule<BookAuthor>>(); var agrawal = new AgrawalFaster<BookAuthor> { MinLift = 0.01, MinConfidence = 0.01, TransactionsCount = transactions.Count(), GetItemsets = () => itemsets, SaveRule = rule => rules.Add(rule) }; agrawal.Run(); FrequentItemsTable.RegisterClassForCellReuse (typeof(FrequentItemTableViewCell), FrequentTableViewDelegate.CellIdentifier); FrequentItemsTable.Source = new FrequentTableViewDelegate{ GetData = () => itemsets.OrderByDescending(i=>i.Support).ToList() }; FrequentRulesTable.RegisterClassForCellReuse (typeof(FrequentRulesTableViewCell), FrequentRulesTableViewDelegate.CellIdentifier); FrequentRulesTable.Source = new FrequentRulesTableViewDelegate{ GetData = () => rules.OrderByDescending(i=>i.Confidence).ToList() }; }
static void Main(string[] args) { DateTime start = DateTime.Now; double minSupport = 0.005; double minConfidence = 0.5; string filepath = @"path/to/file"; List <Transaction> dataset = PrepareData(filepath); FPGrowth method = new FPGrowth(dataset, minSupport * dataset.Count); FPTree tree = method.GenerateTree(false); var patterns = method.MineFrequentPatterns(); var rules = method.GenerateRules(patterns, minSupport, minConfidence); DateTime end = DateTime.Now; Console.WriteLine("Program finished in {0} seconds", (end - start).TotalSeconds); patterns.ForEach(p => Console.WriteLine(p)); Console.WriteLine("=========================================================================="); rules.Sort((r1, r2) => r2.Confidence.CompareTo(r1.Confidence)); rules.ForEach(p => Console.WriteLine(p)); Console.ReadKey(); }
}//DMForm //Methods ************************************************************* //Events ************************************************************** private void btnExecute_Click(object sender, EventArgs e) { if (cmbAlgorithm.SelectedItem == null) { MessageBox.Show("Please select an algorithm."); return; }//if no algorithm string algorithm = cmbAlgorithm.SelectedItem.ToString(); if (algorithm.Equals("Apriori")) { rtbResults.Text = ""; List <ItemSetDto> results = new List <ItemSetDto>(); DateTime timer = DateTime.Now; if (cmbSource.SelectedItem == null) { MessageBox.Show("Please select a data source."); return; }//if if (txtMinSupport.Text.Equals("")) { MessageBox.Show("Please enter a Minimum Support"); return; }//if double minSupport = Convert.ToDouble(txtMinSupport.Text); string datasource = cmbSource.SelectedItem.ToString(); DataSourceBLL dsBLL = new DataSourceBLL(); Apriori ap = new Apriori(); List <TransactionDto> transactions = dsBLL.process(datasource); if (transactions == null) { return; } results = ap.Process(transactions, null, minSupport); StringBuilder sb = new StringBuilder(); foreach (ItemSetDto isd in results) { sb.AppendLine(isd.toString()); } rtbResults.AppendText(sb.ToString() + "\nTime to Complete: " + (DateTime.Now - timer).ToString()); }//if "Apriori" else if (algorithm.Equals("FPGrowth")) { if (txtMinSupport.Text.Equals("")) { MessageBox.Show("Please enter a Minimum Support"); return; }//if double minSupport = Convert.ToDouble(txtMinSupport.Text); string datasource = cmbSource.SelectedItem.ToString(); DataSourceBLL dsBLL = new DataSourceBLL(); List <TransactionDto> dtos = dsBLL.process(datasource); if (dtos == null) { return; } rtbResults.Text = ""; DateTime timer = DateTime.Now; FPGrowth fpg = new FPGrowth(dtos, minSupport); List <FrequentPattern> fp = fpg.mine(); StringBuilder sb = new StringBuilder(); foreach (FrequentPattern f in fp) { sb.AppendLine(f.ToString()); } rtbResults.AppendText(sb.ToString() + "\nTime to Complete: " + (DateTime.Now - timer).ToString()); }//if "FPGrowth" else if (algorithm.Equals("Eclat")) { rtbResults.Text = ""; List <VerticalItemSetDto> results = new List <VerticalItemSetDto>(); DateTime timer = DateTime.Now; if (cmbSource.SelectedItem == null) { MessageBox.Show("Please select a data source."); return; }//if if (txtMinSupport.Text.Equals("")) { MessageBox.Show("Please enter a Minimum Support"); return; }//if double minSupport = Convert.ToDouble(txtMinSupport.Text); string datasource = cmbSource.SelectedItem.ToString(); DataSourceBLL dsBLL = new DataSourceBLL(); Eclat ec = new Eclat(); List <TransactionDto> transactions = dsBLL.process(datasource); if (transactions == null) { return; } results = ec.Process(transactions, null, minSupport); StringBuilder sb = new StringBuilder(); foreach (VerticalItemSetDto isd in results) { sb.AppendLine(isd.toString()); } rtbResults.AppendText(sb.ToString() + "\nTime to Complete: " + (DateTime.Now - timer).ToString()); } //if "Eclat" } //btnExecute_Click
public void TestMaxHeapFPGrowthData2() { FPGrowth<String> fp = new FPGrowth<String>(); ICollection<KeyValuePair<List<string>, long>> transactions = new List<KeyValuePair<List<string>, long>>(); transactions.Add(new KeyValuePair<List<string>, long>(Arrays.asList("X"), 12L)); transactions.Add(new KeyValuePair<List<string>, long>(Arrays.asList("Y"), 4L)); transactions.Add(new KeyValuePair<List<string>, long>(Arrays.asList("X", "Y"), 10L)); transactions.Add(new KeyValuePair<List<string>, long>(Arrays.asList("X", "Y", "Z"), 11L)); using (var outWriter = new StringWriter()) { Console.WriteLine(fp.GenerateFList(transactions, 2)); fp.GenerateTopKFrequentPatterns( transactions, fp.GenerateFList(transactions, 2), 2, 100, new HashSet<string>(), new StringOutputConverter(new SequenceFileOutputCollector<string, TopKStringPatterns>(outWriter, "({0},{1}), ")), new ContextStatusUpdater()); var res = @"(Z,([X,Y,Z],11)), (Y,([Y],25), ([X,Y],21), ([X,Y,Z],11)), (X,([X],33), ([X,Y],21), ([X,Y,Z],11)), "; Assert.AreEqual(outWriter.ToString(), res); } }