Exemplo n.º 1
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);
            }
        }
Exemplo n.º 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 }));
        }
Exemplo n.º 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 }
        }
        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);
        }
Exemplo n.º 5
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();
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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;
            }
        }
Exemplo n.º 8
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
        }
Exemplo n.º 9
0
        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);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 12
0
    // 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]);
        }
    }
Exemplo n.º 13
0
        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));
        }
Exemplo n.º 14
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"));
        }
Exemplo n.º 15
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);
    }
Exemplo n.º 16
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);
        }
        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);
        }
Exemplo n.º 18
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);
    }
Exemplo n.º 19
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);
        }
Exemplo n.º 20
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]);
        }
Exemplo n.º 21
0
        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);
        }
Exemplo n.º 22
0
        public static void BuildDecisionTreeOnYearAndUser()
        {
            DataHandler.ImportReviewData(5);
            DataHandler.Reviews.Shuffle();
            DataTable data = new DataTable("Review Simple Input Data");

            data.Columns.Add("Year");
            data.Columns.Add("User");
            data.Columns.Add("Review");
            for (int i = 0; i < 1500; i++)
            {
                var      currentReview = DataHandler.Reviews[i];
                object[] values        = new object[3];
                values[0] = currentReview.reviewTime.Year;
                values[1] = currentReview.reviewerID;
                values[2] = currentReview.overall;

                data.Rows.Add(values);
            }

            // Create a new codification codebook to
            // convert strings into integer symbols
            var       codebook = new Codification(data, "Year", "User", "Review");
            DataTable symbols  = codebook.Apply(data, "Year", "User", "Review");

            int[][] inputs  = symbols.ToJagged <int>("Year", "User");
            int[]   outputs = symbols.ToArray <int>("Review");

            // Gather information about decision variables
            DecisionVariable[] attributes =
            {
                new DecisionVariable("Year", 7),  // 3 years
                new DecisionVariable("User", 18), // 18 possible users
            };

            // Create a new instance of the ID3 algorithm
            var id3learning = new ID3Learning(attributes);
            // Learn the training instances!
            DecisionTree tree = id3learning.Learn(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

            DataTable newData = new DataTable("Review Simple Input Data");

            newData.Columns.Add("Year");
            newData.Columns.Add("User");
            newData.Columns.Add("Review");
            for (int i = 1500; i < 2000; i++)
            {
                var      currentReview = DataHandler.Reviews[i];
                object[] values        = new object[3];
                values[0] = currentReview.reviewTime.Year;
                values[1] = currentReview.reviewerID;
                values[2] = currentReview.overall;

                newData.Rows.Add(values);
            }

            DataTable newSymbols = codebook.Apply(data, "Year", "User", "Review");

            int[][] newInputs  = newSymbols.ToJagged <int>("Year", "User");
            int[]   newOutputs = newSymbols.ToArray <int>("Review");

            int[] answers = tree.Decide(newInputs);

            ScatterplotBox.Show("Expected results", newOutputs.Select(i => (double)i).ToArray());
            ScatterplotBox.Show("Decision Tree results", newOutputs.Select(i => (double)i).ToArray())
            .Hold();
        }
        public void validation()
        {
            var data = path;

            var csv             = new CsvReader(File.OpenText(path));
            var myCustomObjects = csv.GetRecords <MealData>();

            DataTable dt = new DataTable("FoodDBSample");
            DataRow   row;

            dt.Columns.Add("Category", "Carb", "Protein", "Fat", "Calorie", "Fiber", "Decision");
            foreach (var record in myCustomObjects)
            {
                row = dt.NewRow();


                row["Category"] = record.Category;
                row["Carb"]     = record.Carb;
                row["Protein"]  = record.Protein;
                row["Fat"]      = record.Fat;
                row["Calorie"]  = record.Calorie;
                row["Fiber"]    = record.Fiber;
                row["Decision"] = record.Outcome;

                dt.Rows.Add(row);
            }
            var codebook = new Codification(dt);

            DataTable symbols = codebook.Apply(dt);

            int[][] inputs  = symbols.ToJagged <int>("Category", "Carb", "Protein", "Fat", "Calorie", "Fiber");
            int[]   outputs = symbols.ToArray <int>("Decision");

            //specify which columns to use for making decisions
            var id3learning = new ID3Learning()
            {
                new DecisionVariable("Category", 4),
                new DecisionVariable("Carb", 2),
                new DecisionVariable("Protein", 2),
                new DecisionVariable("Fat", 2),
                new DecisionVariable("Calorie", 2),
                new DecisionVariable("Fiber", 2)
            };



            DecisionTree tree = id3learning.Learn(inputs, outputs);

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

            // measure the cross-validation performance of
            // a decision tree with a maximum tree height of 5. With variables
            // able to join the decision path at most 2 times during evaluation:
            var cv = CrossValidation.Create(

                k: 5,                             // 5-fold cross-validation

                learner: (p) => new ID3Learning() //create the learning algorithm
            {
                new DecisionVariable("Category", 4),
                new DecisionVariable("Carb", 2),
                new DecisionVariable("Protein", 2),
                new DecisionVariable("Fat", 2),
                new DecisionVariable("Calorie", 2),
                new DecisionVariable("Fiber", 2)
            },


                loss: (actual, expected, p) => new ZeroOneLoss(expected).Loss(actual),

                // function can be used to perform any special
                // operations before the actual learning is done, but
                // here we will just leave it as simple as it can be:
                fit: (teacher, x, y, w) => teacher.Learn(x, y, w),

                // pass the input and output data
                // that will be used in cross-validation.
                x: inputs, y: outputs
                );

            // After the cross-validation object has been created,
            // we can call its .Learn method with the input and
            // output data that will be partitioned into the folds:
            var result = cv.Learn(inputs, outputs);

            //Gather info
            int numberOfSamples = result.NumberOfSamples;
            int numberOfInputs  = result.NumberOfInputs;
            int numberOfOutputs = result.NumberOfOutputs;

            double trainingError   = result.Training.Mean;
            double validationError = result.Validation.Mean;

            System.Diagnostics.Debug.WriteLine("ID3 Mean: " + validationError);
            System.Diagnostics.Debug.WriteLine("ID3 Error: " + trainingError);
        }
        public List <Meal> ID3(dynamic food, List <User> users)
        {
            //dtable();
            // Dictionary<string,List<Category>> FoodCategory = new Dictionary<string,List<Category>>();
            List <Meal> category = new List <Meal> {
            };

            foreach (User u in users)
            {
                BMI  = u.bmi;
                User = u.user;
            }


            //Iterate through food and add each food with range of macronutrients to category list
            foreach (var nw in food)
            {
                var value = (nw.Category.Predicted).Split(",");
                category.Add(new Meal {
                    food = nw.Food.Food, protein = value[0], carb = value[1], fat = value[2], calorie = value[3], fiber = value[4], nprotein = nw.Food.Total_Protein, ncarb = nw.Food.Total_Carb, nfat = nw.Food.Total_Fat, ncalorie = nw.Food.Num_Calorie, nfiber = nw.Food.Fiber, serving = nw.Food.Serving, img = nw.Food.img, fgroup = nw.Food.fgroup, PrefID = nw.Food.ID, servingQty = nw.Food.ServingQty.ToString(), decision = ""
                });
            }

            var csv             = new CsvReader(File.OpenText(path));
            var myCustomObjects = csv.GetRecords <MealData>();

            DataTable dt = new DataTable("FoodDBSample");
            DataRow   row;

            dt.Columns.Add("Category", "Carb", "Protein", "Fat", "Calorie", "Fiber", "Decision");
            foreach (var record in myCustomObjects)
            {
                row = dt.NewRow();


                row["Category"] = record.Category;
                row["Carb"]     = record.Carb;
                row["Protein"]  = record.Protein;
                row["Fat"]      = record.Fat;
                row["Calorie"]  = record.Calorie;
                row["Fiber"]    = record.Fiber;
                row["Decision"] = record.Outcome;

                dt.Rows.Add(row);
            }
            var codebook = new Codification(dt);

            DataTable symbols = codebook.Apply(dt);

            int[][] inputs  = symbols.ToJagged <int>("Category", "Carb", "Protein", "Fat", "Calorie", "Fiber");
            int[]   outputs = symbols.ToArray <int>("Decision");

            //specify which columns to use for making decisions
            var id3learning = new ID3Learning()
            {
                new DecisionVariable("Category", 4),
                new DecisionVariable("Carb", 2),
                new DecisionVariable("Protein", 2),
                new DecisionVariable("Fat", 2),
                new DecisionVariable("Calorie", 2),
                new DecisionVariable("Fiber", 2)
            };


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

            // Compute the training error when predicting training instances
            double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs));



            List <dynamic> predict = new List <dynamic> {
            };

            //iterate through data structure category and make prediction for each food
            foreach (var item in category)
            {
                predict.Add(codebook.Transform(new[, ]
                {
                    { "Category", $"{BMI}" },
                    { "Carb", $"{item.carb}" },
                    { "Protein", $"{item.protein}" },
                    { "Fat", $"{item.fat}" },
                    { "Calorie", $"{item.calorie}" },
                    { "Fiber", $"{item.fiber}" }
                }));
            }

            //Accord.IO.Serializer.Save(tree, Path.Combine(basePath, "ID3TreeModel.bin"));
            //int predicted = tree.Decide(query);
            List <string> q = new List <string>();

            foreach (var i in predict)
            {
                q.Add(codebook.Revert("Decision", tree.Decide(i)));
            }
            // We can translate it back to strings using
            //string answer = codebook.Revert("Decision", predicted);
            //foreach (var item in q)
            //{
            //    System.Diagnostics.Debug.WriteLine(item);
            //}
            var         foods    = category.Zip(q, (n, w) => new { Food = n, Decision = w });
            List <Meal> positive = new List <Meal> {
            };
            List <Meal> negative = new List <Meal> {
            };

            foreach (var nw in foods)
            {
                nw.Food.decision = nw.Decision;
            }
            foreach (var item in foods)
            {
                //System.Diagnostics.Debug.WriteLine(item.Food.food + "\n" + item.Food.nprotein + "\n" + item.Food.ncarb + "\n" + item.Food.nfat + "\n" + item.Food.ncalorie + "\n" + item.Food.serving + "\n" + item.Food.img + "\n" + item.Food.fgroup+"\n"+item.Decision);
                //System.Diagnostics.Debug.WriteLine(item.Food.food+"\n"+item.Decision);
                if (item.Decision.Equals("positive"))
                {
                    positive.Add(new Meal {
                        CurrentUser = User, food = item.Food.food, nprotein = item.Food.nprotein, ncarb = item.Food.ncarb, nfat = item.Food.nfat, ncalorie = item.Food.ncalorie, nfiber = item.Food.nfiber, img = item.Food.img, serving = item.Food.serving, fgroup = item.Food.fgroup, PrefID = item.Food.PrefID, servingQty = item.Food.servingQty, decision = item.Decision
                    });
                }
                if (item.Decision.Equals("negative"))
                {
                    negative.Add(new Meal {
                        CurrentUser = User, food = item.Food.food, nprotein = item.Food.nprotein, ncarb = item.Food.ncarb, nfat = item.Food.nfat, ncalorie = item.Food.ncalorie, nfiber = item.Food.nfiber, img = item.Food.img, serving = item.Food.serving, fgroup = item.Food.fgroup, PrefID = item.Food.PrefID, servingQty = item.Food.servingQty, decision = item.Decision
                    });
                }
            }
            //foreach (var item in positive)
            //{

            //    //System.Diagnostics.Debug.WriteLine(item.food + "\n" + item.nprotein + "\n" + item.ncarb + "\n" + item.nfat + "\n" + item.ncalorie + "\n" + item.serving + "\n" + item.img + "\n" + item.fgroup + "\n" + item.decision);
            //    System.Diagnostics.Debug.WriteLine(item.food + "\n" + "Protein: "+item.nprotein + "\n" + "Carb: " + item.ncarb + "\n" + "Fat: " + item.nfat + "\n" + "Calorie: " + item.ncalorie+"\n"+ "Fiber: "+item.nfiber+"\n"+ "Outcome: " + item.decision);

            //}
            //foreach (var item in negative)
            //{

            //    //System.Diagnostics.Debug.WriteLine(item.food + "\n" + item.nprotein + "\n" + item.ncarb + "\n" + item.nfat + "\n" + item.ncalorie + "\n" + item.serving + "\n" + item.img + "\n" + item.fgroup + "\n" + item.decision);
            //    System.Diagnostics.Debug.WriteLine(item.food + "\n" + "Protein: " + item.nprotein + "\n" + "Carb: " + item.ncarb + "\n" + "Fat: " + item.nfat + "\n" + "Calorie: " + item.ncalorie + "\n" + "Fiber: " + item.nfiber + "\n" + "Outcome: " + item.decision);

            //}


            // Validation purposes only
            //var cm = GeneralConfusionMatrix.Estimate(tree, inputs, outputs);
            //double err = cm.Error;
            //double acc = cm.Accuracy;
            //double kappa = cm.Kappa;
            //validation();
            //System.Diagnostics.Debug.WriteLine("error: " + err);
            //System.Diagnostics.Debug.WriteLine("accuracy: " + acc);
            DisplayDiet diet = new DisplayDiet();

            diet.GetFoods(positive);
            diet.GetUser(users);
            //diet.CreateDiet(positive);
            return(positive);
        }
