Пример #1
0
        public static ItemsetCollection FindSubsets(Itemset itemset, int n)
        {
            ItemsetCollection subsets = new ItemsetCollection();

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

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

                    Itemset subset = new Itemset();
                    for (int 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)
        {
            Itemset           I  = db.GetUniqueItems();
            ItemsetCollection L  = new ItemsetCollection(); //resultant large itemsets
            ItemsetCollection Li = new ItemsetCollection(); //large itemset in each iteration
            ItemsetCollection Ci = new ItemsetCollection(); //pruned itemset in each iteration

            //first iteration (1-item itemsets)
            foreach (string item in I)
            {
                Ci.Add(new Itemset()
                {
                    item
                });
            }

            //next iterations
            int k = 2;

            while (Ci.Count != 0)
            {
                //set Li from Ci (pruning)
                Li.Clear();
                foreach (Itemset 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);
        }
Пример #3
0
        public Apriori()
        {
            ItemsetCollection data = new ItemsetCollection();

            List <itemset> temp = db.itemsets.ToList();

            foreach (var item in temp)
            {
                Itemset       item11       = new Itemset();
                List <string> itemsetsList = item.items.Split(',').ToList();
                foreach (string sp in itemsetsList)
                {
                    item11.Add(sp);
                }
                data.Add(item11);
            }
            StringBuilder s           = new StringBuilder();
            Itemset       uniqueItems = data.GetUniqueItems();

            s.Append(uniqueItems + "<br/>");
            //test apriori
            ItemsetCollection L = AprioriMining.DoApriori(data, 30.0);

            s.Append("<br/>" + L.Count + " itemsets in L<br/>");
            foreach (Itemset i in L)
            {
                s.Append(i + "<br/>");
            }

            //test mining
            List <AssociationRule> allRules = AprioriMining.Mine(data, L, 70.0);

            rules = allRules;
            s.Append("<br/>" + allRules.Count + " rules<br/>");
            foreach (AssociationRule rule in allRules)
            {
                s.Append(rule + "<br/>");
            }
            str = s.ToString();
        }