Пример #1
0
 /// <summary>
 /// Ctor for the class.
 /// </summary>
 /// <param name="value">Value represented by the node.</param>
 /// <param name="parentNode">Parent of the node in the tree</param>
 /// <param name="parentTree">Tree where the node belongs to.</param>
 /// <param name="support">Support for the node</param>
 public TreeNode(T value, TreeNode <T> parentNode, FpTree <T> parentTree, int support)
 {
     Value              = value;
     ParentNode         = parentNode;
     _parentTree        = parentTree;
     TransactionCounter = support;
     _children          = new Dictionary <T, TreeNode <T> >();
 }
Пример #2
0
        /// <summary>
        /// Fp-growth procedure for specified tree and the conditional base pattern.
        /// </summary>
        /// <param name="tree">FP-tree</param>
        /// <param name="alpha">The cinditional base pattern</param>
        private void FpGrowth(FpTree <T> tree, IEnumerable <T> alpha)
        {
            if (tree.HasSinglePath())
            {
                var path = tree.GetSinglePath();

                var elements = path.Select(node => node.Value).ToList();

                var values = EnumerableHelper.GetSubsets(elements);
                foreach (var list in values)
                {
                    if (list.Count <= 0)
                    {
                        continue;
                    }

                    var support = int.MaxValue;
                    foreach (var node in path)
                    {
                        if (node.TransactionCounter < support)
                        {
                            support = node.TransactionCounter;
                        }
                    }

                    var itemSet = new List <T>(list);
                    itemSet.AddRange(alpha);
                    var frequentItemSet = new FrequentItemSet <T>(itemSet);
                    FrequentItemSets.Add(frequentItemSet, support);
                }
                return;
            }

            var sortedFrequencyList = tree.GetFrequencyList();

            foreach (var list in sortedFrequencyList)
            {
                foreach (var value in list.Value)
                {
                    var beta = new List <T>(alpha);
                    beta.Insert(0, value);
                    var patternBase = tree.GetCondPatternBase(value);
                    FrequentItemSets.Add(new FrequentItemSet <T>(beta), tree.GetSupport(value));

                    var baseGenerator = new CondPatternBaseGenerator <T>();
                    var database      = baseGenerator.Generate(patternBase, _minSupport);
                    if (database.Count > 0)
                    {
                        var newTree = new FpTree <T>();
                        newTree.BuildConditionalTree(database);
                        FpGrowth(newTree, beta);
                    }
                }
            }
        }
Пример #3
0
        /// <summary>
        /// Generate the decision rules based on the specisied FP-tree and the minimal
        /// confidence.
        /// </summary>
        /// <param name="tree">FP-tree</param>
        /// <param name="minConfidence">The minimal confidence</param>
        /// <returns></returns>
        public List <DecisionRule <T> > GenerateRuleSet(FpTree <T> tree, double minConfidence)
        {
            FpGrowth(tree, new List <T>());

            var decisionRules = new List <DecisionRule <T> >();

            foreach (var frequentItemSet in FrequentItemSets.Keys)
            {
                if (frequentItemSet.ItemSet.Count < 2)
                {
                    continue;
                }

                var subSets = EnumerableHelper.GetSubsets(frequentItemSet.ItemSet);

                foreach (var t in subSets)
                {
                    var leftSide = new FrequentItemSet <T>(t);
                    for (var j = 0; j < subSets.Count; j++)
                    {
                        var rightSide = new FrequentItemSet <T>(subSets[j]);
                        if (rightSide.ItemSet.Count != 1 || !FrequentItemSet <T> .SetsSeparated(rightSide, leftSide))
                        {
                            continue;
                        }

                        if (FrequentItemSets.ContainsKey(leftSide))
                        {
                            var confidence = (double)FrequentItemSets[frequentItemSet] / FrequentItemSets[leftSide];
                            if (confidence >= minConfidence)
                            {
                                var rule = new DecisionRule <T>(leftSide.ItemSet, rightSide.ItemSet, FrequentItemSets[frequentItemSet], confidence);
                                decisionRules.Add(rule);
                            }
                        }
                    }
                }
            }
            return(decisionRules);
        }