Exemplo n.º 25
0
        //4, 10, 2, 1, 0, 0, 0, 0
        public string BankPrediction(int a, int b, int c, int d, int e, int f, int g, int h, string name)
        {
            string salida = "Vacio";

            try
            {
                var codebook = new Accord.Statistics.Filters.Codification(table);
                System.Data.DataTable symbols = codebook.Apply(table);

                int[][] inputs  = DataTableToMatrix(symbols, new string[] { "AGE", "JOB", "MARITAL", "EDUCATION", "DEBT", "BALANCE", "HOUSING", "LOAN" });
                int[][] outputs = DataTableToMatrix(symbols, new string[] { "DEPOSIT" });
                int[]   output  = new int[outputs.Length];
                for (int i = 0; i < outputs.Length; i++)
                {
                    output[i] = outputs[i][0];
                }

                ID3Learning teacher = new ID3Learning()
                {
                    new DecisionVariable("AGE", 5),
                    new DecisionVariable("JOB", 12),
                    new DecisionVariable("MARITAL", 3),
                    new DecisionVariable("EDUCATION", 4),
                    new DecisionVariable("DEBT", 2),
                    new DecisionVariable("BALANCE", 6),
                    new DecisionVariable("HOUSING", 2),
                    new DecisionVariable("LOAN", 2)
                };

                DecisionTree tree = teacher.Learn(inputs, output);

                //mandar la variable error a un label que me muestre el error que tiene el arbol
                double error = new Accord.Math.Optimization.Losses.ZeroOneLoss(output).Loss(tree.Decide(inputs));
                double ep    = Math.Floor(error * 100);
                errorLabel.Text = ep + "%" + " " + "-" + " " + "(" + error + ")";


                int[] input      = new int[] { a, b, c, d, e, f, g, h };
                int   prediccion = tree.Decide(input);

                string predijo = prediccion == 1 ? "yes" : "no";
                if (predijo.Equals("yes"))
                {
                    outputLabel.ForeColor = Color.FromArgb(0, 255, 0);
                }
                else if (predijo.Equals("no"))
                {
                    outputLabel.ForeColor = Color.FromArgb(255, 0, 0);
                }
                outputLabel.Text = predijo;
                salida           = predijo;

                subjectLabel.Text = "for" + " " + name + " " + "the prediction is";
            }
            catch (Exception) {
                string message = "Yo cannot make predictions without\nloading the data";
                string title   = "Warning";
                MessageBox.Show(message, title);
            }
            return(salida);
        }
