示例#1
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);
        }
示例#2
0
        public void not_seen_before_test()
        {
            // DecisionTree chokes on variable values it has never seen before #689
            int[][] training =
            {
                new [] { 0, 2, 4 },
                new [] { 1, 5, 2 },
                new [] { 1, 5, 6 },
            };

            int[][] testing =
            {
                new [] { 99,  2,  4 },
                new [] {  1,  5, 17 },
                new [] {  1, 15,  6 },
            };

            int[] outputs =
            {
                1, 1, 0
            };

            ID3Learning teacher = new ID3Learning();

            DecisionTree tree = teacher.Learn(training, outputs);

            int[] train = tree.Decide(training);
            int[] test  = tree.Decide(testing);

            Assert.IsTrue(train.IsEqual(new int[] { 1, 1, 0 }));
            Assert.IsTrue(test.IsEqual(new int[] { 1, 0, 0 }));
        }
示例#3
0
        private static void simpleDecisionTreeExample()
        {
            // In this example, we will learn a decision tree directly from integer
            // matrices that define the inputs and outputs of our learning problem.
            // XOR = https://en.wikipedia.org/wiki/Exclusive_or
            int[][] inputs =
            {
                new int[] { 0, 0 },
                new int[] { 0, 1 },
                new int[] { 1, 0 },
                new int[] { 1, 1 },
            };

            int[] outputs = // xor between inputs[0] and inputs[1]
            {
                0, 1, 1, 0
            };

            // Create an ID3 learning algorithm
            ID3Learning teacher = new ID3Learning();

            // Learn a decision tree for the XOR problem
            var tree = teacher.Learn(inputs, outputs);

            var vrTestResult = tree.Decide(inputs);

            // Compute the error in the learning
            double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs));

            // The tree can now be queried for new examples:
            int[] predicted = tree.Decide(inputs); // should be { 0, 1, 1, 0 }
        }
示例#4
0
        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);
        }
示例#5
0
        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);
        }
示例#6
0
        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);
            }
        }
示例#7
0
        public void ConsistencyTest1_automatic()
        {
            int n = 10000;

            int[,] random = Matrix.Random(n, 10, 0.0, 11.0).ToInt32();

            int[][] samples = random.ToJagged();
            int[]   outputs = new int[n];

            for (int i = 0; i < samples.Length; i++)
            {
                if (samples[i][0] > 8)
                {
                    outputs[i] = 1;
                }
            }

            ID3Learning teacher = new ID3Learning();

            var tree = teacher.Learn(samples, outputs);

            double error = teacher.ComputeError(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);
            }
        }
示例#8
0
        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);
        }
示例#9
0
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();

            sw.Start();
            Console.WriteLine("parsing...");
            List <Flight> flights = Flight.ParseFlights();

            sw.Stop();
            Console.WriteLine("done, time taken: " + sw.Elapsed.Seconds + " seconds");

            /*
             * 1,000,000 samples = ~5 min and 6.5GB RAM (too much for my system)    91% accuracy
             * 750,000 samples   = ~1:30 total and about 5GB RAM (much more doable) 93% accuracy
             */
            Flight.FlightsToArrays(flights, 750000);

            ID3Learning teacher = new ID3Learning();

            Console.WriteLine("training...");
            sw.Restart();
            var tree = teacher.Learn(Flight.FieldsArray, Flight.OutputArray);

            sw.Stop();
            Console.WriteLine("done, time taken: " + sw.Elapsed.Seconds + " seconds");

            double error = new ZeroOneLoss(Flight.OutputArray).Loss(tree.Decide(Flight.FieldsArray));

            Console.WriteLine("accuracy on training set: " + ((1 - error) * 100) + "%");

            Console.ReadKey();
        }
        public void Learn(List <BaseAttribute <T> > attributeColumns, BaseAttribute <T> outputAttributeColumn)
        {
            // Create a new instance of the ID3 algorithm
            ID3Learning id3learning = new ID3Learning(DecisionTree);

            // Translate our training data into integer symbols using our codebook:
            DataTable symbols = Codebook.Apply(Data);

            var columnNames = attributeColumns.Select(attribute => attribute.Name).ToArray();

            int[][] inputs  = symbols.ToJagged <int>(columnNames);
            int[]   outputs = symbols.ToJagged <int>(outputAttributeColumn.Name).GetColumn(0);

            // Learn the training instances!
            DecisionTree = id3learning.Learn(inputs, outputs);

            double error = new ZeroOneLoss(outputs).Loss(DecisionTree.Decide(inputs));

            Debug.WriteLine(error);

            // Moreover, we may decide to convert our tree to a set of rules:
            DecisionSet rules = DecisionTree.ToRules();
            // And using the codebook, we can inspect the tree reasoning:
            string ruleText = rules.ToString(Codebook as Codification <string>, outputAttributeColumn.Name,
                                             System.Globalization.CultureInfo.InvariantCulture);

            Debug.WriteLine(ruleText);
        }
