コード例 #1
0
        public void Count_TwoItemsAdded_ReturnsTwo()
        {
            // arrange
            string f1 = "F1";
            string f2 = "F2";
            FeatureNumericalManager featureManager = new FeatureNumericalManager();

            featureManager.Add(new FeatureNumerical("F1"));
            featureManager.Add(new FeatureNumerical("F2"));
            ItemNumericalSet set = new ItemNumericalSet(featureManager);

            ItemNumerical i1 = set.CreateItem();

            i1.SetValue(f1, 3);
            i1.SetValue(f2, 4.7f);
            set.AddItem(i1);

            ItemNumerical i2 = set.CreateItem();

            i2.SetValue(f1, 1);
            i2.SetValue(f2, 2.1f);
            set.AddItem(i2);

            // act
            int qty = set.Count();

            // assert
            Assert.AreEqual(2, qty);
        }
コード例 #2
0
        private void FillItemSet_Features4_Items15()
        {
            string f1 = "F1";
            string f2 = "F2";
            string f3 = "F3";
            string f4 = "F4";

            _set = new ItemNumericalSet(new List <string> {
                f1, f2, f3, f4
            });

            const int qty = 15;

            int[] a1 = new int[qty] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 11, 12, 13, 14, 15
            };
            float[] a2 = new float[qty] {
                1f, 0.2f, 2.3f, 4.1f, 4f, 2.76f, 1.2f, 3f, 3.14f, 0f, 2.2f, 1.4f, 1.01f, 0.3f, 0.23f
            };
            float[] a3 = new float[qty] {
                5f, 1.2f, 6.1f, 2.2f, 1f, 2.6f, 8.2f, 2f, 4.1f, 1.4f, 0.2f, 2.4f, 1.1f, 4.3f, 1.3f
            };
            int[] a4 = new int[qty] {
                1, 2, 6, 3, 1, 0, 4, 4, 2, 5, 0, 3, 3, 1, 2
            };
            for (int i = 0; i < qty; i++)
            {
                ItemNumerical item = _set.CreateItem();
                item.SetValue(f1, a1[i]);
                item.SetValue(f2, a2[i]);
                item.SetValue(f3, a3[i]);
                item.SetValue(f4, a4[i]);
                _set.AddItem(item);
            }
        }
コード例 #3
0
        private void FillItemSet_Features2_Items15()
        {
            string f1 = "F1";
            string f2 = "F2";

            _set = new ItemNumericalSet(new List <string> {
                f1, f2
            });

            const int qty = 15;

            //int[] a1 = new int[qty] { -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7 };
            //int[] a1 = new int[qty] { 0, 0, 0, 0, 0, 0, 0, 7, 8, 9, 10, 11, 12, 13, 14 };
            int[] a1 = new int[qty] {
                0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
            };
            //int[] a1 = new int[qty] { -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
            //int[] a1 = new int[qty] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
            //int[] a1 = new int[qty] { -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0 };
            //int[] a1 = new int[qty] { 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5 };
            float[] a2 = new float[qty] {
                1f, 0.2f, 2.3f, 4.1f, 4f, 2.76f, 1.2f, 3f, 3.14f, 0f, 2.1f, 3.5f, 1.4f, 2.9f, 0.4f
            };
            for (int i = 0; i < qty; i++)
            {
                ItemNumerical item = _set.CreateItem();
                item.SetValue(f1, a1[i]);
                item.SetValue(f2, a2[i]);
                _set.AddItem(item);
            }
        }
コード例 #4
0
        public void AddItem()
        {
            // arrange
            List <string> featureNames = new List <string> {
                "F1", "F2", "F3"
            };
            ItemNumericalSet set = new ItemNumericalSet(featureNames);

            // act
            FeatureNumericalValue[] arr = new FeatureNumericalValue[3] {
                new FeatureNumericalValue {
                    FeatureName = "F1", FeatureValue = 1.1
                },
                new FeatureNumericalValue {
                    FeatureName = "F2", FeatureValue = 1.2
                },
                new FeatureNumericalValue {
                    FeatureName = "F3", FeatureValue = 1.3
                }
            };
            ItemNumerical item = set.AddItem(arr);

            // assert
            Assert.IsNotNull(item);
            Assert.AreEqual(true, item.HasValue("F1") & item.HasValue("F2") & item.HasValue("F3"));
            Assert.AreEqual(1.3, item.GetValue("F3"));
        }
