private double GetConfidence(string X, string XY, ItemsDictionary allFrequentItems) { double supportX = allFrequentItems[X].Support; double supportXY = allFrequentItems[XY].Support; return(supportXY / supportX); }
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 }); }
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); }
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); }
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); }
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); } }
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); }
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); }