示例#11
0
        public string getRecommendationsByUsers(string id = "user4")
        {
            DataTable data = new DataTable("dataTable");

            PopulateHead(data);
            PopulateTable(data, id);

            Codification codification = new Codification(data);
            DataTable    codifiedData = codification.Apply(data);

            int[][] input = codifiedData.ToJagged <int>("Age", "Gender");

            int[] predictions = codifiedData.ToArray <int>("Best Genre");

            ID3Learning decisionTreeLearningAlgorithm = new ID3Learning {
            };

            try
            {
                var   customer = _context.Customers.Where(c => c.Username == id).FirstOrDefault();
                int[] query;
                if (customer.Age <= 12)
                {
                    query = codification.Transform(new[, ] {
                        { "Age", "0-12" }, { "Gender", customer.Gender.ToString() }
                    });
                }
                else if (12 < customer.Age && customer.Age <= 25)
                {
                    query = codification.Transform(new[, ] {
                        { "Age", "13-25" }, { "Gender", customer.Gender.ToString() }
                    });
                }
                else if (25 < customer.Age && customer.Age < 40)
                {
                    query = codification.Transform(new[, ] {
                        { "Age", "26-39" }, { "Gender", customer.Gender.ToString() }
                    });
                }
                else
                {
                    query = codification.Transform(new[, ] {
                        { "Age", "40+" }, { "Gender", customer.Gender.ToString() }
                    });
                }

                DecisionTree decisionTree = decisionTreeLearningAlgorithm.Learn(input, predictions);
                int          result       = decisionTree.Decide(query);
                string       diagnosis    = codification.Revert("Best Genre", result);
                return(diagnosis);
            }
            catch (Exception)
            {
                return("Unfortunatly No Matches Were Found");

                throw;
            }
        }
