无法使用Accord.Net框架实现基本决策树

时间:2017-04-23 18:10:15

标签: c# visual-studio id3

我正在尝试学习如何在C#中实现决策树。我是这个主题的新手,我正在使用Accord网站上的例子来学习。 我的代码都是从网站上复制过来的,但是visual studio一直告诉我: visual studio无法启动调试,因为缺少调试目标。 我的代码就是这个:

    using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Accord;
using Accord.MachineLearning.DecisionTrees;
using Accord.MachineLearning.DecisionTrees.Learning;
using Accord.Math;
using Accord.Statistics.Filters;

namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {


            DataTable data = new DataTable("Mitchell's Tennis Example");

            data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");

            data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No");
            data.Rows.Add("D2", "Sunny", "Hot", "High", "Strong", "No");
            data.Rows.Add("D3", "Overcast", "Hot", "High", "Weak", "Yes");
            data.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes");
            data.Rows.Add("D5", "Rain", "Cool", "Normal", "Weak", "Yes");
            data.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No");
            data.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes");
            data.Rows.Add("D8", "Sunny", "Mild", "High", "Weak", "No");
            data.Rows.Add("D9", "Sunny", "Cool", "Normal", "Weak", "Yes");
            data.Rows.Add("D10", "Rain", "Mild", "Normal", "Weak", "Yes");
            data.Rows.Add("D11", "Sunny", "Mild", "Normal", "Strong", "Yes");
            data.Rows.Add("D12", "Overcast", "Mild", "High", "Strong", "Yes");
            data.Rows.Add("D13", "Overcast", "Hot", "Normal", "Weak", "Yes");
            data.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No");

            // Create a new codification codebook to 
            // convert strings into integer symbols
            Codification codebook = new Codification(data, "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");


            // Translate our training data into integer symbols using our codebook:
            DataTable symbols = codebook.Apply(data);
            int[][] inputs = symbols.ToArray<int>("Outlook", "Temperature", "Humidity", "Wind");
            int[] outputs = symbols.ToArray<int>("PlayTennis");

            // Gather information about decision variables
            DecisionVariable[] attributes =
{
            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) 
    };

            int classCount = 2; // 2 possible output values for playing tennis: yes or no

            //Create the decision tree using the attributes and classes
            DecisionTree tree = new DecisionTree(attributes, classCount);

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

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

            string answer = codebook.Translate("PlayTennis", tree.Compute(codebook.Translate("Sunny", "Hot", "High", "Strong")));

            Console.WriteLine("Calculate for: Sunny, Hot, High, Strong");
            Console.WriteLine("Answer: " + answer);

        }
    }
}

P.S。我正在使用2017年的视觉工作室社区。

1 个答案:

答案 0 :(得分:1)

来自this SO question

  
      
  1. 确保项目的输出路径正确(项目&gt;属性&gt;构建&gt;输出路径)
  2.   
  3. 进入菜单构建&gt; Configuration Manager,并检查您的主/入项目是否已选中Build。如果没有,请检查它。
  4.   

这似乎解决了大多数用户的问题。