Search code examples
c#.netmachine-learningdecision-treeaccord.net

How to correctly provide input data for Accord.NET DecisionTrees


I'm trying to learn machine learning and in particular decision trees, I copied this piece of code from the Accord .Net framework website and it doesn't seem to be working for me, and I can't figure out why. The error it gives me is on line 40 saying:"System.IndexOutOfRangeException: 'Index was outside the bounds of the array.'" I'm not sure what I'm getting wrong, the data set it uses is found here: https://en.wikipedia.org/wiki/Iris_flower_data_set Maybe I'm having trouble giving it the data set in the correct manner? By the way I'm using Visual Studio Community 2017.

This is the code:

using Accord.MachineLearning.DecisionTrees;
using Accord.MachineLearning.DecisionTrees.Learning;
using Accord.MachineLearning.DecisionTrees.Rules;
using Accord.Math;
using Accord.Math.Optimization.Losses;
using Accord.Statistics.Filters;
using ConsoleApp2.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            // In this example, we will process the famous Fisher's Iris dataset in 
            // which the task is to classify weather the features of an Iris flower 
            // belongs to an Iris setosa, an Iris versicolor, or an Iris virginica:
            // 
            //  - https://en.wikipedia.org/wiki/Iris_flower_data_set
            // 

            // First, let's load the dataset into an array of text that we can process
             // In this example, we will process the famous Fisher's Iris dataset in 
            // which the task is to classify weather the features of an Iris flower 
            // belongs to an Iris setosa, an Iris versicolor, or an Iris virginica:
            // 
            //  - https://en.wikipedia.org/wiki/Iris_flower_data_set
            // 

            // First, let's load the dataset into an array of text that we can process
            string[][] text = Resources.iris_data.Split(new[] { "\r\n" },
                StringSplitOptions.RemoveEmptyEntries).Apply(x => x.Split(','));

            // The first four columns contain the flower features
            double [][] inputs = text.GetColumns(0, 1, 2, 3).To<double[][]>();

            // The last column contains the expected flower type
            string[] labels = text.GetColumn(4);

            // Since the labels are represented as text, the first step is to convert
            // those text labels into integer class labels, so we can process them
            // more easily. For this, we will create a codebook to encode class labels:
            // 
            var codebook = new Codification("Output", labels);

            // With the codebook, we can convert the labels:
            int[] outputs = codebook.Translate("Output", labels);

            // Let's declare the names of our input variables:
            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), 
            };

            // Now, we can finally create our tree for the 3 classes:
            var tree = new DecisionTree(inputs: features, classes: 3);

            // And we can use the C4.5 for learning:
            var teacher = new C45Learning(tree);

            // And finally induce the tree:
            teacher.Learn(inputs, outputs);

            // To get the estimated class labels, we can use
            int[] predicted = tree.Decide(inputs);

            // And the classification error (of 0.0266) can be computed as 
            double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs));

            // Moreover, we may decide to convert our tree to a set of rules:
            DecisionSet rules = tree.ToRules();

            // And using the codebook, we can inspect the tree reasoning:
            string ruleText = rules.ToString(codebook, "Output",
                System.Globalization.CultureInfo.InvariantCulture);

            // The output is:
            string expected = @"Iris-setosa =: (petal length <= 2.45)
Iris-versicolor =: (petal length > 2.45) && (petal width <= 1.75) && (sepal length <= 7.05) && (sepal width <= 2.85)
Iris-versicolor =: (petal length > 2.45) && (petal width <= 1.75) && (sepal length <= 7.05) && (sepal width > 2.85)
Iris-versicolor =: (petal length > 2.45) && (petal width > 1.75) && (sepal length <= 5.95) && (sepal width > 3.05)
Iris-virginica =: (petal length > 2.45) && (petal width <= 1.75) && (sepal length > 7.05)
Iris-virginica =: (petal length > 2.45) && (petal width > 1.75) && (sepal length > 5.95)
Iris-virginica =: (petal length > 2.45) && (petal width > 1.75) && (sepal length <= 5.95) && (sepal width <= 3.05)
";

            Console.WriteLine("expected");
            Console.ReadLine();

        }
    }
}