示例#12
0
        public Codification GenerateDecisionTreeLib(DataTable data)
        {
            Codification b = new Codification(data);

            DataTable symbols = b.Apply(data);

            int[][] inputs = DataTableToMatrix(symbols, new string[] { "CAP SHAPE", "CAP SURFACE", "CAP COLOR",
                                                                       "BRUISES", "ODOR", "GILL ATTACHMENT",
                                                                       "GILL SPACING", "GILL SIZE", "GILL COLOR",
                                                                       "STALK SHAPE", "STALK ROOT", "STALK SURFACE ABOVE RING",
                                                                       "STALK SURFACE BELOW RING", "STALK COLOR ABOVE RING", "STALK COLOR BELOW RING",
                                                                       "VEIL TYPE", "VEIL COLOR", "RING NUMBER",
                                                                       "RING TYPE", "SPORE PRINT COLOR", "POPULATION",
                                                                       "HABITAT" });

            int[][] mOutputs = DataTableToMatrix(symbols, new string[] { "TYPE" });
            int[]   outputs  = new int[mOutputs.Length];
            for (int i = 0; i < mOutputs.Length; i++)
            {
                outputs[i] = mOutputs[i][0];
            }

            ID3Learning id3learning = new ID3Learning()
            {
                new DecisionVariable("CAP SHAPE", Mushroom.CAP_SHAPE.Length),                               //1
                new DecisionVariable("CAP SURFACE", Mushroom.CAP_SURFACE.Length),                           //2
                new DecisionVariable("CAP COLOR", Mushroom.CAP_COLOR.Length),                               //3

                new DecisionVariable("BRUISES", Mushroom.BRUISES.Length),                                   //4
                new DecisionVariable("ODOR", Mushroom.ODOR.Length),                                         //5

                new DecisionVariable("GILL ATTACHMENT", Mushroom.GILL_ATTACHMENT.Length),                   //6
                new DecisionVariable("GILL SPACING", Mushroom.GILL_SPACING.Length),                         //7
                new DecisionVariable("GILL SIZE", Mushroom.GILL_SIZE.Length),                               //8
                new DecisionVariable("GILL COLOR", Mushroom.GILL_COLOR.Length),                             //9

                new DecisionVariable("STALK SHAPE", Mushroom.STALK_SHAPE.Length),                           //10
                new DecisionVariable("STALK ROOT", Mushroom.STALK_ROOT.Length),                             //11
                new DecisionVariable("STALK SURFACE ABOVE RING", Mushroom.STALK_SURFACE_ABOVE_RING.Length), //12
                new DecisionVariable("STALK SURFACE BELOW RING", Mushroom.STALK_SURFACE_BELOW_RING.Length), //13
                new DecisionVariable("STALK COLOR ABOVE RING", Mushroom.STALK_COLOR_ABOVE_RING.Length),     //14
                new DecisionVariable("STALK COLOR BELOW RING", Mushroom.STALK_COLOR_BELOW_RING.Length),     //15

                new DecisionVariable("VEIL TYPE", Mushroom.VEIL_TYPE.Length),                               //16
                new DecisionVariable("VEIL COLOR", Mushroom.VEIL_COLOR.Length),                             //17

                new DecisionVariable("RING NUMBER", Mushroom.RING_NUMBER.Length),                           //18
                new DecisionVariable("RING TYPE", Mushroom.RING_TYPE.Length),                               //19

                new DecisionVariable("SPORE PRINT COLOR", Mushroom.SPORE_PRINT_COLOR.Length),               //20
                new DecisionVariable("POPULATION", Mushroom.POPULATION.Length),                             //21
                new DecisionVariable("HABITAT", Mushroom.HABITAT.Length)                                    //22
            };

            decisionTreeLib = id3learning.Learn(inputs, outputs);

            return(b);
        }
示例#13
0
        public void learn_test_automatic()
        {
            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
            };

            ID3Learning teacher = new ID3Learning();

            var tree = teacher.Learn(inputs, outputs);

            double error = teacher.ComputeError(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
        }
示例#14
0
        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);
        }
        public void Learn()
        {
            var id3learning = new ID3Learning()
            {
                new DecisionVariable("year", 2016 - 1985),
                new DecisionVariable("generation", 6),
                new DecisionVariable("sex", 2),
            };

            tree = id3learning.Learn(inputs, outputs);
        }
示例#16
0
        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);
        }
