机器学习 -- 决策树C45算法使用实例


测试数据:
//############################### data table ##########################
// Outlook		Temp		Humidity		Windy		Play Golf
// Rainy		Hot		High			False		No
// Overoast		Hot		High			True		No
// Sunny		Mild		High			False		Yes
// Sunny		Cool		Normal			False		Yes
// Sunny		Cool		Normal			True		No
// Overoast		Cool		Normal			True		Yes
// Rainy		Mild		High			False		No
// Rainy		Cool		Normal			False		Yes
// Sunny		Mild		Normal			False		Yes
// Rainy		Mild		Normal			True		Yes
// Overoast		Mild		High			True		Yes
// Overoast		Hot		Normal			False		Yes
// Sunny		Mild		High			True		No
//######################################################################




示例代码 (C# + accord framwork):
    
public enum Outlook
    {
        Rainy,
        Overoast,
        Sunny
    }


    public enum Temp
    {
        Hot,
        Mild,
        Cool
    }


    public enum Humidity
    {
        High,
        Normal
    }


    public enum Play_Golf
    {
        No,
        Yes
    }


    public static class C45Demo
    {
        public static void Execute()
        {


            string[] inputColumns =
            {
                "Outlook","Temp","Humidity","Windy"
            };


            string outputColumn = "Play_Golf";




            // Let's populate a data table with this information.
            // 
            DataTable table = new DataTable("Play");
            table.Columns.Add(inputColumns);
            table.Columns.Add(outputColumn);


            table.Rows.Add(Outlook.Rainy, Temp.Hot, Humidity.High, false, Play_Golf.No);
            table.Rows.Add(Outlook.Overoast, Temp.Hot, Humidity.High, true, Play_Golf.No);
            table.Rows.Add(Outlook.Sunny, Temp.Mild, Humidity.High, false, Play_Golf.Yes);


            table.Rows.Add(Outlook.Sunny, Temp.Cool, Humidity.Normal, false, Play_Golf.Yes);
            table.Rows.Add(Outlook.Sunny, Temp.Cool, Humidity.Normal, true, Play_Golf.No);
            table.Rows.Add(Outlook.Overoast, Temp.Cool, Humidity.Normal, true, Play_Golf.Yes);
            table.Rows.Add(Outlook.Rainy, Temp.Mild, Humidity.High, false, Play_Golf.No);
            table.Rows.Add(Outlook.Rainy, Temp.Cool, Humidity.Normal, false, Play_Golf.No);
            table.Rows.Add(Outlook.Sunny, Temp.Mild, Humidity.Normal, false, Play_Golf.No);
            table.Rows.Add(Outlook.Rainy, Temp.Mild, Humidity.Normal, true, Play_Golf.No);
            table.Rows.Add(Outlook.Overoast, Temp.Mild, Humidity.High, true, Play_Golf.No);
            table.Rows.Add(Outlook.Overoast, Temp.Hot, Humidity.Normal, false, Play_Golf.No);
            table.Rows.Add(Outlook.Sunny, Temp.Mild, Humidity.High, true, Play_Golf.No);


            Codification codebook = new Codification(table);


            DataTable symbols = codebook.Apply(table);
            double[][] inputs = symbols.ToArray(inputColumns);
            int[] outputs = symbols.ToArray<int>(outputColumn);


            var attributes = DecisionVariable.FromCodebook(codebook, inputColumns);
            int classCount = 2; // play or not play


            DecisionTree tree = new DecisionTree(attributes, classCount);


            C45Learning c45 = new C45Learning(tree);


            double error = c45.Run(inputs, outputs);


            int result = tree.Compute(inputs[4]);
            Console.WriteLine(result);


        }
    }


static void Main(string[] args)
{
      C45Demo.Execute();
      Console.ReadLine();
}

你可能感兴趣的:(机器学习 -- 决策树C45算法使用实例)