Example #1
0
 public static int Compare(FeatureInteraction a, FeatureInteraction b)
 {
     return(_comparer(a, b));
 }
 public static int Compare(FeatureInteraction a, FeatureInteraction b)
 {
     return _comparer(a, b);
 }
Example #3
0
        private void CollectFeatureInteractions(XgbTree tree, HashSet <XgbTreeNode> currentInteraction, double currentGain, double currentCover, double pathProbability, int depth, int deepening)
        {
            if (tree.IsLeafNode)
            {
                return;
            }

            currentInteraction.Add(tree.Data);
            currentGain  += tree.Data.Gain;
            currentCover += tree.Data.Cover;

            var pathProbabilityLeft  = pathProbability * (((XgbTree)tree.Left).Data.Cover / tree.Data.Cover);
            var pathProbabilityRight = pathProbability * (((XgbTree)tree.Right).Data.Cover / tree.Data.Cover);

            var fi = new FeatureInteraction(currentInteraction, currentGain, currentCover, pathProbability, 1);

            if (depth < _maxDeepening || _maxDeepening < 0)
            {
                var newInteractionLeft = new HashSet <XgbTreeNode>()
                {
                };
                var newInteractionRight = new HashSet <XgbTreeNode>()
                {
                };

                CollectFeatureInteractions((XgbTree)tree.Left, newInteractionLeft, 0, 0, pathProbabilityLeft, depth + 1, deepening + 1);
                CollectFeatureInteractions((XgbTree)tree.Right, newInteractionRight, 0, 0, pathProbabilityRight, depth + 1, deepening + 1);
            }

            var path = string.Join("-", currentInteraction.Select(x => x.Number));

            if (!_treeFeatureInteractions.ContainsKey(fi.Name))
            {
                _treeFeatureInteractions.Add(fi.Name, fi);
                _pathMemo.Add(path);
            }
            else
            {
                // reoccurrence?
                if (_pathMemo.Contains(path))
                {
                    return;
                }

                _pathMemo.Add(path);
                var tfi = _treeFeatureInteractions[fi.Name];
                tfi.Gain                 += currentGain;
                tfi.Cover                += currentCover;
                tfi.FScore               += 1;
                tfi.FScoreWeighted       += pathProbability;
                tfi.AverageFScoreWeighted = tfi.FScoreWeighted / tfi.FScore;
                tfi.AverageGain           = tfi.Gain / tfi.FScore;
                tfi.ExpectedGain         += currentGain * pathProbability;
            }

            if (currentInteraction.Count - 1 == _maxInteractionDepth)
            {
                return;
            }


            var currentInteractionLeft  = new HashSet <XgbTreeNode>(currentInteraction);
            var currentInteractionRight = new HashSet <XgbTreeNode>(currentInteraction);

            var leftTree  = (XgbTree)(tree.Left);
            var rightTree = (XgbTree)(tree.Right);

            if (leftTree.IsLeafNode && deepening == 0)
            {
                var tfi = _treeFeatureInteractions[fi.Name];
                tfi.SumLeafValuesLeft += leftTree.Data.LeafValue;
                tfi.SumLeafCoversLeft += leftTree.Data.Cover;
                tfi.HasLeafStatistics  = true;
            }

            if (rightTree.IsLeafNode && deepening == 0)
            {
                var tfi = _treeFeatureInteractions[fi.Name];
                tfi.SumLeafValuesRight += rightTree.Data.LeafValue;
                tfi.SumLeafCoversRight += rightTree.Data.Cover;
                tfi.HasLeafStatistics   = true;
            }

            CollectFeatureInteractions((XgbTree)tree.Left, currentInteractionLeft, currentGain, currentCover, pathProbabilityLeft, depth + 1, deepening);
            CollectFeatureInteractions((XgbTree)tree.Right, currentInteractionRight, currentGain, currentCover, pathProbabilityRight, depth + 1, deepening);
        }