示例#17
0
        /*
         * Takes a Datatable with the training data
         * translates the data to ints
         * trains using the training data
         * The last col of the datatable input is the thing to predicted
         */
        public void Train(int index)
        {
            DataTable dataTable = this.theData;

            // Debug.Write("DataTable size: ");
            // Debug.Write("Rows: " + dataTable.Rows.Count);
            // Debug.Write("Cols: " + dataTable.Columns.Count);

            ArrayList inputNames = new ArrayList();

            foreach (DataColumn column in dataTable.Columns)
            {
                inputNames.Add(column.ColumnName);
            }
            this.toPredict = (string)inputNames[index];                                         // The column to predict
            inputNames.RemoveAt(index);                                                         // the data input data (predict column removed)
            this.inputNamesArr = (string[])inputNames.ToArray(typeof(string));

            // Debug.Write("Input arr size: " + inputNamesArr.Length);

            // Using Accord.Statistics.Filters to present the data as integers,
            // as integers are more efficient
            this.codebook = new Codification(dataTable)
            {
                DefaultMissingValueReplacement = 0
            };                                                                                   // codebook object that can convert  strings to ints, null/missing value will be defaulted to 0
            DataTable symbols = codebook.Apply(dataTable);                                       // applying our data to the codebook

            int[][] inputs  = symbols.ToJagged <int>(inputNamesArr);                             // The conversion to ints
            int[]   outputs = symbols.ToArray <int>(toPredict);                                  // The conversion to ints

            // Debug.Write("Array size: ");
            // Debug.Write("inputs: " + inputs.Length);
            // Debug.Write("outputs: " + outputs.Length);

            // Debug.Write("Test");

            var id3 = new ID3Learning()                                                          // the id3 algo
            {
                Attributes = DecisionVariable.FromCodebook(codebook, inputNamesArr)              // the trees decision attributes/headers from excel, second argument could be given saying what columns it should be
            };

            this.tree = id3.Learn(inputs, outputs);                                              // Learn using the inputs and output defined above

            // transform the rules of the tree into a string
            DecisionSet treeRules = tree.ToRules();

            ruleText = treeRules.ToString(codebook, toPredict,
                                          System.Globalization.CultureInfo.InvariantCulture);
            Debug.WriteLine(ruleText);
        }
        public void BuildTree(DataTable data)
        {
            Codificate(data);
            var id3learning = new ID3Learning()
            {
                new DecisionVariable("Branch", 3),        // 3 possible values (A, B, C)
                new DecisionVariable("Customer Type", 2), // 2 possible values (Normal, Member)
                new DecisionVariable("Gender", 2),        // 2 possible values (Female, Male)
                new DecisionVariable("Payment", 3)        // 3 possible values (Cash, Credit card and Ewallet)
            };

            // Learn the training instances!
            tree = id3learning.Learn(inputs, outputs);
        }
示例#19
0
        //
        //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);
        }
    // Use this for initialization
    void Start()
    {
        // In this example, we will learn a decision tree directly from integer
        // matrices that define the inputs and outputs of our learning problem.

        int[][] inputs =                // Tabela de valores lógicos (1 é verdadeiro e 0 é falso)
        {
            new int[] { 1, 0 },
            new int[] { 0, 1 },
            new int[] { 0, 0 },
            new int[] { 1, 1 },
        };

        int[] outputs =                 // Operação AND
        {
            0, 0, 0, 1
        };

        int[][] exampleData =
        {
            new int[] { 1, 1 },
            new int[] { 0, 0 },
            new int[] { 1, 0 },
            new int[] { 0, 1 },
        };

        // Create an ID3 learning algorithm
        ID3Learning teacher = new ID3Learning();

        // Learn a decision tree for the XOR problem
        var tree = teacher.Learn(inputs, outputs);

        // Compute the error in the learning
        double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs));

        Debug.Log("Houve erro?" + error);

        // The tree can now be queried for new examples:
        int[] predicted = tree.Decide(exampleData);         // A saída será { 1, 0, 0, 0 }

        for (int i = 0; i < predicted.Length; i++)
        {
            Debug.Log(predicted[i]);
        }
    }
示例#21
0
        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);
        }
