예제 #1
0
        private double GetConfidence(string X, string XY, ItemsDictionary allFrequentItems)
        {
            double supportX  = allFrequentItems[X].Support;
            double supportXY = allFrequentItems[XY].Support;

            return(supportXY / supportX);
        }
예제 #2
0
        Output IApriori.ProcessTransaction(double minSupport, double minConfidence, IEnumerable <string> items, string[] transactions)
        {
            IList <Item>    frequentItems    = GetL1FrequentItems(minSupport, items, transactions);
            ItemsDictionary allFrequentItems = new ItemsDictionary();

            allFrequentItems.ConcatItems(frequentItems);
            IDictionary <string, double> candidates = new Dictionary <string, double>();
            double transactionsCount = transactions.Count();

            do
            {
                candidates    = GenerateCandidates(frequentItems, transactions);
                frequentItems = GetFrequentItems(candidates, minSupport, transactionsCount);
                allFrequentItems.ConcatItems(frequentItems);
            }while (candidates.Count != 0);

            HashSet <Rule> rules       = GenerateRules(allFrequentItems);
            IList <Rule>   strongRules = GetStrongRules(minConfidence, rules, allFrequentItems);
            Dictionary <string, Dictionary <string, double> > closedItemSets = GetClosedItemSets(allFrequentItems);
            IList <string> maximalItemSets = GetMaximalItemSets(closedItemSets);

            return(new Output
            {
                StrongRules = strongRules,
                MaximalItemSets = maximalItemSets,
                ClosedItemSets = closedItemSets,
                FrequentItems = allFrequentItems
            });
        }
예제 #3
0
        private HashSet <Rule> GenerateRules(ItemsDictionary allFrequentItems)
        {
            var rulesList = new HashSet <Rule>();

            foreach (var item in allFrequentItems)
            {
                if (item.Name.Length > 1)
                {
                    IEnumerable <string> subsetsList = GenerateSubsets(item.Name);

                    foreach (var subset in subsetsList)
                    {
                        string remaining = GetRemaining(subset, item.Name);
                        Rule   rule      = new Rule(subset, remaining, 0);

                        if (!rulesList.Contains(rule))
                        {
                            rulesList.Add(rule);
                        }
                    }
                }
            }

            return(rulesList);
        }
예제 #4
0
        private Dictionary <string, Dictionary <string, double> > GetClosedItemSets(ItemsDictionary allFrequentItems)
        {
            var closedItemSets = new Dictionary <string, Dictionary <string, double> >();
            int i = 0;

            foreach (var item in allFrequentItems)
            {
                Dictionary <string, double> parents = GetItemParents(item.Name, ++i, allFrequentItems);

                if (CheckIsClosed(item.Name, parents, allFrequentItems))
                {
                    closedItemSets.Add(item.Name, parents);
                }
            }

            return(closedItemSets);
        }
예제 #5
0
        private Dictionary <string, double> GetItemParents(string child, int index, ItemsDictionary allFrequentItems)
        {
            var parents = new Dictionary <string, double>();

            for (int j = index; j < allFrequentItems.Count; j++)
            {
                string parent = allFrequentItems[j].Name;

                if (parent.Length == child.Length + 1)
                {
                    if (CheckIsSubset(child, parent))
                    {
                        parents.Add(parent, allFrequentItems[parent].Support);
                    }
                }
            }

            return(parents);
        }
예제 #6
0
        private void AddStrongRule(Rule rule, string XY, List <Rule> strongRules, double minConfidence, ItemsDictionary allFrequentItems)
        {
            double confidence = GetConfidence(rule.X, XY, allFrequentItems);

            if (confidence >= minConfidence)
            {
                Rule newRule = new Rule(rule.X, rule.Y, confidence);
                strongRules.Add(newRule);
            }

            confidence = GetConfidence(rule.Y, XY, allFrequentItems);

            if (confidence >= minConfidence)
            {
                Rule newRule = new Rule(rule.Y, rule.X, confidence);
                strongRules.Add(newRule);
            }
        }
예제 #7
0
        private IList <Rule> GetStrongRules(double minConfidence, HashSet <Rule> rules, ItemsDictionary allFrequentItems)
        {
            var strongRules = new List <Rule>();

            foreach (Rule rule in rules)
            {
                string xy = _sorter.Sort(rule.X + rule.Y);
                AddStrongRule(rule, xy, strongRules, minConfidence, allFrequentItems);
            }

            strongRules.Sort();
            return(strongRules);
        }
예제 #8
0
        private bool CheckIsClosed(string child, Dictionary <string, double> parents, ItemsDictionary allFrequentItems)
        {
            foreach (string parent in parents.Keys)
            {
                if (allFrequentItems[child].Support == allFrequentItems[parent].Support)
                {
                    return(false);
                }
            }

            return(true);
        }