コード例 #1
0
        public void GetFeatureNames_ItemSetFeatures5_ReturnsListOf5()
        {
            // arrange
            List <string> featureNames = new List <string> {
                "F1", "F2", "F3", "F4", "F5"
            };
            ItemNumericalSet set = new ItemNumericalSet(featureNames);

            // act
            List <string> names = set.GetFeatureNames();

            // assert
            CollectionAssert.AreEqual(featureNames, names);
        }
コード例 #2
0
ファイル: TreeBuilder.cs プロジェクト: pavl0v/RandomForest
        public TreeGenerative Build(ItemNumericalSet set)
        {
            _categoryNameGenerator.Reset();
            var            buildComplete = BuildComplete;
            TreeGenerative tree          = new TreeGenerative(_resolutionFeatureName, _maxItemCountInCategory);

            tree.FeatureNames = set.GetFeatureNames();
            tree.Root         = new NodeGenerative();
            tree.Root.Set     = set;
            tree.Root.Average = tree.Root.Set.GetAverage(_resolutionFeatureName);
            BuildRecursion(tree.Root);
            if (buildComplete != null)
            {
                buildComplete(this, EventArgs.Empty);
            }
            return(tree);
        }
コード例 #3
0
        public FeatureNumericalSplitValue Split(ItemNumericalSet set, string resolutionFeatureName)
        {
            double totalIndex = set.GetRSS(resolutionFeatureName);

            if (totalIndex == 0)
            {
                return(null);
            }

            double minIndex = totalIndex;

            var featureNameList = set.GetFeatureNames();
            var featureNames    = featureNameList.Where(x => x != resolutionFeatureName);

            if (!featureNames.Any())
            {
                throw new Exception();
            }

            FeatureNumericalSplitValue res = new FeatureNumericalSplitValue();

            foreach (string fn in featureNames)
            {
                set.SortItems(fn);

                int qty = set.Count();
                for (int k = 1; k < qty; k++)
                {
                    //ItemNumericalSet left = new ItemNumericalSet(featureNameList);
                    //ItemNumericalSet right = new ItemNumericalSet(featureNameList);
                    ItemNumericalSet left  = set.Clone();
                    ItemNumericalSet right = set.Clone();
                    for (int i = 0; i < k; i++)
                    {
                        left.AddItem(set.GetItem(i));
                    }
                    for (int i = k; i < qty; i++)
                    {
                        right.AddItem(set.GetItem(i));
                    }

                    double leftIndex  = left.GetRSS(resolutionFeatureName);
                    double rightIndex = right.GetRSS(resolutionFeatureName);

                    bool sumIndexIsLessThanMinIndex = leftIndex + rightIndex < minIndex;
                    bool sumIndexIsEqualToMinIndex  = leftIndex + rightIndex == minIndex;

                    if (sumIndexIsLessThanMinIndex || (k == qty - 1 && sumIndexIsEqualToMinIndex))
                    {
                        if (sumIndexIsLessThanMinIndex)
                        {
                            minIndex = leftIndex + rightIndex;
                        }

                        res.FeatureName = fn;
                        double lv = left.GetItem(left.Count() - 1).GetValue(fn);
                        double rv = right.GetItem(0).GetValue(fn);
                        res.FeatureValue = Math.Round(lv + (rv - lv) / 2, 5);
                        res.Left         = left;
                        res.Right        = right;
                    }
                }
            }

            return(res);
        }
コード例 #4
0
ファイル: SplitterGini.cs プロジェクト: pavl0v/RandomForest
        public FeatureNumericalSplitValue Split(ItemNumericalSet set, string resolutionFeatureName)
        {
            double totalIndex = set.GetGini2(resolutionFeatureName);

            if (totalIndex == 0)
            {
                return(null);
            }

            double minIndex = totalIndex;
            double maxDelta = 0;
            int    n        = set.Count();

            var featureNameList = set.GetFeatureNames();
            var featureNames    = featureNameList.Where(x => x != resolutionFeatureName);

            if (!featureNames.Any())
            {
                throw new Exception();
            }

            FeatureNumericalSplitValue res = new FeatureNumericalSplitValue();

            foreach (string fn in featureNames)
            {
                set.SortItems(fn);

                int qty = set.Count();
                for (int k = 1; k < qty; k++)
                {
                    //ItemNumericalSet left = new ItemNumericalSet(featureNameList);
                    //ItemNumericalSet right = new ItemNumericalSet(featureNameList);
                    ItemNumericalSet left  = set.Clone();
                    ItemNumericalSet right = set.Clone();
                    for (int i = 0; i < k; i++)
                    {
                        left.AddItem(set.GetItem(i));
                    }
                    for (int i = k; i < qty; i++)
                    {
                        right.AddItem(set.GetItem(i));
                    }

                    double leftIndex  = left.GetGini2(resolutionFeatureName);
                    double rightIndex = right.GetGini2(resolutionFeatureName);
                    int    lc         = left.Count();
                    int    rc         = right.Count();
                    double delta      = totalIndex - ((lc * 1.0) / n) * leftIndex - ((rc * 1.0) / n) * rightIndex;

                    if (delta > maxDelta)
                    {
                        maxDelta        = delta;
                        res.FeatureName = fn;
                        double lv = left.GetItem(left.Count() - 1).GetValue(fn);
                        double rv = right.GetItem(0).GetValue(fn);
                        res.FeatureValue = Math.Round(lv + (rv - lv) / 2, 5);
                        res.Left         = left;
                        res.Right        = right;
                    }
                }
            }

            return(res);
        }
コード例 #5
0
ファイル: Forest.cs プロジェクト: pavl0v/RandomForest
 public List <string> GetFeatureNames()
 {
     return(_set.GetFeatureNames());
 }