public void EnumerateTest() { DecisionTree tree; int[][] inputs; int[] outputs; ID3LearningTest.CreateMitchellExample(out tree, out inputs, out outputs); DecisionNode[] expected = { tree.Root, tree.Root.Branches[0], // Outlook = 0 tree.Root.Branches[0].Branches[0], // Humidity = 0 tree.Root.Branches[0].Branches[1], // Humidity = 1 tree.Root.Branches[1], // Outlook = 1 tree.Root.Branches[2], // Outlook = 2 tree.Root.Branches[2].Branches[0], // Wind = 0 tree.Root.Branches[2].Branches[1], // Wind = 1 }; int i = 0; foreach (var node in tree) { Assert.AreEqual(expected[i++], node); } Assert.AreEqual(expected.Length, i); }
public void unknown_values_test() { // https://github.com/accord-net/framework/issues/689 DecisionTree tree; int[][] inputs; int[] outputs; ID3LearningTest.CreateMitchellExample(out tree, out inputs, out outputs); int a = tree.Decide(new[] { 42, Double.NaN, 52, 21 }); int b = tree.Decide(new[] { 42, 3, 52, 21 }); Assert.AreEqual(0, a); Assert.AreEqual(0, b); }
public void WriteTest() { DecisionTree tree; int[][] inputs; int[] outputs; ID3LearningTest.CreateMitchellExample(out tree, out inputs, out outputs); var csc = new CSharpCodeProvider(); var parameters = new CompilerParameters(new[] { "System.dll", "mscorlib.dll" }) { GenerateInMemory = true }; var compilerResult = csc.CompileAssemblyFromSource(parameters, tree.ToCode("AccordDecisionTree")); Assert.That(compilerResult.Errors.HasErrors, Is.False, ToString(compilerResult.Errors)); }
public void DeserializationTest1() { MemoryStream stream = new MemoryStream(Properties.Resources.tree); DecisionTree tree = Serializer.Load <DecisionTree>(stream); Assert.AreEqual(4, tree.InputCount); Assert.AreEqual(2, tree.OutputClasses); Assert.IsNotNull(tree.Root); DecisionTree newtree; int[][] inputs; int[] outputs; ID3LearningTest.CreateMitchellExample(out newtree, out inputs, out outputs); for (int i = 0; i < inputs.Length; i++) { int y = tree.Compute(inputs[i].ToDouble()); Assert.AreEqual(outputs[i], y); } DecisionNode[] expected = { tree.Root, tree.Root.Branches[0], // Outlook = 0 tree.Root.Branches[1], // Outlook = 1 tree.Root.Branches[2], // Outlook = 2 tree.Root.Branches[0].Branches[0], // Humidity = 0 tree.Root.Branches[0].Branches[1], // Humidity = 1 tree.Root.Branches[2].Branches[0], // Wind = 0 tree.Root.Branches[2].Branches[1], // Wind = 1 }; int c = 0; foreach (var node in tree.Traverse(DecisionTreeTraversal.BreadthFirst)) { Assert.AreEqual(expected[c++], node); } Assert.AreEqual(expected.Length, c); }
public void ComputeTest() { DecisionTree tree; int[][] inputs; int[] outputs; ID3LearningTest.CreateMitchellExample(out tree, out inputs, out outputs); Assert.AreEqual(4, tree.InputCount); Assert.AreEqual(2, tree.OutputClasses); for (int i = 0; i < inputs.Length; i++) { int y = tree.Compute(inputs[i].ToDouble()); Assert.AreEqual(outputs[i], y); } }
public void CreateTest() { DecisionTree tree; int[][] inputs; int[] outputs; ID3LearningTest.CreateMitchellExample(out tree, out inputs, out outputs); // Convert to an expression tree var expression = tree.ToExpression(); // Compiles the expression var func = expression.Compile(); for (int i = 0; i < inputs.Length; i++) { int y = func(inputs[i].ToDouble()); Assert.AreEqual(outputs[i], y); } }
public void TraverseTest() { DecisionTree tree; int[][] inputs; int[] outputs; ID3LearningTest.CreateMitchellExample(out tree, out inputs, out outputs); { DecisionNode[] expected = { tree.Root, tree.Root.Branches[0], // Outlook = 0 tree.Root.Branches[1], // Outlook = 1 tree.Root.Branches[2], // Outlook = 2 tree.Root.Branches[0].Branches[0], // Humidity = 0 tree.Root.Branches[0].Branches[1], // Humidity = 1 tree.Root.Branches[2].Branches[0], // Wind = 0 tree.Root.Branches[2].Branches[1], // Wind = 1 }; int i = 0; foreach (var node in tree.Traverse(DecisionTreeTraversal.BreadthFirst)) { Assert.AreEqual(expected[i++], node); } Assert.AreEqual(expected.Length, i); } { DecisionNode[] expected = { tree.Root, tree.Root.Branches[0], // Outlook = 0 tree.Root.Branches[0].Branches[0], // Humidity = 0 tree.Root.Branches[0].Branches[1], // Humidity = 1 tree.Root.Branches[1], // Outlook = 1 tree.Root.Branches[2], // Outlook = 2 tree.Root.Branches[2].Branches[0], // Wind = 0 tree.Root.Branches[2].Branches[1], // Wind = 1 }; int i = 0; foreach (var node in tree.Traverse(DecisionTreeTraversal.DepthFirst)) { Assert.AreEqual(expected[i++], node); } Assert.AreEqual(expected.Length, i); } { DecisionNode[] expected = { tree.Root.Branches[0].Branches[0], // Humidity = 0 tree.Root.Branches[0].Branches[1], // Humidity = 1 tree.Root.Branches[0], // Outlook = 0 tree.Root.Branches[1], // Outlook = 1 tree.Root.Branches[2].Branches[0], // Wind = 0 tree.Root.Branches[2].Branches[1], // Wind = 1 tree.Root.Branches[2], // Outlook = 2 tree.Root, }; int i = 0; foreach (var node in tree.Traverse(DecisionTreeTraversal.PostOrder)) { Assert.AreEqual(expected[i++], node); } Assert.AreEqual(expected.Length, i); } }