Solution

  • Judging by the code sample itself, all you need is a static class containing your data in CSV format:

        static public class Resources
        {
            public static string iris_data = 
    @"7.9,3.8,6.4,2,I. virginica
    7.7,3.8,6.7,2.2,I. virginica
    7.7,2.6,6.9,2.3,I. virginica
    7.7,2.8,6.7,2,I. virginica
    7.7,3,6.1,2.3,I. virginica
    7.6,3,6.6,2.1,I. virginica
    7.4,2.8,6.1,1.9,I. virginica
    7.3,2.9,6.3,1.8,I. virginica
    7.2,3.6,6.1,2.5,I. virginica
    7.2,3.2,6,1.8,I. virginica
    7.2,3,5.8,1.6,I. virginica
    7.1,3,5.9,2.1,I. virginica
    7,3.2,4.7,1.4,I. versicolor
    6.9,3.1,4.9,1.5,I. versicolor
    6.9,3.2,5.7,2.3,I. virginica
    6.9,3.1,5.4,2.1,I. virginica
    6.9,3.1,5.1,2.3,I. virginica
    6.8,2.8,4.8,1.4,I. versicolor
    6.8,3,5.5,2.1,I. virginica
    6.8,3.2,5.9,2.3,I. virginica
    6.7,3.1,4.4,1.4,I. versicolor
    6.7,3,5,1.7,I. versicolor
    6.7,3.1,4.7,1.5,I. versicolor
    6.7,2.5,5.8,1.8,I. virginica
    6.7,3.3,5.7,2.1,I. virginica
    6.7,3.1,5.6,2.4,I. virginica
    6.7,3.3,5.7,2.5,I. virginica
    6.7,3,5.2,2.3,I. virginica
    6.6,2.9,4.6,1.3,I. versicolor
    6.6,3,4.4,1.4,I. versicolor
    6.5,2.8,4.6,1.5,I. versicolor
    6.5,3,5.8,2.2,I. virginica
    6.5,3.2,5.1,2,I. virginica
    6.5,3,5.5,1.8,I. virginica
    6.5,3,5.2,2,I. virginica
    6.4,3.2,4.5,1.5,I. versicolor
    6.4,2.9,4.3,1.3,I. versicolor
    6.4,2.7,5.3,1.9,I. virginica
    6.4,3.2,5.3,2.3,I. virginica
    6.4,2.8,5.6,2.1,I. virginica
    6.4,2.8,5.6,2.2,I. virginica
    6.4,3.1,5.5,1.8,I. virginica
    6.3,3.3,4.7,1.6,I. versicolor
    6.3,2.5,4.9,1.5,I. versicolor
    6.3,2.3,4.4,1.3,I. versicolor
    6.3,3.3,6,2.5,I. virginica
    6.3,2.9,5.6,1.8,I. virginica
    6.3,2.7,4.9,1.8,I. virginica
    6.3,2.8,5.1,1.5,I. virginica
    6.3,3.4,5.6,2.4,I. virginica
    6.3,2.5,5,1.9,I. virginica
    6.2,2.2,4.5,1.5,I. versicolor
    6.2,2.9,4.3,1.3,I. versicolor
    6.2,2.8,4.8,1.8,I. virginica
    6.2,3.4,5.4,2.3,I. virginica
    6.1,2.9,4.7,1.4,I. versicolor
    6.1,2.8,4,1.3,I. versicolor
    6.1,2.8,4.7,1.2,I. versicolor
    6.1,3,4.6,1.4,I. versicolor
    6.1,3,4.9,1.8,I. virginica
    6.1,2.6,5.6,1.4,I. virginica
    6,2.2,4,1,I. versicolor
    6,2.9,4.5,1.5,I. versicolor
    6,2.7,5.1,1.6,I. versicolor
    6,3.4,4.5,1.6,I. versicolor
    6,2.2,5,1.5,I. virginica
    6,3,4.8,1.8,I. virginica
    5.9,3,4.2,1.5,I. versicolor
    5.9,3.2,4.8,1.8,I. versicolor
    5.9,3,5.1,1.8,I. virginica
    5.8,4,1.2,0.2,I. setosa
    5.8,2.7,4.1,1,I. versicolor
    5.8,2.7,3.9,1.2,I. versicolor
    5.8,2.6,4,1.2,I. versicolor
    5.8,2.7,5.1,1.9,I. virginica
    5.8,2.8,5.1,2.4,I. virginica
    5.8,2.7,5.1,1.9,I. virginica
    5.7,4.4,1.5,0.4,I. setosa
    5.7,3.8,1.7,0.3,I. setosa
    5.7,2.8,4.5,1.3,I. versicolor
    5.7,2.6,3.5,1,I. versicolor
    5.7,3,4.2,1.2,I. versicolor
    5.7,2.9,4.2,1.3,I. versicolor
    5.7,2.8,4.1,1.3,I. versicolor
    5.7,2.5,5,2,I. virginica
    5.6,2.9,3.6,1.3,I. versicolor
    5.6,3,4.5,1.5,I. versicolor
    5.6,2.5,3.9,1.1,I. versicolor
    5.6,3,4.1,1.3,I. versicolor
    5.6,2.7,4.2,1.3,I. versicolor
    5.6,2.8,4.9,2,I. virginica
    5.5,4.2,1.4,0.2,I. setosa
    5.5,3.5,1.3,0.2,I. setosa
    5.5,2.3,4,1.3,I. versicolor
    5.5,2.4,3.8,1.1,I. versicolor
    5.5,2.4,3.7,1,I. versicolor
    5.5,2.5,4,1.3,I. versicolor
    5.5,2.6,4.4,1.2,I. versicolor
    5.4,3.9,1.7,0.4,I. setosa
    5.4,3.7,1.5,0.2,I. setosa
    5.4,3.9,1.3,0.4,I. setosa
    5.4,3.4,1.7,0.2,I. setosa
    5.4,3.4,1.5,0.4,I. setosa
    5.4,3,4.5,1.5,I. versicolor
    5.3,3.7,1.5,0.2,I. setosa
    5.2,3.5,1.5,0.2,I. setosa
    5.2,3.4,1.4,0.2,I. setosa
    5.2,4.1,1.5,0.1,I. setosa
    5.2,2.7,3.9,1.4,I. versicolor
    5.1,3.5,1.4,0.2,I. setosa
    5.1,3.5,1.4,0.3,I. setosa
    5.1,3.8,1.5,0.3,I. setosa
    5.1,3.7,1.5,0.4,I. setosa
    5.1,3.3,1.7,0.5,I. setosa
    5.1,3.4,1.5,0.2,I. setosa
    5.1,3.8,1.9,0.4,I. setosa
    5.1,3.8,1.6,0.2,I. setosa
    5.1,2.5,3,1.1,I. versicolor
    5,3.6,1.4,0.2,I. setosa
    5,3.4,1.5,0.2,I. setosa
    5,3,1.6,0.2,I. setosa
    5,3.4,1.6,0.4,I. setosa
    5,3.2,1.2,0.2,I. setosa
    5,3.5,1.3,0.3,I. setosa
    5,3.5,1.6,0.6,I. setosa
    5,3.3,1.4,0.2,I. setosa
    5,2,3.5,1,I. versicolor
    5,2.3,3.3,1,I. versicolor
    4.9,3,1.4,0.2,I. setosa
    4.9,3.1,1.5,0.1,I. setosa
    4.9,3.1,1.5,0.2,I. setosa
    4.9,3.6,1.4,0.1,I. setosa
    4.9,2.4,3.3,1,I. versicolor
    4.9,2.5,4.5,1.7,I. virginica
    4.8,3.4,1.6,0.2,I. setosa
    4.8,3,1.4,0.1,I. setosa
    4.8,3.4,1.9,0.2,I. setosa
    4.8,3.1,1.6,0.2,I. setosa
    4.8,3,1.4,0.3,I. setosa
    4.7,3.2,1.3,0.2,I. setosa
    4.7,3.2,1.6,0.2,I. setosa
    4.6,3.1,1.5,0.2,I. setosa
    4.6,3.4,1.4,0.3,I. setosa
    4.6,3.6,1,0.2,I. setosa
    4.6,3.2,1.4,0.2,I. setosa
    4.5,2.3,1.3,0.3,I. setosa
    4.4,2.9,1.4,0.2,I. setosa
    4.4,3,1.3,0.2,I. setosa
    4.4,3.2,1.3,0.2,I. setosa
    4.3,3,1.1,0.1,I. setosa
    ";
        }
    

    Also, you may want to compare your expected and actual results:

    Console.WriteLine("expected = \n{0}", expected);
    Console.WriteLine("ruleText = \n{0}", ruleText);
    

    It should give you something like this:

    expected =
    Iris-setosa =: (2 <= 2.45)
    Iris-versicolor =: (2 > 2.45) && (3 <= 1.75) && (0 <= 7.05) && (1 <= 2.85)
    Iris-versicolor =: (2 > 2.45) && (3 <= 1.75) && (0 <= 7.05) && (1 > 2.85)
    Iris-versicolor =: (2 > 2.45) && (3 > 1.75) && (0 <= 5.95) && (1 > 3.05)
    Iris-virginica =: (2 > 2.45) && (3 <= 1.75) && (0 > 7.05)
    Iris-virginica =: (2 > 2.45) && (3 > 1.75) && (0 > 5.95)
    Iris-virginica =: (2 > 2.45) && (3 > 1.75) && (0 <= 5.95) && (1 <= 3.05)
    
    ruleText =
    I. virginica =: (2 > 2.45) && (3 <= 1.75) && (0 > 7.05)
    I. virginica =: (2 > 2.45) && (3 > 1.75) && (0 > 5.95)
    I. virginica =: (2 > 2.45) && (3 > 1.75) && (0 <= 5.95) && (1 <= 3.05)
    I. versicolor =: (2 > 2.45) && (3 <= 1.75) && (0 <= 7.05) && (1 <= 2.85)
    I. versicolor =: (2 > 2.45) && (3 <= 1.75) && (0 <= 7.05) && (1 > 2.85)
    I. versicolor =: (2 > 2.45) && (3 > 1.75) && (0 <= 5.95) && (1 > 3.05)
    I. setosa =: (2 <= 2.45)