示例#22
0
        private static void initDecisionTreeModel()
        {
            dtStatic.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");
            dtStatic.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No");
            dtStatic.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No");
            dtStatic.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes");
            dtStatic.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes");
            dtStatic.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes");
            dtStatic.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No");
            dtStatic.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes");
            dtStatic.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No");
            dtStatic.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes");
            dtStatic.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes");
            dtStatic.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes");
            dtStatic.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes");
            dtStatic.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes");
            dtStatic.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No");
            dtStatic.Rows.Add("D15", "Rain", "Cool", "High", "Strong", "No");
            dtStatic.Rows.Add("D16", "Rain", "Hot", "High", "Strong", "Yes");
            dtStatic.Rows.Add("D17", "Rain", "Hot", "High", "Weak", "Yes");
            dtStatic.Rows.Add("D18", "Rain", "Cool", "High", "Weak", "No");
            dtStatic.Rows.Add("D19", "Rain", "Cool", "High", "Weak", "Yes");
            dtStatic.Rows.Add("D20", "Rain", "Mild", "High", "Strong", "Yes");

            myCodeBook = new Codification(dtStatic);

            DataTable symbols = myCodeBook.Apply(dtStatic);

            int[][] inputs      = symbols.ToJagged <int>("Outlook", "Temperature", "Humidity", "Wind");
            int[]   outputs     = symbols.ToArray <int>("PlayTennis");
            var     id3learning = new ID3Learning()
            {
                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)
            };

            myTreeModel = id3learning.Learn(inputs, outputs);

            double error = new ZeroOneLoss(outputs).Loss(myTreeModel.Decide(inputs));

            Console.WriteLine("learnt model training accuracy is: " + (100 - error).ToString("N2"));
        }
        public ClassifierWReview()
        {
            //runData2 and runData2_1
            //string filedata = System.IO.File.ReadAllText("../runData2.txt");
            string filedata = System.IO.File.ReadAllText("../runData2_1.txt");

            string[] inputColumns =
            {
                "Day", "Outlook", "Temperature", "Humidity", "Wind", "SprintReview"
            };

            string outputColumn = "GoRun";

            DataTable data = new DataTable("Internet Services Run Calculator");

            data.Columns.Add(inputColumns);
            data.Columns.Add(outputColumn);

            string[] lines = filedata.Split(
                new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);

            foreach (var line in lines)
            {
                data.Rows.Add(line.Split(','));
            }

            //create codebook to turn the strings into number representations
            codebook = new Accord.Statistics.Filters.Codification(data);

            // Translate our training data into integer symbols using our codebook:
            DataTable symbols = codebook.Apply(data);

            int[][] inputs  = symbols.ToJagged <int>("Outlook", "Temperature", "Humidity", "Wind", "SprintReview");
            int[]   outputs = symbols.ToArray <int>("GoRun");

            string[]           decisionVariables = { "Outlook", "Temperature", "Humidity", "Wind", "SprintReview" };
            DecisionVariable[] attributes        = DecisionVariable.FromCodebook(codebook, decisionVariables);
            // Create a teacher ID3 algorithm
            var id3learning = new ID3Learning(attributes);

            tree = id3learning.Learn(inputs, outputs);
            // Compute the training error when predicting training instances
            double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs));
        }
示例#24
0
        static DecisionTree DecisionTreeClassification(List <int[]> trainingData, List <int[]> testingData, out double precision)
        {
            int    testingCount      = testingData.Count / 10;
            int    trainingCount     = testingData.Count - testingCount;
            double errorAverage      = 0;
            int    indexTestingStart = testingData.Count - testingCount;
            int    indexTestingEnd   = testingData.Count;
            double prec = 0;

            Console.WriteLine("Decision Tree Classification");
            DecisionTree bestDecision = null;

            for (int i = 0; i < iterations; i++)
            {
                var watch = System.Diagnostics.Stopwatch.StartNew();
                Console.WriteLine("Testing from: {0} to {1}", indexTestingStart, indexTestingEnd);
                int[][] inputData, testinputData;
                int[]   outputData, testoutputData;

                PrepareInputOutput(out inputData, out outputData, out testinputData, out testoutputData, trainingData, testingData, indexTestingStart, indexTestingEnd);

                ID3Learning teacher  = new ID3Learning();
                var         decision = teacher.Learn(inputData, outputData);
                Console.WriteLine("Medis sukurtas - ismokta");
                double error = new ZeroOneLoss(testoutputData).Loss(decision.Decide(testinputData));
                Console.WriteLine("Apmokymo tikslumas: {0}", 1 - error);
                if (1 - error > prec)
                {
                    prec         = 1 - error;
                    bestDecision = decision;
                }
                watch.Stop();
                var elapsedMs = watch.ElapsedMilliseconds;
                Console.WriteLine("Iteracija baigta per: {0}ms", elapsedMs);
                indexTestingEnd    = indexTestingStart;
                indexTestingStart -= testingCount;
                errorAverage      += error;
                bestDecision       = decision;
                Console.WriteLine("------------------------------------------------------------------------------");
            }
            precision = 1 - (errorAverage / iterations);
            return(bestDecision);
        }
