Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
0
        private void button_Click(object sender, RoutedEventArgs e)
        {
            //Decided against the Iris set which is in Accord
            //var iris = new Iris();
            //double[][] inputs = iris.Instances;
            //int[] outputs = iris.ClassLabels;

            string[][] data = DataSet.CustomIris.iris_values.Split(new[] { "\r\n" },
                                                                   StringSplitOptions.RemoveEmptyEntries).Apply(x => x.Split(','));

            //features
            double[][] inputs = data.GetColumns(0, 1, 2, 3).To <double[][]>();

            //labels
            string[] labels = data.GetColumn(4);

            //Codebook translates any input into usable (integers) for the tree
            //var codebook = new Codification(outputs, inputs);
            var cb = new Codification("Output", labels);

            int[] outputs = cb.Transform("Output", labels);

            DecisionVariable[] features =
            {
                new DecisionVariable("sepal length", DecisionVariableKind.Continuous),
                new DecisionVariable("sepal width",  DecisionVariableKind.Continuous),
                new DecisionVariable("petal length", DecisionVariableKind.Continuous),
                new DecisionVariable("petal width",  DecisionVariableKind.Continuous),
            };

            var decisionTree = new DecisionTree(inputs: features, classes: 3);
            var c45learner   = new C45Learning(decisionTree);

            c45learner.Learn(inputs, outputs);

            int[] estimated = decisionTree.Decide(inputs);

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

            //Why rules?
            DecisionSet decisionSet = decisionTree.ToRules();

            string ruleText = decisionSet.ToString(cb, "Output",
                                                   System.Globalization.CultureInfo.InvariantCulture);

            //var tree = new DecisionTree(inputs: features, classes: 3);

            #region UI
            //Set ouput to UI
            tb_output.Text = ruleText;

            //Calculate the flowers and input to UI -> TODO Bindings
            var setosaCount     = 0;
            var versicolorCount = 0;
            var virginicaCount  = 0;

            for (int i = 0; i < estimated.Length; i++)
            {
                if (estimated[i] == 0)
                {
                    setosaCount++;
                }
                if (estimated[i] == 1)
                {
                    versicolorCount++;
                }
                if (estimated[i] == 2)
                {
                    virginicaCount++;
                }
            }

            tb_setosa.Text = setosaCount.ToString();
            tb_versi.Text  = versicolorCount.ToString();
            tb_virgi.Text  = virginicaCount.ToString();
            #endregion UI
        }