本文整理汇总了C#中Accord.MachineLearning.DecisionTrees.DecisionTree.ToRules方法的典型用法代码示例。如果您正苦于以下问题:C# DecisionTree.ToRules方法的具体用法?C# DecisionTree.ToRules怎么用?C# DecisionTree.ToRules使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Accord.MachineLearning.DecisionTrees.DecisionTree
的用法示例。
在下文中一共展示了DecisionTree.ToRules方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: AttributeReuseTest1
public void AttributeReuseTest1()
{
string[][] text = Resources.iris_data.Split(
new[] { '\n' }, StringSplitOptions.RemoveEmptyEntries)
.Apply(x => x.Split(','));
double[][] inputs = new double[text.Length][];
for (int i = 0; i < inputs.Length; i++)
inputs[i] = text[i].First(4).Convert(s => Double.Parse(s, System.Globalization.CultureInfo.InvariantCulture));
string[] labels = text.GetColumn(4);
Codification codebook = new Codification("Label", labels);
int[] outputs = codebook.Translate("Label", labels);
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),
};
DecisionTree tree = new DecisionTree(features, codebook.Columns[0].Symbols);
C45Learning teacher = new C45Learning(tree);
teacher.Join = 3;
double error = teacher.Run(inputs, outputs);
Assert.AreEqual(0.02, error, 1e-10);
DecisionSet rules = tree.ToRules();
double newError = ComputeError(rules, inputs, outputs);
Assert.AreEqual(0.02, newError, 1e-10);
string ruleText = rules.ToString(codebook,
System.Globalization.CultureInfo.InvariantCulture);
// TODO: implement this assertion properly, actually checking
// the text contents once the feature is completely finished.
Assert.AreEqual(600, ruleText.Length);
}
示例2: IrisDatasetTest
public void IrisDatasetTest()
{
#region doc_iris
// 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[] { '\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 can be computed as
double error = new ZeroOneLoss(outputs) // 0.0266
{
Mean = true
}.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)
";
#endregion
Assert.AreEqual(0.026666666666666668, error, 1e-10);
Assert.AreEqual(4, tree.NumberOfInputs);
Assert.AreEqual(3, tree.NumberOfOutputs);
double newError = ComputeError(rules, inputs, outputs);
Assert.AreEqual(0.026666666666666668, newError, 1e-10);
Assert.AreEqual(expected, ruleText);
}
示例3: Run
public void Run(String filename)
{
ReadFile(filename);
// Create a new codification codebook to
// convert strings into integer symbols
Codification codebook = new Codification(data, inputColumns.ToArray());
// Translate our training data into integer symbols using our codebook:
DataTable symbols = codebook.Apply(data);
foreach (String s in inputColumns)
CreateDic(s, symbols);
CreateDic(outputColumn, symbols);
int[][] inputs = (from p in symbols.AsEnumerable()
select GetInputRow(p)
).Cast<int[]>().ToArray();
int[] outputs = (from p in symbols.AsEnumerable()
select GetIndex(outputColumn, p[outputColumn].ToString())).Cast<int>().ToArray();
// Gather information about decision variables
DecisionVariable[] attributes = GetDecisionVariables();
int classCount = GetCount(outputColumn); // 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);
//C45Learning c45learning = new C45Learning(tree);
// Learn the training instances!
id3learning.Run(inputs, outputs);
//c45learning.Run(inputs2, outputs);
/*
string answer = codebook.Translate(outputColumn,
tree.Compute(codebook.Translate("Sunny", "Hot", "High", "Strong")));
Console.WriteLine("Calculate for: Sunny, Hot, High, Strong");
Console.WriteLine("Answer: " + answer);
*/
var expression = tree.ToExpression();
Console.WriteLine(tree.ToCode("ClassTest"));
DecisionSet rules = tree.ToRules();
Console.WriteLine(rules.ToString());
// Compiles the expression to IL
var func = expression.Compile();
}
示例4: Run
public void Run(String filename)
{
ReadFile(filename);
// Now, we have to convert the textual, categorical data found
// in the table to a more manageable discrete representation.
//
// For this, we will create a codebook to translate text to
// discrete integer symbols:
//
Codification codebook = new Codification(data);
// And then convert all data into symbols
//
DataTable symbols = codebook.Apply(data);
for (int i = 0; i < inputColumns.Count; i++)
if (inputTypes[i] == "string")
CreateDic(inputColumns[i], symbols);
CreateDic(outputColumn, symbols);
double[][] inputs = (from p in symbols.AsEnumerable()
select GetInputRow(p)
).Cast<double[]>().ToArray();
int[] outputs = (from p in symbols.AsEnumerable()
select GetIndex(outputColumn, p[outputColumn].ToString())).Cast<int>().ToArray();
// From now on, we can start creating the decision tree.
//
var attributes = DecisionVariable.FromCodebook(codebook, inputColumns.ToArray());
DecisionTree tree = new DecisionTree(attributes, 5); //outputClasses: 5
// Now, let's create the C4.5 algorithm
C45Learning c45 = new C45Learning(tree);
// and learn a decision tree. The value of
// the error variable below should be 0.
//
double error = c45.Run(inputs, outputs);
// To compute a decision for one of the input points,
// such as the 25-th example in the set, we can use
//
//int y = tree.Compute(inputs[25]);
// Finally, we can also convert our tree to a native
// function, improving efficiency considerably, with
//
//Func<double[], int> func = tree.ToExpression().Compile();
// Again, to compute a new decision, we can just use
//
//int z = func(inputs[25]);
var expression = tree.ToExpression();
Console.WriteLine(tree.ToCode("ClassTest"));
DecisionSet s = tree.ToRules();
Console.WriteLine(s.ToString());
}
示例5: Run
public void Run()
{
DataTable data = new DataTable("Mitchell's Tennis Example");
data.Columns.Add("Day");
data.Columns.Add("Outlook");
data.Columns.Add("Temperature");
data.Columns.Add("Humidity");
data.Columns.Add("Wind");
data.Columns.Add("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);
CreateDic("Outlook", symbols);
CreateDic("Temperature", symbols);
CreateDic("Humidity", symbols);
CreateDic("Wind", symbols);
CreateDic("PlayTennis", symbols);
int[][] inputs = (from p in symbols.AsEnumerable()
select new int[]
{
GetIndex("Outlook", p["Outlook"].ToString()),
GetIndex("Temperature", p["Temperature"].ToString()),
GetIndex("Humidity", p["Humidity"].ToString()),
GetIndex("Wind", p["Wind"].ToString())
}).Cast<int[]>().ToArray();
int[] outputs = (from p in symbols.AsEnumerable()
select GetIndex("PlayTennis", p["PlayTennis"].ToString())).Cast<int>().ToArray();
/*
// 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)
};
*/
DecisionVariable[] attributes =
{
new DecisionVariable("Outlook", GetCount("Outlook")), // 3 possible values (Sunny, overcast, rain)
new DecisionVariable("Temperature", GetCount("Temperature")), // 3 possible values (Hot, mild, cool)
new DecisionVariable("Humidity", GetCount("Humidity")), // 2 possible values (High, normal)
new DecisionVariable("Wind", GetCount("Wind")) // 2 possible values (Weak, strong)
};
int classCount = GetCount("PlayTennis"); // 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);
var expression = tree.ToExpression();
Console.WriteLine(tree.ToCode("ClassTest"));
DecisionSet s = tree.ToRules();
Console.WriteLine(s.ToString());
// Compiles the expression to IL
var func = expression.Compile();
}