Exemplo n.º 26
0
        public void learn_doc2()
        {
            #region doc_learn_mitchell
            // In this example, we will be using the famous Play Tennis example by Tom Mitchell (1998).
            // In Mitchell's example, one would like to infer if a person would play tennis or not
            // based solely on four input variables. Those variables are all categorical, meaning that
            // there is no order between the possible values for the variable (i.e. there is no order
            // relationship between Sunny and Rain, one is not bigger nor smaller than the other, but are
            // just distinct). Moreover, the rows, or instances presented above represent days on which the
            // behavior of the person has been registered and annotated, pretty much building our set of
            // observation instances for learning:

            // Note: this example uses DataTables to represent the input data , but this is not required.
            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");

            // In order to try to learn a decision tree, we will first convert this problem to a more simpler
            // representation. Since all variables are categories, it does not matter if they are represented
            // as strings, or numbers, since both are just symbols for the event they represent. Since numbers
            // are more easily representable than text string, we will convert the problem to use a discrete
            // alphabet through the use of a Accord.Statistics.Filters.Codification codebook.</para>

            // A codebook effectively transforms any distinct possible value for a variable into an integer
            // symbol. For example, “Sunny” could as well be represented by the integer label 0, “Overcast”
            // by “1”, Rain by “2”, and the same goes by for the other variables. So:</para>

            // Create a new codification codebook to
            // convert strings into integer symbols
            var codebook = new 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");
            int[]     outputs = symbols.ToArray <int>("PlayTennis");

            // For this task, in which we have only categorical variables, the simplest choice
            // to induce a decision tree is to use the ID3 algorithm by Quinlan. Let’s do it:

            // Create a teacher ID3 algorithm
            var id3learning = new ID3Learning()
            {
                // Now that we already have our learning input/ouput pairs, we should specify our
                // decision tree. We will be trying to build a tree to predict the last column, entitled
                // “PlayTennis”. For this, we will be using the “Outlook”, “Temperature”, “Humidity” and
                // “Wind” as predictors (variables which will we will use for our decision). Since those
                // are categorical, we must specify, at the moment of creation of our tree, the
                // characteristics of each of those variables. So:

                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)

                // Note: It is also possible to create a DecisionVariable[] from a codebook:
                // DecisionVariable[] attributes = DecisionVariable.FromCodebook(codebook);
            };

            // Learn the training instances!
            DecisionTree tree = id3learning.Learn(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

            int[] query = codebook.Transform(new[, ]
            {
                { "Outlook", "Sunny" },
                { "Temperature", "Hot" },
                { "Humidity", "High" },
                { "Wind", "Strong" }
            });

            // 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("PlayTennis", predicted); // Answer will be: "No"
            #endregion

            Assert.AreEqual(0, predicted);
            Assert.AreEqual("No", answer);
            Assert.AreEqual(0, error);
        }