コード例 #5
0
        private void FillItemSet_Features2_Items10()
        {
            string f1 = "F1";
            string f2 = "F2";

            _set = new ItemNumericalSet(new List <string> {
                f1, f2
            });

            const int qty = 10;

            int[] a1 = new int[qty] {
                1, 2, 3, 4, 5, 6, 7, 8, 9, 0
            };
            float[] a2 = new float[qty] {
                1f, 0.2f, 2.3f, 4.1f, 4f, 2.76f, 1.2f, 3f, 3.14f, 0f
            };
            for (int i = 0; i < qty; i++)
            {
                ItemNumerical item = _set.CreateItem();
                item.SetValue(f1, a1[i]);
                item.SetValue(f2, a2[i]);
                _set.AddItem(item);
            }
        }
コード例 #6
0
        public void GetFeatureNames_Item_ReturnsListOfNames()
        {
            // arrange
            List <string> featureNames = new List <string> {
                "F1", "F2"
            };
            ItemNumericalSet set = new ItemNumericalSet(featureNames);
            ItemNumerical    i   = set.CreateItem();

            set.AddItem(i);

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

            // assert
            CollectionAssert.AreEqual(featureNames, names);
        }
コード例 #7
0
        public void AddFeature_ItemSetFeatures1Items1_ReturnsItemHasFeature()
        {
            // arrange
            List <string> featureNames = new List <string> {
                "F1"
            };
            ItemNumericalSet set = new ItemNumericalSet(featureNames);
            ItemNumerical    i   = set.CreateItem();

            i.SetValue("F1", 3.2);
            set.AddItem(i);

            // act
            bool r = set.AddFeature("F2");

            // assert
            Assert.AreEqual(true, r & i.HasValue("F2"));
        }
コード例 #8
0
        public void GetSeparateValue_ItemSetFeatures1Items1_ReturnsSeparateValue()
        {
            // arrange
            ISplitter        splitter = new SplitterRss();
            ItemNumericalSet set      = new ItemNumericalSet(new List <string>()
            {
                "F1"
            });
            ItemNumerical item = set.CreateItem();

            item.SetValue("F1", 1);
            set.AddItem(item);

            // act
            var sv = splitter.Split(set, "F1");

            // assert
            Assert.IsNull(sv);
        }
コード例 #9
0
        public void GetSeparateValue_ItemSetFeatures2Items5_ReturnsSeparateValue()
        {
            // arrange
            string f1 = "F1";
            string f2 = "F2";
            FeatureNumericalManager featureManager = new FeatureNumericalManager();

            featureManager.Add(new FeatureNumerical(f1));
            featureManager.Add(new FeatureNumerical(f2));
            _set = new ItemNumericalSet(featureManager);

            int[] a1 = new int[5] {
                1, 2, 3, 4, 5
            };
            float[] a2 = new float[5] {
                1f, 0.2f, 2.3f, 4.1f, 4f
            };
            for (int i = 0; i < 5; i++)
            {
                ItemNumerical item = _set.CreateItem();
                item.SetValue(f1, a1[i]);
                item.SetValue(f2, a2[i]);
                _set.AddItem(item);
            }

            ISplitter splitter = new SplitterRss();

            // act
            FeatureNumericalValue sv = splitter.Split(_set, "F1");
            double fv = Math.Round(sv.FeatureValue, 2);

            // assert
            Assert.IsNotNull(sv);
            //Assert.AreEqual(3.15, fv);
            Assert.AreEqual(1.65, fv);
        }
コード例 #10
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);
        }
コード例 #11
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);
        }