Я пытаюсь изучить машинное обучение и, в частности, деревья решений, я скопировал этот фрагмент кода с веб-сайта Accord .Net framework, и, похоже, он у меня не работает, и я не могу понять, почему. Ошибка, которую он мне дает, находится в строке 40: «System.IndexOutOfRangeException:« Индекс был за пределами массива ». Я не уверен, что я ошибаюсь, набор данных, который он использует, находится здесь: https://en.wikipedia.org/wiki/Iris_flower_data_set Возможно, у меня возникли проблемы с его вводом данные установлены правильно? Кстати, я использую Visual Studio Community 2017.
Это код:
using Accord.MachineLearning.DecisionTrees;
using Accord.MachineLearning.DecisionTrees.Learning;
using Accord.MachineLearning.DecisionTrees.Rules;
using Accord.Math;
using Accord.Math.Optimization.Losses;
using Accord.Statistics.Filters;
using ConsoleApp2.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp2
{
class Program
{
static void Main(string[] args)
{
// In this example, we will process the famous Fisher's Iris dataset in
// which the task is to classify weather the features of an Iris flower
// belongs to an Iris setosa, an Iris versicolor, or an Iris virginica:
//
// - https://en.wikipedia.org/wiki/Iris_flower_data_set
//
// First, let's load the dataset into an array of text that we can process
// In this example, we will process the famous Fisher's Iris dataset in
// which the task is to classify weather the features of an Iris flower
// belongs to an Iris setosa, an Iris versicolor, or an Iris virginica:
//
// - https://en.wikipedia.org/wiki/Iris_flower_data_set
//
// First, let's load the dataset into an array of text that we can process
string[][] text = Resources.iris_data.Split(new[] { "\r\n" },
StringSplitOptions.RemoveEmptyEntries).Apply(x => x.Split(','));
// The first four columns contain the flower features
double [][] inputs = text.GetColumns(0, 1, 2, 3).To<double[][]>();
// The last column contains the expected flower type
string[] labels = text.GetColumn(4);
// Since the labels are represented as text, the first step is to convert
// those text labels into integer class labels, so we can process them
// more easily. For this, we will create a codebook to encode class labels:
//
var codebook = new Codification("Output", labels);
// With the codebook, we can convert the labels:
int[] outputs = codebook.Translate("Output", labels);
// Let's declare the names of our input variables:
DecisionVariable[] features =
{
new DecisionVariable("sepal length", DecisionVariableKind.Continuous),
new DecisionVariable("sepal width", DecisionVariableKind.Continuous),
new DecisionVariable("petal length", DecisionVariableKind.Continuous),
new DecisionVariable("petal width", DecisionVariableKind.Continuous),
};
// Now, we can finally create our tree for the 3 classes:
var tree = new DecisionTree(inputs: features, classes: 3);
// And we can use the C4.5 for learning:
var teacher = new C45Learning(tree);
// And finally induce the tree:
teacher.Learn(inputs, outputs);
// To get the estimated class labels, we can use
int[] predicted = tree.Decide(inputs);
// And the classification error (of 0.0266) can be computed as
double error = new ZeroOneLoss(outputs).Loss(tree.Decide(inputs));
// Moreover, we may decide to convert our tree to a set of rules:
DecisionSet rules = tree.ToRules();
// And using the codebook, we can inspect the tree reasoning:
string ruleText = rules.ToString(codebook, "Output",
System.Globalization.CultureInfo.InvariantCulture);
// The output is:
string expected = @"Iris-setosa =: (petal length <= 2.45)
Iris-versicolor =: (petal length > 2.45) && (petal width <= 1.75) && (sepal length <= 7.05) && (sepal width <= 2.85)
Iris-versicolor =: (petal length > 2.45) && (petal width <= 1.75) && (sepal length <= 7.05) && (sepal width > 2.85)
Iris-versicolor =: (petal length > 2.45) && (petal width > 1.75) && (sepal length <= 5.95) && (sepal width > 3.05)
Iris-virginica =: (petal length > 2.45) && (petal width <= 1.75) && (sepal length > 7.05)
Iris-virginica =: (petal length > 2.45) && (petal width > 1.75) && (sepal length > 5.95)
Iris-virginica =: (petal length > 2.45) && (petal width > 1.75) && (sepal length <= 5.95) && (sepal width <= 3.05)
";
Console.WriteLine("expected");
Console.ReadLine();
}
}
}