Example #4
0
        private void CollectFeatureInteractions(XgbTree tree, HashSet<XgbTreeNode> currentInteraction, double currentGain, double currentCover, double pathProbability, int depth, int deepening)
        {
            if (tree.IsLeafNode)
            {
                return;
            }

            currentInteraction.Add(tree.Data);
            currentGain += tree.Data.Gain;
            currentCover += tree.Data.Cover;

            var pathProbabilityLeft = pathProbability * (((XgbTree)tree.Left).Data.Cover / tree.Data.Cover);
            var pathProbabilityRight = pathProbability * (((XgbTree)tree.Right).Data.Cover / tree.Data.Cover);

            var fi = new FeatureInteraction(currentInteraction, currentGain, currentCover, pathProbability, depth, _treeIndex, 1);

            if (depth < _maxDeepening || _maxDeepening < 0)
            {
                var newInteractionLeft = new HashSet<XgbTreeNode>() { };
                var newInteractionRight = new HashSet<XgbTreeNode>() { };

                CollectFeatureInteractions((XgbTree)tree.Left, newInteractionLeft, 0, 0, pathProbabilityLeft, depth + 1, deepening + 1);
                CollectFeatureInteractions((XgbTree)tree.Right, newInteractionRight, 0, 0, pathProbabilityRight, depth + 1, deepening + 1);
            }

            var path = string.Join("-", currentInteraction.Select(x => x.Number));

            if (!_treeFeatureInteractions.ContainsKey(fi.Name))
            {
                _treeFeatureInteractions.Add(fi.Name, fi);
                _pathMemo.Add(path);
            }
            else
            {
                if(_pathMemo.Contains(path))
                {
                    return;
                }

                _pathMemo.Add(path);
                var tfi = _treeFeatureInteractions[fi.Name];
                tfi.Gain += currentGain;
                tfi.Cover += currentCover;
                tfi.FScore += 1;
                tfi.FScoreWeighted += pathProbability;
                tfi.AverageFScoreWeighted = tfi.FScoreWeighted / tfi.FScore;
                tfi.AverageGain = tfi.Gain / tfi.FScore;
                tfi.ExpectedGain += currentGain * pathProbability;
                tfi.TreeDepth += depth;
                tfi.AverageTreeDepth = tfi.TreeDepth / tfi.FScore;
                tfi.TreeIndex += _treeIndex;
                tfi.AverageTreeIndex = tfi.TreeIndex / tfi.FScore;
                tfi.SplitValueHistogram.Merge(fi.SplitValueHistogram);
            }

            if (currentInteraction.Count - 1 == _maxInteractionDepth)
                return;

            var currentInteractionLeft = new HashSet<XgbTreeNode>(currentInteraction);
            var currentInteractionRight = new HashSet<XgbTreeNode>(currentInteraction);

            var leftTree = (XgbTree)(tree.Left);
            var rightTree = (XgbTree)(tree.Right);

            if (leftTree.IsLeafNode && deepening == 0)
            {
                var tfi = _treeFeatureInteractions[fi.Name];
                tfi.SumLeafValuesLeft += leftTree.Data.LeafValue;
                tfi.SumLeafCoversLeft += leftTree.Data.Cover;
                tfi.HasLeafStatistics = true;
            }

            if (rightTree.IsLeafNode && deepening == 0)
            {
                var tfi = _treeFeatureInteractions[fi.Name];
                tfi.SumLeafValuesRight += rightTree.Data.LeafValue;
                tfi.SumLeafCoversRight += rightTree.Data.Cover;
                tfi.HasLeafStatistics = true;
            }

            CollectFeatureInteractions((XgbTree)tree.Left, currentInteractionLeft, currentGain, currentCover, pathProbabilityLeft, depth + 1, deepening);
            CollectFeatureInteractions((XgbTree)tree.Right, currentInteractionRight, currentGain, currentCover, pathProbabilityRight, depth + 1, deepening);
        }
Example #5
0
        private void CollectFeatureInteractions(XgbTree tree, HashSet<XgbTreeNode> currentInteraction, double currentGain, double currentCover, double pathProbability, int depth)
        {
            if (tree.IsLeafNode)
                return;

            currentInteraction.Add(tree.Data);
            currentGain += tree.Data.Gain;
            currentCover += tree.Data.Cover;

            var pathProbabilityLeft = pathProbability * (((XgbTree)tree.Left).Data.Cover / tree.Data.Cover);
            var pathProbabilityRight = pathProbability * (((XgbTree)tree.Right).Data.Cover / tree.Data.Cover);

            var fi = new FeatureInteraction(currentInteraction, currentGain, currentCover, pathProbability, 1);

            if (depth < _maxDeepening || _maxDeepening < 0)
            {
                var newInteractionLeft = new HashSet<XgbTreeNode>() { };
                var newInteractionRight = new HashSet<XgbTreeNode>() { };

                CollectFeatureInteractions((XgbTree)tree.Left, newInteractionLeft, 0, 0, pathProbabilityLeft, depth + 1);
                CollectFeatureInteractions((XgbTree)tree.Right, newInteractionRight, 0, 0, pathProbabilityRight, depth + 1);
            }

            var path = string.Join("-", currentInteraction.Select(x => x.Number));

            if (!_treeFeatureInteractions.ContainsKey(fi.Name))
            {
                _treeFeatureInteractions.Add(fi.Name, fi);
                _pathMemo.Add(path);
            }
            else
            {
                // reoccurrence?
                if (!_pathMemo.Contains(path))
                {
                    _pathMemo.Add(path);
                    var tfi = _treeFeatureInteractions[fi.Name];
                    tfi.Gain += currentGain;
                    tfi.Cover += currentCover;
                    tfi.FScore += 1;
                    tfi.FScoreWeighted += pathProbability;
                    tfi.AverageFScoreWeighted = tfi.FScoreWeighted / tfi.FScore;
                    tfi.AverageGain = tfi.Gain / tfi.FScore;
                    tfi.ExpectedGain += currentGain * pathProbability;
                }

                return;
            }

            if (currentInteraction.Count - 1 == _maxInteractionDepth)
                return;

            var currentInteractionLeft = new HashSet<XgbTreeNode>(currentInteraction);
            var currentInteractionRight = new HashSet<XgbTreeNode>(currentInteraction);

            CollectFeatureInteractions((XgbTree)tree.Left, currentInteractionLeft, currentGain, currentCover, pathProbabilityLeft, depth + 1);
            CollectFeatureInteractions((XgbTree)tree.Right, currentInteractionRight, currentGain, currentCover, pathProbabilityRight, depth + 1);
        }