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

Posted

技术标签:

【中文标题】无法使用 Accord.Net 框架实现基本决策树【英文标题】:Can't implement a basic decision tree using Accord.Net framework 【发布时间】:2017-04-23 18:10:14 【问题描述】:

我正在尝试学习如何在 C# 中实现决策树。我对这个主题真的很陌生,我正在使用 Accord 网站上给出的示例来学习。 我的代码都是从网站上复制的,但视觉工作室一直告诉我: 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);

        
    

附:我正在使用 Visual Studio Community 2017。

【问题讨论】:

您的项目是否构建?你设置好启动项目了吗? 是的,它构建得很好,我已经设置了启动项目......但我仍然遇到同样的错误 我建议您从头开始创建一个新项目并重新构建它。示例代码构建和运行没有问题,EXCEPT,您需要将这一行 data.Columns.Add("Day", "Outlook", "Temperature", "Humidity", "Wind", "PlayTennis"); 替换为单独的调用,如下所示:data.Columns.Add("Day");data.Columns.Add("Outlook");data.Columns.Add("Temperature");data.Columns.Add("Humidity");@987654327 @data.Columns.Add("PlayTennis");. 您的问题和问题与 Accord.net 和 ID3 算法没有任何关系。但这是您项目中的任何其他代码都可能发生的一般问题。 【参考方案1】:

来自this SO question

    确保项目的输出路径正确(项目>属性>构建>输出路径) 进入菜单到 Build > Configuration Manager,并检查您的主/入口项目是否已选中 Build。如果没有,请检查。

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

【讨论】:

它并没有解决我的问题,但还是谢谢

以上是关于无法使用 Accord.Net 框架实现基本决策树的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 C# 在 Accord.NET 中构建观察数组

在 Accord.Net SVM 中有一个“未识别”类

决策树方法的基本思想是啥

机器学习-决策树的基本思想

使用 C# Accord .Net 从网络摄像头录制视频

Accord.Net - LibLinear 上的 CacheSize