Exemplo n.º 27
0
        public static void Run()
        {
            // In this example, we will be using the famous Play Tennis example by Tom Mitchell(1998).
            // In Mitchell's example, one would like to infer if a person would play tennis or not
            // based solely on four input variables. Those variables are all categorical, meaning that
            // there is no order between the possible values for the variable (i.e. there is no order
            // relationship between Sunny and Rain, one is not bigger nor smaller than the other, but are
            // just distinct).

            // Note: this example uses DataTables to represent the input data , but this is not required.
            var example = "Overtake";

            Console.WriteLine(example);

            DataTable data = new DataTable(example);

            data.Columns.Add("Separation", typeof(String));
            data.Columns.Add("Speed", typeof(String));
            data.Columns.Add("OncomingSpeed", typeof(String));
            data.Columns.Add("Result", typeof(String));
            var shuffledInputs = GetInputs(100);

            for (int index = 0; index < shuffledInputs.Length; index++)
            {
                data.Rows.Add(shuffledInputs[index][0], shuffledInputs[index][1], shuffledInputs[index][2], shuffledInputs[index][3]);
            }



            // In order to try to learn a decision tree, we will first convert this problem to a more simpler
            // representation. Since all variables are categories, it does not matter if they are represented
            // as strings, or numbers, since both are just symbols for the event they represent. Since numbers
            // are more easily representable than text string, we will convert the problem to use a discrete
            // alphabet through the use of a Accord.Statistics.Filters.Codification codebook.</para>

            // A codebook effectively transforms any distinct possible value for a variable into an integer
            // symbol. For example, “Sunny” could as well be represented by the integer label 0, “Overcast”
            // by “1”, Rain by “2”, and the same goes by for the other variables. So:</para>

            // Create a new codification codebook to convert strings into integer symbols
            var codebook = new Codification(data);

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

            int[][]  inputs     = symbols.ToJagged <int>(new string[] { "Separation", "Speed", "OncomingSpeed", "Result" });
            int[]    outputs    = symbols.ToArray <int>("Overtake");
            string[] classNames = new string[] { "success", "fail" };

            // For this task, in which we have only categorical variables, the simplest choice
            // to induce a decision tree is to use the ID3 algorithm by Quinlan. Let’s do it:

            // Create an ID3 algorithm
            var id3learning = new ID3Learning()
            {
                // Now that we already have our learning input/ouput pairs, we should specify our
                // decision tree. We will be trying to build a tree to predict the last column, entitled
                // “PlayTennis”. For this, we will be using the “Outlook”, “Temperature”, “Humidity” and
                // “Wind” as predictors (variables which will we will use for our decision). Since those
                // are categorical, we must specify, at the moment of creation of our tree, the
                // characteristics of each of those variables. So:

                new DecisionVariable("Separation", 150),    // 3 possible values (Sunny, overcast, rain)
                new DecisionVariable("Speed", 150),         // 3 possible values (Hot, mild, cool)
                new DecisionVariable("OncomingSpeed", 150), // 2 possible values (High, normal)
                new DecisionVariable("Result", 2)           // 2 possible values (Weak, strong)
            };

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

            // The tree can now be queried for new examples through
            // its Decide method. For example, we can create a query

            int[] query = codebook.Transform(new[, ]
            {
                { "Separation", "150" },
                { "Speed", "150" },
                { "OncomingSpeed", "150" },
                { "Result", "success" }
            });

            // And then predict the label using
            int predicted = tree.Decide(query);

            var answer = codebook.Revert("Overtake", predicted);

            Console.WriteLine("");

            Console.WriteLine(answer);

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

            Console.WriteLine($"{error * 100:F10}");

            DecisionSet rules        = tree.ToRules();
            var         encodedRules = rules.ToString();

            Console.WriteLine(encodedRules);



            Console.ReadKey(); // Keep the window open till a key is pressed
        }
