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")); }
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); } }
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); } }
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); } }
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); }
public void GetRandomSubset_9from15_Returns9() { // arrange FillItemSet_Features4_Items15(); float subcountRatio = 0.6f; // act ItemNumericalSet subset = _set.GetRandomSubset(subcountRatio, false); // assert Assert.IsNotNull(subset); Assert.AreEqual(9, subset.Count()); }
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); }
public int GenerateTrees(int count, string resolutionFeatureName, int maxItemCountInCategory, float itemSubsetCountRatio) { _trees.Clear(); var nameGenerator = new NameGenerator(); _treeBulder = new TreeBuilder(resolutionFeatureName, maxItemCountInCategory, nameGenerator, _splitter); for (int i = 0; i < count; i++) { ItemNumericalSet subset = _set.GetRandomSubset(itemSubsetCountRatio, true); TreeGenerative tg = _treeBulder.Build(subset); _trees.Add(tg.ToTree()); nameGenerator.Reset(); } return(TreeCount()); }
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")); }
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); }
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); }
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 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); }
public void FillItemNumericalSet_ExcelDocument_() { // arrange GenerateExcelDocumentF3(); FileInfo fi = new FileInfo(@"temp\test.xlsx"); //FileInfo fi = new FileInfo(@"data\test-4x1000.xlsx"); if (!fi.Exists) { Assert.Fail(); } // act ItemNumericalSet set = ExcelParser.ParseItemNumericalSet(fi.FullName, 1); // assert Assert.IsNotNull(set); Assert.AreEqual(_itemsQty, set.Count()); Assert.AreEqual(true, set.CheckConsistency()); }
public void Export_ExportToTxt() { // arrange ISplitter splitter = new SplitterRss(); ItemNumericalSet set = ExcelParser.ParseItemNumericalSet(@"data\test-4x15.xlsx"); TreeBuilder builder = new TreeBuilder("F1", 5, new Lib.NameGenerator(), splitter); TreeGenerative tree = builder.Build(set); tree.ExportStrategy = new ExportToTxt(); DirectoryInfo di = new DirectoryInfo("temp"); if (!di.Exists) { di.Create(); } // act tree.Export(@"temp\test-4x15.txt"); // assert }
public void Export_ExportToJson() { // arrange string name = "test-2x90-sin"; ISplitter splitter = new SplitterRss(); ItemNumericalSet set = ExcelParser.ParseItemNumericalSet(string.Format(@"data\{0}.xlsx", name)); TreeBuilder builder = new TreeBuilder("Y", 5, new Lib.NameGenerator(), splitter); TreeGenerative tree = builder.Build(set); tree.ExportStrategy = new ExportToJson(); DirectoryInfo di = new DirectoryInfo("temp"); if (!di.Exists) { di.Create(); } // act tree.Export(string.Format(@"temp\{0}.json", name)); // assert }
public int GenerateTreesTPL(int treeCount, string resolutionFeatureName, int maxItemCountInCategory, float itemSubsetCountRatio) { _trees.Clear(); Parallel.For(0, treeCount, (i) => { NameGenerator nameGenerator = new NameGenerator(); ItemNumericalSet subset = _set.GetRandomSubset(itemSubsetCountRatio, true); _treeBulder = new TreeBuilder(resolutionFeatureName, maxItemCountInCategory, nameGenerator, _splitter); _treeBulder.BuildComplete += OnTreeBuildComplete; TreeGenerative tg = _treeBulder.Build(subset); _treeBulder.BuildComplete -= OnTreeBuildComplete; _trees.Add(tg.ToTree()); }); var forestGrowComplete = ForestGrowComplete; if (forestGrowComplete != null) { forestGrowComplete(this, EventArgs.Empty); } return(TreeCount()); }
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 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); }
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); }
private void FillItemSet_ExcelParser() { //_set = ExcelParser.ParseItemNumericalSet(@"data\test-15-4.xlsx"); //_set = ExcelParser.ParseItemNumericalSet(@"data\test-1000-4.xlsx"); _set = ExcelParser.ParseItemNumericalSet(@"data\test-3x1000-cross.xlsx"); }
public int InitializeItemSet(string pathToXlsx) { _set = ExcelParser.ParseItemNumericalSet(pathToXlsx); return(ItemCount()); }
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); }