1
我正試圖學習如何在C#中實現決策樹。我對這個主題非常陌生,我使用Accord網站上給出的例子來學習。 我的代碼全部從網站複製,但Visual Studio一直告訴我: 由於缺少調試目標,Visual Studio無法開始調試。 我的代碼是這一個:無法使用Accord.Net框架實現基本決策樹
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.我使用的Visual Studio 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「);' 'data.Columns.Add(」Wind「);' 'data.Columns.Add(」PlayTennis「);'。 – jsanalytics