Exemplo n.º 28
0
        static void AccordTest()
        {
            int[] ysequence = new int[] { 1, 2, 3, 2 };

            // this generates the correct Y for a given X
            int CalcY(int x) => ysequence[(x - 1) % 4];

            // this generates some inputs - just a few differnt mod of x
            int[] CalcInputs(int x) => new int[]
            {
                x % 2, x % 3, x % 4, x % 5, x % 6
            };


            // build the training data set
            int numtrainingcases = 12;

            int[][] inputs  = new int[numtrainingcases][];
            int[]   outputs = new int[numtrainingcases];

            Console.WriteLine("\t\t\t\t x \t y");
            for (int x = 1; x <= numtrainingcases; x++)
            {
                int y = CalcY(x);
                inputs[x - 1]  = CalcInputs(x);
                outputs[x - 1] = y;
                Console.WriteLine("TrainingData \t " + x + "\t " + y);
            }

            // define how many values each input can have
            DecisionVariable[] attributes =
            {
                new DecisionVariable("Mod2", 2),
                new DecisionVariable("Mod3", 3),
                new DecisionVariable("Mod4", 4),
                new DecisionVariable("Mod5", 5),
                new DecisionVariable("Mod6", 6)
            };

            // define how many outputs (+1 only because y doesn't use zero)
            int classCount = outputs.Max() + 1;

            // create the tree
            DecisionTree tree = new DecisionTree(attributes, classCount);

            // Create a new instance of the ID3 algorithm
            ID3Learning id3learning = new ID3Learning(tree);

            // Learn the training instances! Populates the tree
            id3learning.Learn(inputs, outputs);

            Console.WriteLine();
            // now try to predict some cases that werent in the training data
            for (int x = numtrainingcases + 1; x <= 2 * numtrainingcases; x++)
            {
                int[] query = CalcInputs(x);

                int answer = tree.Decide(query); // makes the prediction

                Console.WriteLine("Prediction \t\t " + x + "\t " + answer);
            }

            Console.WriteLine("Hello World!");
            Console.ReadKey();
        }