示例#25
0
    void Start()
    {
        // Adicionando classe e atributos à tabela
        keyTable.Columns.Add("First key", typeof(string));
        keyTable.Columns.Add("Second key", typeof(string));
        keyTable.Columns.Add("Third key", typeof(string));
        keyTable.Columns.Add("Exit", typeof(string));

        // Adicionando registros à tabela
        keyTable.Rows.Add("Yellow", "Purple", "Blue", "First");
        keyTable.Rows.Add("Yellow", "Blue", "Purple", "Second");
        keyTable.Rows.Add("Purple", "Yellow", "Blue", "First");
        keyTable.Rows.Add("Purple", "Blue", "Yellow", "Second");
        keyTable.Rows.Add("Blue", "Purple", "Yellow", "First");
        keyTable.Rows.Add("Blue", "Yellow", "Purple", "Second");

        //	Para ficar menos custoso computacionalmente, o Accord converte as
        //	strings em integer symbols. Para isso, usa-se o codebook
        codebook = new Codification(keyTable);

        // Converterndo os dados da tabela para integer symbols usando o codebook
        DataTable symbols = codebook.Apply(keyTable);

        int[][] inputs  = symbols.ToJagged <int> ("First key", "Second key", "Third key");
        int[]   outputs = symbols.ToArray <int> ("Exit");

        // Criando o algoritmo ID3
        var id3Learning = new ID3Learning()
        {
            // Quantidade de instâncias diferentes em cada coluna
            new DecisionVariable("First key", 3),               //	Cada uma possui três instâncias possíveis:
            new DecisionVariable("Second key", 3),              //	1.yellow	2.purple	3.blue
            new DecisionVariable("Third key", 3)
        };

        //	Treinando a árvore
        tree = id3Learning.Learn(inputs, outputs);

        //	Verificando se houve erro no treino da árvore
        double errorTraining = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs));

        Debug.Log("Tree error? (0 = no, 1 = yes) \n" + errorTraining);
    }
        public TrainerHelper Train(System.Data.DataTable table, string columnName)
        {
            var container            = new TrainerHelper();
            var trainingCodification = new Codification()
            {
                DefaultMissingValueReplacement = Double.NaN
            };

            trainingCodification.Learn(table);
            DataTable symbols = trainingCodification.Apply(table);

            container.columnNamesArray =
                table.Columns.Cast <DataColumn>().Select(x => x.ColumnName).Where(s => s != columnName).ToArray();

            var columnOrdinal = table.Columns[columnName].Ordinal;

            int[][]    tempInputs = symbols.ToJagged <int>(container.columnNamesArray);
            double[][] inputs     = new double[tempInputs.Length][];
            for (var i = 0; i < tempInputs.Length; i++)
            {
                // var flattened = this.ExpandRow(trainingCodification, tempInputs[i], columnOrdinal);
                // inputs[i] = flattened;
            }


            int[] outputs = symbols.ToArray <int>(columnName);

            var id3learning = new ID3Learning();

            id3learning.Attributes = DecisionVariable.FromCodebook(trainingCodification);
            // Learn the training instances!
            DecisionTree tree = id3learning.Learn(tempInputs, outputs);

            container.decisionTree = tree;


            //var lbnr = new LowerBoundNewtonRaphson() { MaxIterations = 100, Tolerance = 1e-6 };
            //var mlr = lbnr.Learn(inputs, outputs);
            container.codification = trainingCodification;
            container.symbols      = symbols;
            return(container);
        }
示例#27
0
    private void Train()
    {
        DataTable data = GetDataTable(Application.dataPath + "/" + trainData);

        //DebugTable(data);
        codebook = new Codification(data);
        DataTable symbols = codebook.Apply(data);

        int[][] inputs  = symbols.ToArray <int>("LIFE", "TOWERS", "MELIANTS", "TIME", "ENEMY_COINS");
        int[]   outputs = symbols.ToArray <int>("POSITION");

        var id3learning = new ID3Learning();

        id3learning.Attributes = DecisionVariable.FromData(inputs);

        tree = id3learning.Learn(inputs, outputs);

        double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs));

        tree.Save(Application.dataPath + "/" + treeLocation);
    }
