重新创建 Accord.NET DecisionTree 网站示例 - 给定的键不在字典中
Recreating Accord.NET DecisionTree website example - Given keys not present in the dictionary
我完全按照 Accord.net 网站上的示例进行操作:
Link to the example, scroll to the bottom
我的代码是网站示例中代码的副本。我有正确的 NuGet 包。
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);
// 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);
我得到这个异常:
The given key was not present in the dictionary.
在这行代码:
string answer = codebook.Translate("PlayTennis",
tree.Compute(codebook.Translate("Sunny", "Hot", "High", "Strong")));
我从哪里开始解决这个问题?我已经检查了调试器中的密码本,它包含了应有的元素。
框架文档似乎有点过时了。而不是将字典创建为
Codification codebook = new Codification(data);
框架现在期望用户在 Codification 的构造函数中传递感兴趣的列。因此,Codification 字典现在应该创建为
Codification codebook = new Codification(data,
"Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");
文档将在几天后重建!
我完全按照 Accord.net 网站上的示例进行操作:
Link to the example, scroll to the bottom
我的代码是网站示例中代码的副本。我有正确的 NuGet 包。
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);
// 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);
我得到这个异常:
The given key was not present in the dictionary.
在这行代码:
string answer = codebook.Translate("PlayTennis",
tree.Compute(codebook.Translate("Sunny", "Hot", "High", "Strong")));
我从哪里开始解决这个问题?我已经检查了调试器中的密码本,它包含了应有的元素。
框架文档似乎有点过时了。而不是将字典创建为
Codification codebook = new Codification(data);
框架现在期望用户在 Codification 的构造函数中传递感兴趣的列。因此,Codification 字典现在应该创建为
Codification codebook = new Codification(data,
"Outlook", "Temperature", "Humidity", "Wind", "PlayTennis");
文档将在几天后重建!