Exemplo n.º 29
0
        static void Main(string[] args)
        {
            //Aufs Maul oder nicht? Das ist hier die Frage...
            Console.Title           = "TreeTest";
            Console.ForegroundColor = ConsoleColor.Yellow;

            Dictionary <int, string> outputDict = new Dictionary <int, string>
            {
                { 0, "nicht aufs maul" },
                { 1, "aufs Maul" }
            };

            Dictionary <string, string> inputDict = new Dictionary <string, string>()
            {
                { "0_0", "nicht verwandt" },
                { "0_1", "irgendwie verwandt" },
                { "1_0", "zimmer sieht kacke aus" },
                { "1_1", "Zimmer is aufgeraeumt" },
                { "2_0", "is scheiße drauf" },
                { "2_1", "is nett" },
                { "3_0", "kann dich sau nicht leiden" },
                { "3_1", "findet dich nice" }
            };

            DecisionVariable[] decVars = new DecisionVariable[]
            {
                new DecisionVariable("Verwandt", 2),
                new DecisionVariable("Aufgeraeumt", 2),
                new DecisionVariable("GoodMood", 2),
                new DecisionVariable("Sympathie", 2),
            };
            int          classCount = 2; // nur ja/nein
            DecisionTree dectree    = new DecisionTree(decVars, classCount);

            ID3Learning id3 = new ID3Learning(dectree);
            C45Learning c45 = new C45Learning(dectree);

            int[][] inputs = new int[][]
            {
                //          V  A  G  S
                new int[] { 0, 0, 0, 0 },
                new int[] { 0, 0, 0, 1 },
                new int[] { 0, 0, 1, 0 },
                new int[] { 0, 0, 1, 1 },
                new int[] { 0, 1, 0, 0 },
                new int[] { 0, 1, 0, 1 },
                new int[] { 0, 1, 1, 0 },
                new int[] { 0, 1, 1, 1 },
                //----------V--A--G--S---
                new int[] { 1, 0, 0, 0 },
                new int[] { 1, 0, 0, 1 },
                new int[] { 1, 0, 1, 0 },
                new int[] { 1, 0, 1, 1 },
                new int[] { 1, 1, 0, 0 },
                new int[] { 1, 1, 0, 1 },
                new int[] { 1, 1, 1, 0 },
                new int[] { 1, 1, 1, 1 },
            };
            int[] outputs = new int[]
            {
                1, 1, 0, 0, 1, 0, 0, 0,
                1, 1, 1, 0, 0, 0, 0, 0,
            };

            if (inputs.Length != outputs.Length)
            {
                throw new IndexOutOfRangeException("mismatched Array lenths");
            }

            if (true)
            {
                id3.Learn(inputs, outputs);
            }
            else
            {
                c45.Learn(inputs, outputs);
            }

            dectree.PrintTree(prevDecision: "Start").Write();
            Console.Write("\n--Written As Expression--\n" + dectree.ToRules().ToString());

            while (true)
            {
                string   inputString  = Console.ReadLine();
                string[] inputStrings = inputString.Split(new char[] { '-', '.', ',', ';', ':' });
                int[]    inputAsInt   = new int[] {
                    inputStrings[0].ToInt(replacementOnError: 0, intervall: new System.Drawing.Point(0, 1)),
                    inputStrings[1].ToInt(replacementOnError: 0, intervall: new System.Drawing.Point(0, 1)),
                    inputStrings[2].ToInt(replacementOnError: 0, intervall: new System.Drawing.Point(0, 1)),
                    inputStrings[3].ToInt(replacementOnError: 0, intervall: new System.Drawing.Point(0, 1)),
                };
                for (int i = 0; i < inputAsInt.Length; i++)
                {
                    string message;
                    inputDict.TryGetValue(i + "_" + inputAsInt[i], out message);
                    Console.WriteLine(message);
                }
                int    result = dectree.Decide(inputAsInt);
                string convertedResult;
                outputDict.TryGetValue(result, out convertedResult);
                Console.WriteLine("==>" + convertedResult);
            }
        }
