public void ArgumentCheck1() { int[][] samples = { new [] { 0, 2, 4 }, new [] { 1, 5, 2 }, null, new [] { 1, 5, 6 }, }; int[] outputs = { 1, 1, 0, 0 }; DecisionVariable[] vars = new DecisionVariable[3]; for (int i = 0; i < vars.Length; i++) { vars[i] = DecisionVariable.Discrete(i.ToString(), new IntRange(0, 10)); } DecisionTree tree = new DecisionTree(vars, 2); ID3Learning teacher = new ID3Learning(tree); bool thrown = false; try { double error = teacher.Run(samples, outputs); } catch (ArgumentNullException) { thrown = true; } Assert.IsTrue(thrown); }
public void LargeSampleTest2() { Accord.Math.Tools.SetupGenerator(0); int[][] dataSamples = Matrix.Random(500, 3, 0, 10).ToInt32().ToArray(); int[] target = Matrix.Random(500, 1, 0, 2).ToInt32().GetColumn(0); DecisionVariable[] features = { new DecisionVariable("Outlook", 10), new DecisionVariable("Temperature", 10), new DecisionVariable("Humidity", 10), }; DecisionTree tree = new DecisionTree(features, 2); ID3Learning id3Learning = new ID3Learning(tree) { Rejection = false }; double error = id3Learning.Run(dataSamples, target); foreach (var node in tree) { if (node.IsLeaf) { Assert.IsNotNull(node.Output); } } Assert.IsTrue(error < 0.15); }
public void ConsistencyTest1() { int[,] random = Matrix.Random(1000, 10, 0.0, 10.0).ToInt32(); int[][] samples = random.ToJagged(); int[] outputs = new int[1000]; for (int i = 0; i < samples.Length; i++) { if (samples[i][0] > 8) { outputs[i] = 1; } } DecisionVariable[] vars = new DecisionVariable[10]; for (int i = 0; i < vars.Length; i++) { vars[i] = new DecisionVariable(i.ToString(), new IntRange(0, 10)); } DecisionTree tree = new DecisionTree(vars, 2); ID3Learning teacher = new ID3Learning(tree); double error = teacher.Run(samples, outputs); Assert.AreEqual(0, error); Assert.AreEqual(11, tree.Root.Branches.Count); for (int i = 0; i < tree.Root.Branches.Count; i++) { Assert.IsTrue(tree.Root.Branches[i].IsLeaf); } }
public void LargeSampleTest1() { Accord.Math.Tools.SetupGenerator(0); int[][] dataSamples = Matrix.Random(500, 3, 0.0, 10.0).ToInt32().ToJagged(); int[] target = Matrix.Random(500, 1, 0.0, 2.0).ToInt32().GetColumn(0); DecisionVariable[] features = { new DecisionVariable("Outlook", 10), new DecisionVariable("Temperature", 10), new DecisionVariable("Humidity", 10), }; DecisionTree tree = new DecisionTree(features, 2); ID3Learning id3Learning = new ID3Learning(tree); double error = id3Learning.Run(dataSamples, target); Assert.IsTrue(error < 0.2); var code = tree.ToCode("MyTree"); Assert.IsNotNull(code); Assert.IsTrue(code.Length > 0); }
public static void CreateXORExample(out DecisionTree tree, out int[][] inputs, out int[] outputs) { inputs = new int[][] { new int[] { 1, 0, 0, 1 }, new int[] { 0, 1, 0, 0 }, new int[] { 0, 0, 0, 0 }, new int[] { 1, 1, 0, 0 }, new int[] { 0, 1, 1, 1 }, new int[] { 0, 0, 1, 1 }, new int[] { 1, 0, 1, 1 } }; outputs = new int[] { 1, 1, 0, 0, 1, 0, 1 }; DecisionVariable[] attributes = { new DecisionVariable("a1", 2), new DecisionVariable("a2", 2), new DecisionVariable("a3", 2), new DecisionVariable("a4", 2) }; int classCount = 2; tree = new DecisionTree(attributes, classCount); ID3Learning id3 = new ID3Learning(tree); double error = id3.Run(inputs, outputs); }
private List <productos> revisarProductos(DataTable data) { var codebook = new Codification(data); int numCategorias = db.categorias.Count(); DecisionVariable[] attributes = { new DecisionVariable("Categoria", numCategorias), // 3 possible values (Sunny, overcast, rain) new DecisionVariable("Precio", 5), // 3 possible values (Hot, mild, cool) }; int classCount = 2; // 2 possible output values for playing tennis: yes or no DecisionTree tree = new DecisionTree(attributes, classCount); // Create a new instance of the ID3 algorithm ID3Learning id3learning = new ID3Learning(tree); // Translate our training data into integer symbols using our codebook: DataTable symbols = codebook.Apply(data); int[][] inputs = symbols.ToIntArray("Categoria", "Precio"); int[] outputs = symbols.ToIntArray("recomendar").GetColumn(0); // Learn the training instances! id3learning.Run(inputs, outputs); // Compute the training error when predicting training instances double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs)); // The tree can now be queried for new examples through // its decide method. For example, we can create a query List <productos> product = db.productos.ToList(); foreach (productos item in db.productos.ToList()) { int[] query = codebook.Transform(new[, ] { { "Categoria", Convert.ToString(item.fkCategoria) }, { "Precio", devolverTipoPrecio(item.precio) } }); // And then predict the label using int predicted = tree.Decide(query); // result will be 0 // We can translate it back to strings using string answer = codebook.Revert("recomendar", predicted); // Answer will be: "No" if (answer.Equals("no")) { product.Remove(item); } } return(product); }
private ColoredTree <string, string> UpdateTree() { ID3Learning id3 = new ID3Learning(); id3.Initialize(learning.DataCell); ColoredTree <string, string> decisionTree = id3.Run(learning.Dataset, learning.Target); this.SerializeTree(decisionTree); this.OnTreeUpdated(); return(decisionTree); }
// //You can use the following additional attributes as you write your tests: // //Use ClassInitialize to run code before running the first test in the class //[ClassInitialize()] //public static void MyClassInitialize(TestContext testContext) //{ //} // //Use ClassCleanup to run code after all tests in a class have run //[ClassCleanup()] //public static void MyClassCleanup() //{ //} // //Use TestInitialize to run code before running each test //[TestInitialize()] //public void MyTestInitialize() //{ //} // //Use TestCleanup to run code after each test has run //[TestCleanup()] //public void MyTestCleanup() //{ //} // #endregion public static void CreateMitchellExample(out DecisionTree tree, out int[][] inputs, out int[] outputs) { DataTable data = new DataTable("Mitchell's Tennis Example"); data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis"); data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No"); data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No"); data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes"); data.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes"); data.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No"); data.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes"); data.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No"); data.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes"); data.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes"); data.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes"); data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes"); data.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No"); // Create a new codification codebook to // convert strings into integer symbols Codification codebook = new Codification(data); DecisionVariable[] attributes = { new DecisionVariable("Outlook", codebook["Outlook"].Symbols), // 3 possible values (Sunny, overcast, rain) new DecisionVariable("Temperature", codebook["Temperature"].Symbols), // 3 possible values (Hot, mild, cool) new DecisionVariable("Humidity", codebook["Humidity"].Symbols), // 2 possible values (High, normal) new DecisionVariable("Wind", codebook["Wind"].Symbols) // 2 possible values (Weak, strong) }; int classCount = codebook["PlayTennis"].Symbols; // 2 possible values (yes, no) tree = new DecisionTree(attributes, classCount); ID3Learning id3 = new ID3Learning(tree); // Extract symbols from data and train the classifier DataTable symbols = codebook.Apply(data); inputs = symbols.ToIntArray("Outlook", "Temperature", "Humidity", "Wind"); outputs = symbols.ToIntArray("PlayTennis").GetColumn(0); id3.Run(inputs, outputs); }
public void LargeRunTest2() { Accord.Math.Random.Generator.Seed = 0; int[,] random = Matrix.Random(1000, 10, 0.0, 10.0).ToInt32(); int[][] samples = random.ToJagged(); int[] outputs = new int[1000]; for (int i = 0; i < samples.Length; i++) { if (samples[i][0] > 5 || Tools.Random.NextDouble() > 0.85) { outputs[i] = 1; } } DecisionVariable[] vars = new DecisionVariable[10]; for (int i = 0; i < vars.Length; i++) { vars[i] = new DecisionVariable("x" + i, 10); } DecisionTree tree = new DecisionTree(vars, 2); var teacher = new ID3Learning(tree); double error = teacher.Run(samples, outputs); Assert.AreEqual(0, error); var rules = DecisionSet.FromDecisionTree(tree); Simplification simpl = new Simplification(rules) { Alpha = 0.05 }; error = simpl.ComputeError(samples.ToDouble(), outputs); Assert.AreEqual(0, error); double newError = simpl.Compute(samples.ToDouble(), outputs); Assert.AreEqual(0.097, newError); }
public void LargeSampleTest_WithRepetition() { Accord.Math.Random.Generator.Seed = 0; int[][] dataSamples = Matrix.Random(500, 3, 0, 10).ToJagged(); int[] target = Matrix.Random(500, 1, 0, 2).GetColumn(0); DecisionVariable[] features = { new DecisionVariable("Outlook", 10), new DecisionVariable("Temperature", 10), new DecisionVariable("Humidity", 10), }; DecisionTree tree = new DecisionTree(features, 2); ID3Learning id3Learning = new ID3Learning(tree) { Rejection = false, Join = 2 // every variable can join two times }; double error = id3Learning.Run(dataSamples, target); int height = tree.GetHeight(); Assert.AreEqual(6, height); foreach (var node in tree) { if (node.IsLeaf) { Assert.IsNotNull(node.Output); } } Assert.IsTrue(error < 0.15); }
public string kararAgaci(DataTable tbl) { int classCount = 2; Codification codebook = new Codification(tbl); DecisionVariable[] attributes = { new DecisionVariable("Clump Thickness", 10), new DecisionVariable("Uniformity of Cell Size", 10),new DecisionVariable("Uniformity of Cell Shape", 10), new DecisionVariable("Marginal Adhesion", 10),new DecisionVariable("Single Epithelial Cell Size", 10), new DecisionVariable("Bare Nuclei", 10),new DecisionVariable("Bland Chromatin", 10), new DecisionVariable("Normal Nucleoli", 10),new DecisionVariable("Mitoses", 10), }; DecisionTree tree = new DecisionTree(attributes, classCount); ID3Learning id3learning = new ID3Learning(tree); // Translate our training data into integer symbols using our codebook: DataTable symbols = codebook.Apply(tbl); int[][] inputs = symbols.ToIntArray("Clump Thickness", "Uniformity of Cell Size", "Uniformity of Cell Shape", "Marginal Adhesion", "Single Epithelial Cell Size", "Bare Nuclei", "Bland Chromatin", "Normal Nucleoli", "Mitoses"); int[] outputs = symbols.ToIntArray("Class").GetColumn(0); // symbols. id3learning.Run(inputs, outputs); int[] query = codebook.Translate(inputlar[0], inputlar[1], inputlar[2], inputlar[3], inputlar[4], inputlar[5], inputlar[6], inputlar[7], inputlar[8]); int output = tree.Compute(query); string answer = codebook.Translate("Class", output); return(answer); }
public static void Exemplo01() { //LINK: http://accord-framework.net/docs/html/T_Accord_MachineLearning_DecisionTrees_Learning_ID3Learning.htm //LINK: http://accord-framework.net/docs/html/T_Accord_MachineLearning_DecisionTrees_DecisionTree.htm DataTable data = new DataTable("Mitchell's Tennis Example"); data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis"); data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No"); data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No"); data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes"); data.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes"); data.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No"); data.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes"); data.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No"); data.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes"); data.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes"); data.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes"); data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes"); data.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No"); // Create a new codification codebook to // convert strings into integer symbols Codification codebook = new Codification(data, "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis"); // Translate our training data into integer symbols using our codebook: DataTable symbols = codebook.Apply(data); int[][] inputs = symbols.ToArray <int>("Outlook", "Temperature", "Humidity", "Wind"); int[] outputs = symbols.ToArray <int>("PlayTennis"); // Gather information about decision variables DecisionVariable[] attributes = { new DecisionVariable("Outlook", 3), // 3 possible values (Sunny, overcast, rain) new DecisionVariable("Temperature", 3), // 3 possible values (Hot, mild, cool) new DecisionVariable("Humidity", 2), // 2 possible values (High, normal) new DecisionVariable("Wind", 2) // 2 possible values (Weak, strong) }; int classCount = 2; // 2 possible output values for playing tennis: yes or no //Create the decision tree using the attributes and classes DecisionTree tree = new DecisionTree(attributes, classCount); //Create a new instance of the ID3 algorithm ID3Learning id3learning = new ID3Learning(tree); // Learn the training instances! id3learning.Run(inputs, outputs); //Suggest you read the example in the guide carefully. At the very end of the procedure they generate the expression tree with var expression = tree.ToExpression(); and compile it: var expression = tree.ToExpression(); var func = expression.Compile(); //The result is a delegate that you can simply execute to get a decision for a given input.In the example, you could do something like bool willPlayTennis = func(new double[] { 1.0, 1.0, 1.0, 1.0 }) == 1; int[] query = codebook.Translate("Sunny", "Hot", "High", "Strong"); int output = tree.Decide(query); string answer = codebook.Translate("PlayTennis", output); Console.WriteLine(answer); Console.ReadLine(); //RESULT: //In the above example, answer will be "No". }
public void Run() { DataTable data = new DataTable("Mitchell's Tennis Example"); data.Columns.Add("Day"); data.Columns.Add("Outlook"); data.Columns.Add("Temperature"); data.Columns.Add("Humidity"); data.Columns.Add("Wind"); data.Columns.Add("PlayTennis"); data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No"); data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No"); data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes"); data.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes"); data.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No"); data.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes"); data.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No"); data.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes"); data.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes"); data.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes"); data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes"); data.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No"); // Create a new codification codebook to // convert strings into integer symbols Codification codebook = new Codification(data, "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis"); // Translate our training data into integer symbols using our codebook: DataTable symbols = codebook.Apply(data); CreateDic("Outlook", symbols); CreateDic("Temperature", symbols); CreateDic("Humidity", symbols); CreateDic("Wind", symbols); CreateDic("PlayTennis", symbols); int[][] inputs = (from p in symbols.AsEnumerable() select new int[] { GetIndex("Outlook", p["Outlook"].ToString()), GetIndex("Temperature", p["Temperature"].ToString()), GetIndex("Humidity", p["Humidity"].ToString()), GetIndex("Wind", p["Wind"].ToString()) }).Cast <int[]>().ToArray(); int[] outputs = (from p in symbols.AsEnumerable() select GetIndex("PlayTennis", p["PlayTennis"].ToString())).Cast <int>().ToArray(); /* * // Gather information about decision variables * DecisionVariable[] attributes = * { * new DecisionVariable("Outlook", 3), // 3 possible values (Sunny, overcast, rain) * new DecisionVariable("Temperature", 3), // 3 possible values (Hot, mild, cool) * new DecisionVariable("Humidity", 2), // 2 possible values (High, normal) * new DecisionVariable("Wind", 2) // 2 possible values (Weak, strong) * }; * */ DecisionVariable[] attributes = { new DecisionVariable("Outlook", GetCount("Outlook")), // 3 possible values (Sunny, overcast, rain) new DecisionVariable("Temperature", GetCount("Temperature")), // 3 possible values (Hot, mild, cool) new DecisionVariable("Humidity", GetCount("Humidity")), // 2 possible values (High, normal) new DecisionVariable("Wind", GetCount("Wind")) // 2 possible values (Weak, strong) }; int classCount = GetCount("PlayTennis"); // 2 possible output values for playing tennis: yes or no //Create the decision tree using the attributes and classes DecisionTree tree = new DecisionTree(attributes, classCount); // Create a new instance of the ID3 algorithm ID3Learning id3learning = new ID3Learning(tree); // Learn the training instances! id3learning.Run(inputs, outputs); string answer = codebook.Translate("PlayTennis", tree.Compute(codebook.Translate("Sunny", "Hot", "High", "Strong"))); Console.WriteLine("Calculate for: Sunny, Hot, High, Strong"); Console.WriteLine("Answer: " + answer); var expression = tree.ToExpression(); Console.WriteLine(tree.ToCode("ClassTest")); DecisionSet s = tree.ToRules(); Console.WriteLine(s.ToString()); // Compiles the expression to IL var func = expression.Compile(); }
private void button2_Click(object sender, EventArgs e) { c1 = 0; c2 = 0; c3 = 0; c4 = 0; c5 = 0; groupBox4.Enabled = true; codebook = new Codification(data); DataTable symbols = codebook.Apply(data); inputs = symbols.ToArray <int>("Outlook", "Temp", "Humid", "Wind"); outputs = symbols.ToArray <int>("Tennis"); row = dataGridView1.RowCount; n1 = new String[row]; n2 = new String[row]; n3 = new String[row]; n4 = new String[row]; n5 = new String[row]; for (int i = 0; i < row; i++) { n1[i] = Convert.ToString(dataGridView1.Rows[i].Cells[1].Value); n2[i] = Convert.ToString(dataGridView1.Rows[i].Cells[2].Value); n3[i] = Convert.ToString(dataGridView1.Rows[i].Cells[3].Value); n4[i] = Convert.ToString(dataGridView1.Rows[i].Cells[4].Value); n5[i] = Convert.ToString(dataGridView1.Rows[i].Cells[5].Value); } bn1 = n1.Distinct(); bn2 = n2.Distinct(); bn3 = n3.Distinct(); bn4 = n4.Distinct(); bn5 = n5.Distinct(); foreach (String i in bn1) { c1 = c1 + 1; } foreach (String i in bn2) { c2 = c2 + 1; } foreach (String i in bn3) { c3 = c3 + 1; } foreach (String i in bn4) { c4 = c4 + 1; } foreach (String i in bn5) { c5 = c5 + 1; } DecisionVariable[] attributes = { new DecisionVariable("Outlook", 3), new DecisionVariable("Temp", 3), new DecisionVariable("Humid", 2), new DecisionVariable("Wind", 2) }; int classCount = 2; tree = new DecisionTree(attributes, classCount); ID3Learning id3learning = new ID3Learning(tree); id3learning.Run(inputs, outputs); inputs = null; outputs = null; myTree = new TreeBuilder(GetTreeData()); myTree.BoxHeight = 30; myTree.BoxWidth = 90; myTree.VerticalSpace = 60; pict.Image = Image.FromStream(myTree.GenerateTree(-1, -1, "1", System.Drawing.Imaging.ImageFormat.Bmp)); }
static void Main(string[] args) { DataTable data = new DataTable("Should I Go To Work For Company X"); data.Columns.Add("Scenario"); data.Columns.Add("Pay"); data.Columns.Add("Benefits"); data.Columns.Add("Culture"); data.Columns.Add("WorkFromHome"); data.Columns.Add("ShouldITakeJob"); data.Rows.Add("D1", "Good", "Good", "Mean", "Yes", "Yes"); data.Rows.Add("D2", "Good", "Good", "Mean", "No", "Yes"); data.Rows.Add("D3", "Average", "Good", "Good", "Yes", "Yes"); data.Rows.Add("D4", "Average", "Good", "Good", "No", "Yes"); data.Rows.Add("D5", "Bad", "Good", "Good", "Yes", "No"); data.Rows.Add("D6", "Bad", "Good", "Good", "No", "No"); data.Rows.Add("D7", "Good", "Average", "Mean", "Yes", "Yes"); data.Rows.Add("D8", "Good", "Average", "Mean", "No", "Yes"); data.Rows.Add("D9", "Average", "Average", "Good", "Yes", "No"); data.Rows.Add("D10", "Average", "Average", "Good", "No", "No"); data.Rows.Add("D11", "Bad", "Average", "Good", "Yes", "No"); data.Rows.Add("D12", "Bad", "Average", "Good", "No", "No"); data.Rows.Add("D13", "Good", "Bad", "Mean", "Yes", "Yes"); data.Rows.Add("D14", "Good", "Bad", "Mean", "No", "Yes"); data.Rows.Add("D15", "Average", "Bad", "Good", "Yes", "No"); data.Rows.Add("D16", "Average", "Bad", "Good", "No", "No"); data.Rows.Add("D17", "Bad", "Bad", "Good", "Yes", "No"); data.Rows.Add("D18", "Bad", "Bad", "Good", "No", "No"); data.Rows.Add("D19", "Good", "Good", "Good", "Yes", "Yes"); data.Rows.Add("D20", "Good", "Good", "Good", "No", "Yes"); // Create a new codification codebook to // convert strings into integer symbols Codification codebook = new Codification(data); DecisionVariable[] attributes = { new DecisionVariable("Pay", 3), new DecisionVariable("Benefits", 3), new DecisionVariable("Culture", 3), new DecisionVariable("WorkFromHome", 2) }; int outputValues = 2; // 2 possible output values: yes or no DecisionTree tree = new DecisionTree(attributes, outputValues); ID3Learning id3 = new ID3Learning(tree); #pragma warning disable CS0618 // Type or member is obsolete // Translate our training data into integer symbols using our codebook: DataTable symbols = codebook.Apply(data); int[][] inputs = symbols.ToArray <int>("Pay", "Benefits", $"Culture", "WorkFromHome"); int[] outputs = symbols.ToIntArray("ShouldITakeJob").GetColumn(0); // Learn the training instances! id3.Run(inputs, outputs); int[] query = codebook.Translate("D19", "Good", "Good", "Good", "Yes"); int output = tree.Compute(query); string answer = codebook.Translate("ShouldITakeJob", output); // answer will be "Yes". #pragma warning restore CS0618 // Type or member is obsolete Console.WriteLine("Answer is: " + answer); Console.ReadKey(); }
public void IncompleteDiscreteVariableTest() { DecisionTree tree; int[][] inputs; int[] outputs; DataTable data = new DataTable("Degenerated Tennis Example"); data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis"); data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No"); data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No"); data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes"); data.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes"); data.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No"); data.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes"); data.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No"); data.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes"); data.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes"); data.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes"); data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes"); data.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No"); // Create a new codification codebook to // convert strings into integer symbols Codification codebook = new Codification(data); DecisionVariable[] attributes = { new DecisionVariable("Outlook", codebook["Outlook"].Symbols + 200), // 203 possible values, 200 undefined new DecisionVariable("Temperature", codebook["Temperature"].Symbols), // 3 possible values (Hot, mild, cool) new DecisionVariable("Humidity", codebook["Humidity"].Symbols), // 2 possible values (High, normal) new DecisionVariable("Wind", codebook["Wind"].Symbols) // 2 possible values (Weak, strong) }; int classCount = codebook["PlayTennis"].Symbols; // 2 possible values (yes, no) tree = new DecisionTree(attributes, classCount); ID3Learning id3 = new ID3Learning(tree); // Extract symbols from data and train the classifier DataTable symbols = codebook.Apply(data); inputs = symbols.ToArray <int>("Outlook", "Temperature", "Humidity", "Wind"); outputs = symbols.ToArray <int>("PlayTennis"); double error = id3.Run(inputs, outputs); Assert.AreEqual(0, error); Assert.AreEqual(203, tree.Root.Branches.Count); Assert.IsTrue(tree.Root.Branches[100].IsLeaf); Assert.IsNull(tree.Root.Branches[100].Output); for (int i = 0; i < inputs.Length; i++) { int y = tree.Compute(inputs[i]); Assert.AreEqual(outputs[i], y); } }
public static void CreateMitchellExample(out DecisionTree tree, out int[][] inputs, out int[] outputs) { DataTable data = new DataTable("Mitchell's Tennis Example"); data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis"); data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No"); data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No"); data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes"); data.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes"); data.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No"); data.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes"); data.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No"); data.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes"); data.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes"); data.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes"); data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes"); data.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No"); // Create a new codification codebook to // convert strings into integer symbols Codification codebook = new Codification(data, "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis"); DecisionVariable[] attributes = { new DecisionVariable("Outlook", codebook["Outlook"].Symbols), // 3 possible values (Sunny, overcast, rain) new DecisionVariable("Temperature", codebook["Temperature"].Symbols), // 3 possible values (Hot, mild, cool) new DecisionVariable("Humidity", codebook["Humidity"].Symbols), // 2 possible values (High, normal) new DecisionVariable("Wind", codebook["Wind"].Symbols) // 2 possible values (Weak, strong) }; int classCount = codebook["PlayTennis"].Symbols; // 2 possible values (yes, no) tree = new DecisionTree(attributes, classCount); ID3Learning id3 = new ID3Learning(tree); // Extract symbols from data and train the classifier DataTable symbols = codebook.Apply(data); inputs = symbols.ToArray <int>("Outlook", "Temperature", "Humidity", "Wind"); outputs = symbols.ToArray <int>("PlayTennis"); double error = id3.Run(inputs, outputs); Assert.AreEqual(0, error); { int[] query = codebook.Translate("Sunny", "Hot", "High", "Strong"); int output = tree.Compute(query); string answer = codebook.Translate("PlayTennis", output); Assert.AreEqual("No", answer); } foreach (DataRow row in data.Rows) { var x = codebook.Translate(row, "Outlook", "Temperature", "Humidity", "Wind"); int y = tree.Compute(x); string actual = codebook.Translate("PlayTennis", y); string expected = row["PlayTennis"] as string; Assert.AreEqual(expected, actual); } { string answer = codebook.Translate("PlayTennis", tree.Compute(codebook.Translate("Sunny", "Hot", "High", "Strong"))); Assert.AreEqual("No", answer); } }
public void RunTest() { int[][] inputs = { new int[] { 0, 0 }, new int[] { 0, 1 }, new int[] { 1, 0 }, new int[] { 1, 1 }, }; int[] outputs = // xor { 0, 1, 1, 0 }; DecisionVariable[] attributes = { new DecisionVariable("x", DecisionVariableKind.Discrete), new DecisionVariable("y", DecisionVariableKind.Discrete), }; DecisionTree tree = new DecisionTree(attributes, 2); ID3Learning teacher = new ID3Learning(tree); double error = teacher.Run(inputs, outputs); Assert.AreEqual(0, error); Assert.AreEqual(0, tree.Root.Branches.AttributeIndex); // x Assert.AreEqual(2, tree.Root.Branches.Count); Assert.IsNull(tree.Root.Value); Assert.IsNull(tree.Root.Value); Assert.AreEqual(0.0, tree.Root.Branches[0].Value); // x = [0] Assert.AreEqual(1.0, tree.Root.Branches[1].Value); // x = [1] Assert.AreEqual(tree.Root, tree.Root.Branches[0].Parent); Assert.AreEqual(tree.Root, tree.Root.Branches[1].Parent); Assert.AreEqual(2, tree.Root.Branches[0].Branches.Count); Assert.AreEqual(2, tree.Root.Branches[1].Branches.Count); Assert.IsTrue(tree.Root.Branches[0].Branches[0].IsLeaf); Assert.IsTrue(tree.Root.Branches[0].Branches[1].IsLeaf); Assert.IsTrue(tree.Root.Branches[1].Branches[0].IsLeaf); Assert.IsTrue(tree.Root.Branches[1].Branches[1].IsLeaf); Assert.AreEqual(0.0, tree.Root.Branches[0].Branches[0].Value); // y = [0] Assert.AreEqual(1.0, tree.Root.Branches[0].Branches[1].Value); // y = [1] Assert.AreEqual(0.0, tree.Root.Branches[1].Branches[0].Value); // y = [0] Assert.AreEqual(1.0, tree.Root.Branches[1].Branches[1].Value); // y = [1] Assert.AreEqual(0, tree.Root.Branches[0].Branches[0].Output); // 0 ^ 0 = 0 Assert.AreEqual(1, tree.Root.Branches[0].Branches[1].Output); // 0 ^ 1 = 1 Assert.AreEqual(1, tree.Root.Branches[1].Branches[0].Output); // 1 ^ 0 = 1 Assert.AreEqual(0, tree.Root.Branches[1].Branches[1].Output); // 1 ^ 1 = 0 }
public void ConstantDiscreteVariableTest() { DecisionTree tree; int[][] inputs; int[] outputs; DataTable data = new DataTable("Degenerated Tennis Example"); data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis"); data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No"); data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No"); data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes"); data.Rows.Add("D4", "Rain", "Hot", "High", "Weak", "Yes"); data.Rows.Add("D5", "Rain", "Hot", "Normal", "Weak", "Yes"); data.Rows.Add("D6", "Rain", "Hot", "Normal", "Strong", "No"); data.Rows.Add("D7", "Overcast", "Hot", "Normal", "Strong", "Yes"); data.Rows.Add("D8", "Sunny", "Hot", "High", "Weak", "No"); data.Rows.Add("D9", "Sunny", "Hot", "Normal", "Weak", "Yes"); data.Rows.Add("D10", "Rain", "Hot", "Normal", "Weak", "Yes"); data.Rows.Add("D11", "Sunny", "Hot", "Normal", "Strong", "Yes"); data.Rows.Add("D12", "Overcast", "Hot", "High", "Strong", "Yes"); data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes"); data.Rows.Add("D14", "Rain", "Hot", "High", "Strong", "No"); // Create a new codification codebook to // convert strings into integer symbols Codification codebook = new Codification(data); DecisionVariable[] attributes = { new DecisionVariable("Outlook", codebook["Outlook"].Symbols), // 3 possible values (Sunny, overcast, rain) new DecisionVariable("Temperature", codebook["Temperature"].Symbols), // 1 constant value (Hot) new DecisionVariable("Humidity", codebook["Humidity"].Symbols), // 2 possible values (High, normal) new DecisionVariable("Wind", codebook["Wind"].Symbols) // 2 possible values (Weak, strong) }; int classCount = codebook["PlayTennis"].Symbols; // 2 possible values (yes, no) bool thrown = false; try { tree = new DecisionTree(attributes, classCount); } catch { thrown = true; } Assert.IsTrue(thrown); attributes[1] = new DecisionVariable("Temperature", 2); tree = new DecisionTree(attributes, classCount); ID3Learning id3 = new ID3Learning(tree); // Extract symbols from data and train the classifier DataTable symbols = codebook.Apply(data); inputs = symbols.ToJagged <int>("Outlook", "Temperature", "Humidity", "Wind"); outputs = symbols.ToArray <int>("PlayTennis"); double error = id3.Run(inputs, outputs); Assert.AreEqual(0, error); for (int i = 0; i < inputs.Length; i++) { int y = tree.Compute(inputs[i]); Assert.AreEqual(outputs[i], y); } }
public static void TestAccord() { /* * http://crsouza.com/2012/01/decision-trees-in-c/ * */ DataTable data = new DataTable("Memory"); /*add People names/ID to columns dynamically*/ data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis"); /*possibly add sentences to this? * maybe keywords*/ data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No"); data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No"); data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No"); data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes"); data.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes"); data.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No"); data.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes"); data.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No"); data.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes"); data.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes"); data.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes"); data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes"); data.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No"); // Create a new codification codebook to // convert strings into integer symbols Codification codebook = new Codification(data, "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis"); /* NO IDEA FOR THIS */ DecisionVariable[] attributes = { new DecisionVariable("Outlook", 3), // 3 possible values (Sunny, overcast, rain) new DecisionVariable("Temperature", 3), // 3 possible values (Hot, mild, cool) new DecisionVariable("Humidity", 2), // 2 possible values (High, normal) new DecisionVariable("Wind", 2) // 2 possible values (Weak, strong) }; /* For possible values, make it one so it narrows to one individual fact about a word*/ int classCount = 2; // 2 possible output values for playing tennis: yes or no DecisionTree tree = new DecisionTree(attributes, classCount); // Create a new instance of the ID3 algorithm ID3Learning id3learning = new ID3Learning(tree); // Translate our training data into integer symbols using our codebook: DataTable symbols = codebook.Apply(data); int[][] inputs = symbols.ToArray <int>("Outlook", "Temperature", "Humidity", "Wind"); int[] outputs = symbols.ToIntArray("PlayTennis").GetColumn(0); // Learn the training instances! id3learning.Run(inputs, outputs); /*This is how we will query the memory*/ int[] query = codebook.Translate("Sunny", "Hot", "High", "Strong"); int output = tree.Compute(query); /*Respond to user*/ string answer = codebook.Translate("PlayTennis", output); // answer will be "No". Console.WriteLine(answer); }
public Data.DecisionTree runDecisionSupport(int codTask, int totalExecutions, int classCount) { Data.DecisionTree decisionTree = new Data.DecisionTree(); // Utilizado se já existe uma árvore de decisão prévia criada, serve para reutilização de alguns parâmetros Data.DecisionTree serializedTree = null; // Verifica se já existe uma árvore gerada anteriormente bool hasSerializedTree = decisionTree.Serialization.hasSerializedTree(codTask); // Carregar a árvore existente if (hasSerializedTree) { serializedTree = decisionTree.deserializeTree(codTask); } // Número total de execuções da tarefa //int totalExecutions = getTotalExecutions(codTask); Dictionary <dynamic, int> fieldSymbols = new Dictionary <dynamic, int>(); // Preenche o dicionário com a lista de campos do processo e a quantidade de símbolos para cada campo fillSymbolsCount(codTask, ref fieldSymbols); // Preenche a relevância dos campos de acordo com a taxa de variação e de nulos setSymbolsRelevance(ref fieldSymbols, totalExecutions, codTask); StringBuilder CodFieldListSB = new StringBuilder(); StringBuilder CodFieldListSBIsNull = new StringBuilder(); StringBuilder DsFieldNameList = new StringBuilder(); List <string> codFieldList = new List <string>(); // Prepara lista de campos List <string> codFieldListComDsFlowResult = prepareFieldList(ref fieldSymbols, ref CodFieldListSB, ref CodFieldListSBIsNull, ref DsFieldNameList, ref codFieldList); // Consulta os dados de treino DataTable data = getData(codTask, CodFieldListSB, CodFieldListSBIsNull); decisionTree.Data = data; // Criando o objeto TRAINING decisionTree.Training.setTrainingData(data); // Preenchendo o VALIDATION do DecisionTree decisionTree.Validation.setValidationData(data); // ############################################# Passado para a classe Training. // Passando o data para criação do codebook // Converte em números inteiros as strings Codification codebook = new Codification(data, codFieldListComDsFlowResult.ToArray()); // Montando a tabela de variáveis de decisão List <DecisionVariable> decisionaVariableList = new List <DecisionVariable>(); foreach (KeyValuePair <dynamic, int> entry in fieldSymbols.Where(p => ((DecisionSupportField)p.Key).relevante == true)) { DecisionSupportField c = entry.Key; c.simbolos = entry.Value; decisionaVariableList.Add(new DecisionVariable(c.codigo.ToString(), c.simbolos + 1)); // Adicionando + 1 do possível nulo } int qtdCamposRelevantes = fieldSymbols.Count(i => ((DecisionSupportField)i.Key).relevante == true); DecisionVariable[] attributes = new DecisionVariable[qtdCamposRelevantes]; // Tabela de variáveis que impactam na decisão attributes = decisionaVariableList.ToArray(); //List<string> DsFlowResults = getTaskResults(codTask); // Número de possíveis resultados para a tarefa sendo analisada //int classCount = DsFlowResults.Count; // Cria a árvore de decisão Accord.MachineLearning.DecisionTrees.DecisionTree tree = new Accord.MachineLearning.DecisionTrees.DecisionTree(attributes, classCount); // Cria uma instância do algoritmo de aprendizado utilizado, o ID3 ID3Learning id3learning = new ID3Learning(tree); // Traduz os dados de treino em simbolos inteiros utilizando o codebook DataTable symbols = codebook.Apply(decisionTree.Training.TrainingData); // Colunas de entrada // *** Quantidade de colunas dos inputs deve ser o mesmo número de DecisionVariables(attributes) int[][] inputs = symbols.ToArray <int>(codFieldList.ToArray()); // Coluna com a saída int[] outputs = symbols.ToArray <int>("DsFlowResult"); // Aprendendo com as instâncias de treino id3learning.Run(inputs, outputs); // ############################################# Passado para a classe Training. decisionTree.Tree = tree; // Atribuindo as listas de códigos de campos e nomes ao objeto decisionTree // Utilizado para manter o uso da palavra chave 'ref' decisionTree.CodFieldListSB = CodFieldListSB; decisionTree.CodFieldListSBIsNull = CodFieldListSBIsNull; decisionTree.DsFieldNameList = DsFieldNameList; decisionTree.codFieldList = codFieldList; decisionTree.codFieldListComDsFlowResult = codFieldListComDsFlowResult; decisionTree.fieldSymbols = fieldSymbols; decisionTree.Codebook = codebook; decisionTree.serializeTree(codTask); return(decisionTree); }
public void Run(String filename) { ReadFile(filename); // Create a new codification codebook to // convert strings into integer symbols Codification codebook = new Codification(data, inputColumns.ToArray()); // Translate our training data into integer symbols using our codebook: DataTable symbols = codebook.Apply(data); foreach (String s in inputColumns) { CreateDic(s, symbols); } CreateDic(outputColumn, symbols); int[][] inputs = (from p in symbols.AsEnumerable() select GetInputRow(p) ).Cast <int[]>().ToArray(); int[] outputs = (from p in symbols.AsEnumerable() select GetIndex(outputColumn, p[outputColumn].ToString())).Cast <int>().ToArray(); // Gather information about decision variables DecisionVariable[] attributes = GetDecisionVariables(); int classCount = GetCount(outputColumn); // 2 possible output values for playing tennis: yes or no //Create the decision tree using the attributes and classes DecisionTree tree = new DecisionTree(attributes, classCount); // Create a new instance of the ID3 algorithm ID3Learning id3learning = new ID3Learning(tree); //C45Learning c45learning = new C45Learning(tree); // Learn the training instances! id3learning.Run(inputs, outputs); //c45learning.Run(inputs2, outputs); /* * string answer = codebook.Translate(outputColumn, * tree.Compute(codebook.Translate("Sunny", "Hot", "High", "Strong"))); * * Console.WriteLine("Calculate for: Sunny, Hot, High, Strong"); * Console.WriteLine("Answer: " + answer); */ var expression = tree.ToExpression(); Console.WriteLine(tree.ToCode("ClassTest")); DecisionSet rules = tree.ToRules(); Console.WriteLine(rules.ToString()); // Compiles the expression to IL var func = expression.Compile(); }