Exemple #1
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);
            }
        }
Exemple #2
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);
        }
Exemple #3
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"));
        }
Exemple #4
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);
            }
        }
Exemple #5
0
        public void Grow_TrainingDataJson()
        {
            // arrange
            IForest forest = new Forest();

            ItemNumerical item1 = ItemNumerical.Create();

            item1.AddValue("X", 1.1);
            item1.AddValue("Y", 2.1);
            item1.AddValue("Z", 0);

            ItemNumerical item2 = ItemNumerical.Create();

            item2.AddValue("X", 1.9);
            item2.AddValue("Y", 3.1);
            item2.AddValue("Z", 0);

            // act
            int treeCount = forest.Grow(@"data\cross");
            var d1        = Math.Round(forest.Resolve(item1), 0);
            var d2        = Math.Round(forest.Resolve(item2), 0);

            // assert
            Assert.AreEqual(50, treeCount);
            Assert.AreEqual(2, d1);
            Assert.AreEqual(1, d2);
        }
Exemple #6
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);
            }
        }
        public ItemNumerical CreateItem()
        {
            ItemNumerical item  = ItemNumerical.Create();
            var           names = _featureManager.GetFeatureNames();

            foreach (var name in names)
            {
                FeatureNumerical feature = _featureManager.Get(name);
                item.AddValue(name);
            }
            return(item);
        }
Exemple #8
0
        public void CreateItem_ReturnsItem()
        {
            // arrange
            FeatureNumericalManager featureManager = new FeatureNumericalManager();

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

            // act
            ItemNumerical item = set.CreateItem();

            // assert
            Assert.IsNotNull(item);
            Assert.AreEqual(true, item.HasValue("F1") & item.HasValue("F2"));
        }
Exemple #9
0
        public void Resolve()
        {
            // arrange
            string        name = "test-2x90-sin";
            ItemNumerical item = ItemNumerical.Create();

            item.AddValue("X", 1.45);
            item.AddValue("Y", 0);

            Tree tree = ImportFromJson.Read(string.Format(@"data\{0}.json", name));

            // act
            var val = tree.Resolve(item);

            // assert
        }
Exemple #10
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);
        }
Exemple #11
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"));
        }
        public ItemNumerical AddItem(FeatureNumericalValue[] values)
        {
            ItemNumerical item = CreateItem();

            foreach (var v in values)
            {
                if (item.HasValue(v.FeatureName))
                {
                    item.SetValue(v.FeatureName, v.FeatureValue);
                }
                else
                {
                    throw new ArgumentException();
                }
            }
            _items.Add(item);

            return(item);
        }
Exemple #13
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);
        }
Exemple #14
0
        public void Resolve()
        {
            // arrange
            string importPath = @"data\forest40";
            Forest forest     = new Forest();

            forest.ImportFromJsonTPL(importPath);
            ItemNumerical item = ItemNumerical.Create();

            item.AddValue("F1", 41);
            item.AddValue("F2", 42);
            item.AddValue("F3", 47);
            item.AddValue("F4", 46);

            // act
            double v = forest.Resolve(item);
            double d = item.GetValue("F3") - v;

            // assert
        }
Exemple #15
0
        public void Grow_TrainingDataXlsx_Gini()
        {
            // arrange
            IForest forest = new Forest();
            ForestGrowParameters parameters = new ForestGrowParameters
            {
                ExportDirectoryPath    = @"temp\cross-gini",
                ExportToJson           = true,
                ResolutionFeatureName  = "Z",
                ItemSubsetCountRatio   = 0.3f,
                TrainingDataPath       = @"data\test-3x1000-cross.xlsx",
                MaxItemCountInCategory = 10,
                TreeCount = 50,
                SplitMode = SplitMode.GINI
            };

            ItemNumerical item1 = ItemNumerical.Create();

            item1.AddValue("X", 1.1);
            item1.AddValue("Y", 2.1);
            item1.AddValue("Z", 0);

            ItemNumerical item2 = ItemNumerical.Create();

            item2.AddValue("X", 1.9);
            item2.AddValue("Y", 3.1);
            item2.AddValue("Z", 0);

            // act
            int treeCount = forest.Grow(parameters);
            var d1        = Math.Round(forest.Resolve(item1), 0);
            var d2        = Math.Round(forest.Resolve(item2), 0);

            // assert
            Assert.AreEqual(50, treeCount);
            Assert.AreEqual(2, d1);
            Assert.AreEqual(1, d2);
        }
Exemple #16
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);
        }
Exemple #17
0
        public void Clone_Item_ReturnsClone()
        {
            // 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    item = set.CreateItem();

            item.SetValue(f1, 5);
            item.SetValue(f2, 3.8f);

            // act
            ItemNumerical clone = item.Clone();

            item.SetValue(f1, 10);

            // assert
            Assert.AreEqual(5, clone.GetValue(f1));
            Assert.AreEqual(3.8f, clone.GetValue(f2));
        }
 public void AddItem(ItemNumerical item)
 {
     _items.Add(item);
 }