Exemplo n.º 30
0
        static void Main(string[] args)
        {
            // Create a new reader, opening a given path
            ExcelReader excel     = new ExcelReader("Intake Inf Cohort 2017 - Training Set.xlsx");
            ExcelReader excelTest = new ExcelReader("Intake Inf Cohort 2017 - Test Set.xlsx");

            // Afterwards, we can query the file for all
            // worksheets within the specified workbook:
            string[] sheets     = excel.GetWorksheetList();
            string[] sheetsTest = excelTest.GetWorksheetList();

            // Finally, we can request an specific sheet:
            DataTable data     = excel.GetWorksheet(sheets[0]);
            DataTable dataTest = excelTest.GetWorksheet(sheets[0]);

            // Loop through each column in data
            foreach (DataColumn column in data.Columns)
            {
                // Replace empty with underscore
                column.ColumnName = column.ColumnName.Replace(" ", "_");
            }

            // Create a new codification codebook to
            // convert strings into integer symbols
            Codification codibook = new Codification(data);

            // Set codibook
            Codification = codibook;

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

            int[][] inputs = symbols.ToJagged <int>(
                codibook.Columns[5].ColumnName,
                codibook.Columns[7].ColumnName,
                codibook.Columns[8].ColumnName,
                codibook.Columns[9].ColumnName,
                codibook.Columns[12].ColumnName,
                codibook.Columns[13].ColumnName,
                codibook.Columns[14].ColumnName,
                codibook.Columns[15].ColumnName,
                codibook.Columns[16].ColumnName,
                codibook.Columns[20].ColumnName,
                codibook.Columns[29].ColumnName,
                codibook.Columns[30].ColumnName,
                codibook.Columns[34].ColumnName
                );
            int[] outputs = symbols.ToMatrix <int>(codibook.Columns[6].ColumnName).GetColumn(0);

            // Create a teacher ID3 algorithm
            var id3 = new ID3Learning()
            {
                new DecisionVariable(codibook.Columns[5].ColumnName, 2),
                new DecisionVariable(codibook.Columns[7].ColumnName, codibook.Columns[7].NumberOfSymbols),
                new DecisionVariable(codibook.Columns[8].ColumnName, codibook.Columns[8].NumberOfSymbols),
                new DecisionVariable(codibook.Columns[9].ColumnName, 3),
                new DecisionVariable(codibook.Columns[12].ColumnName, 10),
                new DecisionVariable(codibook.Columns[13].ColumnName, 10),
                new DecisionVariable(codibook.Columns[14].ColumnName, 10),
                new DecisionVariable(codibook.Columns[15].ColumnName, 10),
                new DecisionVariable(codibook.Columns[16].ColumnName, 2),
                new DecisionVariable(codibook.Columns[20].ColumnName, 2),
                new DecisionVariable(codibook.Columns[29].ColumnName, 2),
                new DecisionVariable(codibook.Columns[30].ColumnName, 2),
                new DecisionVariable(codibook.Columns[34].ColumnName, 2),
            };

            // Learn the training instances!
            Accord.MachineLearning.DecisionTrees.DecisionTree tree = id3.Learn(inputs, outputs);

            // Create a console table for display
            ConsoleTable table = new ConsoleTable("Studentnumber", "Advice", "Conclusion");

            // Loop through each row in data
            foreach (DataRow row in dataTest.Rows)
            {
                // The tree can now be queried for new examples through
                // its decide method. For example, we can create a query
                int[] query = null;

                try
                {
                    query = codibook.Transform(new[, ]
                    {
                        { codibook.Columns[5].ColumnName, row.ItemArray[5].ToString() },
                        { codibook.Columns[7].ColumnName, row.ItemArray[7].ToString() },
                        { codibook.Columns[8].ColumnName, row.ItemArray[8].ToString() },
                        { codibook.Columns[9].ColumnName, row.ItemArray[9].ToString() },
                        { codibook.Columns[12].ColumnName, row.ItemArray[12].ToString() },
                        { codibook.Columns[13].ColumnName, row.ItemArray[13].ToString() },
                        { codibook.Columns[14].ColumnName, row.ItemArray[14].ToString() },
                        { codibook.Columns[15].ColumnName, row.ItemArray[15].ToString() },
                        { codibook.Columns[16].ColumnName, row.ItemArray[16].ToString() },
                        { codibook.Columns[20].ColumnName, row.ItemArray[20].ToString() },
                        { codibook.Columns[29].ColumnName, row.ItemArray[29].ToString() },
                        { codibook.Columns[30].ColumnName, row.ItemArray[30].ToString() },
                        { codibook.Columns[34].ColumnName, row.ItemArray[34].ToString() },
                    });
                }
                catch (Exception)
                {
                    // Show the result of skipped students
                    var studentnumber = row.ItemArray[0].ToString();
                    var advice        = row.ItemArray[6].ToString();
                    var conclusion    = "(Twijfel)";
                    table.AddRow(studentnumber, advice, conclusion);

                    continue;
                }

                // And then predict the label using
                int predicted = tree.Decide(query);

                // Any predictions off are ignored for consistency
                if (predicted != -1)
                {
                    // We can translate it back to strings using
                    string answer = codibook.Revert("advies", predicted);

                    // Show the result in the output
                    var studentnumber = row.ItemArray[0].ToString();
                    var advice        = row.ItemArray[6].ToString();
                    var conclusion    = answer;
                    table.AddRow(studentnumber, advice, conclusion);
                }
                else
                {
                    // Show the result of skipped students
                    var studentnumber = row.ItemArray[0].ToString();
                    var advice        = row.ItemArray[6].ToString();
                    var conclusion    = "(Twijfel)";
                    table.AddRow(studentnumber, advice, conclusion);
                }
            }

            // Write the table in console
            table.Write();

            // Read Key
            Console.ReadKey();
        }