示例#28
0
        static double Decision_Tree(bool show)
        {
            DataTable    data       = DataController.MakeDataTable("../../drug_consumption.txt");
            DataTable    entireData = DataController.MakeDataTable("../../drug_consumption.txt");
            DataTable    tests      = DataController.MakeDataTable("../../drug_consumption_test2.txt");
            Codification codebook   = new Codification(entireData);

            DecisionVariable[] attributes = DataController.GetAttributes();
            int classCount = 7; // (7) "Never Used", "Used over a Decade Ago", "Used in Last Decade", "Used in Last Year", "Used in Last Month", "Used in Last Week", and "Used in Last Day"

            DecisionTree tree        = new DecisionTree(attributes, classCount);
            ID3Learning  id3learning = new ID3Learning(tree);

            id3learning.MaxHeight = 7;
            DataTable symbols    = codebook.Apply(data);
            string    LookingFor = "Cannabis";

            int[][] inputs  = symbols.ToJagged <int>("Age", "Gender", "Education", "Country", "Eticnity", "Nscore", "Escore", "Oscore", "Ascore", "Cscore", "Impulsive", "SS");
            int[]   outputs = symbols.ToArray <int>(LookingFor);

            id3learning.Learn(inputs, outputs);
            DataTable testSymbols = codebook.Apply(tests);

            int[][]     testIn   = testSymbols.ToJagged <int>("Age", "Gender", "Education", "Country", "Eticnity", "Nscore", "Escore", "Oscore", "Ascore", "Cscore", "Impulsive", "SS");
            int[]       testOut  = testSymbols.ToArray <int>(LookingFor);
            DecisionSet rules    = tree.ToRules();
            string      ruleText = rules.ToString(codebook, LookingFor, System.Globalization.CultureInfo.InvariantCulture);
            double      error    = new ZeroOneLoss(testOut).Loss(tree.Decide(testIn));

            if (show == true)
            {
                Console.WriteLine(LookingFor);
                Console.WriteLine();
                Console.WriteLine(ruleText);
                Console.ReadKey();
                Console.WriteLine("Blad - " + Math.Round(error, 4) + "%");
                Console.ReadKey();
            }
            return(error);
        }
示例#29
0
        public void learn_doc()
        {
            #region doc_learn_simplest
            // In this example, we will learn a decision tree directly from integer
            // matrices that define the inputs and outputs of our learning problem.

            int[][] inputs =
            {
                new int[] { 0, 0 },
                new int[] { 0, 1 },
                new int[] { 1, 0 },
                new int[] { 1, 1 },
            };

            int[] outputs = // xor between inputs[0] and inputs[1]
            {
                0, 1, 1, 0
            };

            // Create an ID3 learning algorithm
            ID3Learning teacher = new ID3Learning();

            // Learn a decision tree for the XOR problem
            var tree = teacher.Learn(inputs, outputs);

            // Compute the error in the learning
            double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs));

            // The tree can now be queried for new examples:
            int[] predicted = tree.Decide(inputs); // should be { 0, 1, 1, 0 }
            #endregion

            Assert.AreEqual(0, error);
            Assert.AreEqual(0, predicted[0]);
            Assert.AreEqual(1, predicted[1]);
            Assert.AreEqual(1, predicted[2]);
            Assert.AreEqual(0, predicted[3]);
        }
        public string simulate(int year, string generation, string sex)
        {
            var id3learning = new ID3Learning()
            {
                new DecisionVariable("year", 2016 - 1985),
                new DecisionVariable("generation", 6),
                new DecisionVariable("sex", 2),
            };

            tree = id3learning.Learn(inputs, outputs);

            int[] query = codebook.Transform(new[, ]
            {
                { "year", year.ToString() },
                { "generation", generation },
                { "sex", sex },
            });

            int    predicted = tree.Decide(query);
            string answer    = codebook.Revert("risk", predicted);

            return(answer);
        }