Пример #1
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);
        }
Пример #2
0
        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);
        }