コード例 #1
0
ファイル: AprioriMining.cs プロジェクト: radtek/Golden-Ticket
        public static List <AssociationRule> Mine(ItemsetCollection db, ItemsetCollection l, double confidenceThreshold)
        {
            var allRules = new List <AssociationRule>();

            foreach (Itemset itemset in l)
            {
                var subsets = Bit.FindSubsets(itemset, 0); //get all subsets
                foreach (var subset in subsets)
                {
                    var confidence = (db.FindSupport(itemset) / db.FindSupport(subset)) * 100.0;
                    if (confidence >= confidenceThreshold)
                    {
                        var rule = new AssociationRule();
                        rule.X.AddRange(subset);
                        rule.Y.AddRange(itemset.Remove(subset));
                        rule.Support    = db.FindSupport(itemset);
                        rule.Confidence = confidence;
                        if (rule.X.Count > 0 && rule.Y.Count > 0)
                        {
                            allRules.Add(rule);
                        }
                    }
                }
            }

            return(allRules);
        }
コード例 #2
0
        public static ItemsetCollection FindSubsets(Itemset itemset, int n)
        {
            var subsets = new ItemsetCollection();

            var subsetCount = (int)Math.Pow(2, itemset.Count);

            for (var i = 0; i < subsetCount; i++)
            {
                if (n == 0 || GetOnCount(i, itemset.Count) == n)
                {
                    var binary = DecimalToBinary(i, itemset.Count);

                    var subset = new Itemset();
                    for (var charIndex = 0; charIndex < binary.Length; charIndex++)
                    {
                        if (binary[charIndex] == '1')
                        {
                            subset.Add(itemset[charIndex]);
                        }
                    }
                    subsets.Add(subset);
                }
            }

            return(subsets);
        }
コード例 #3
0
ファイル: AprioriMining.cs プロジェクト: radtek/Golden-Ticket
        public static ItemsetCollection DoApriori(ItemsetCollection db, double supportThreshold)
        {
            var I  = db.GetUniqueItems();
            var l  = new ItemsetCollection(); //resultant large itemsets
            var li = new ItemsetCollection(); //large itemset in each iteration
            var ci = new ItemsetCollection(); //pruned itemset in each iteration

            ci.AddRange(I.Select(item => new Itemset {
                item
            }));

            //first iteration (1-item itemsets)
            //next iterations
            var k = 2;

            while (ci.Count != 0)
            {
                //set Li from Ci (pruning)
                li.Clear();
                foreach (var itemset in ci)
                {
                    itemset.Support = db.FindSupport(itemset);
                    if (itemset.Support >= supportThreshold)
                    {
                        li.Add(itemset);
                        l.Add(itemset);
                    }
                }

                //set Ci for next iteration (find supersets of Li)
                ci.Clear();
                ci.AddRange(Bit.FindSubsets(li.GetUniqueItems(), k)); //get k-item subsets
                k += 1;
            }

            return(l);
        }