DecisionRule Class |
Namespace: Accord.MachineLearning.DecisionTrees.Rules
[SerializableAttribute] public class DecisionRule : ICloneable, IEnumerable, IEquatable<DecisionRule>, IComparable<DecisionRule>
The DecisionRule type exposes the following members.
Name | Description | |
---|---|---|
DecisionRule(Double, Antecedent) |
Initializes a new instance of the DecisionRule class.
| |
DecisionRule(IListDecisionVariable, Double, Antecedent) |
Initializes a new instance of the DecisionRule class.
| |
DecisionRule(IListDecisionVariable, Double, IEnumerableAntecedent) |
Initializes a new instance of the DecisionRule class.
|
Name | Description | |
---|---|---|
Antecedents |
Gets the Antecedent expressions that
must be fulfilled in order for this rule to be applicable.
| |
Count |
Gets the number of antecedents contained
in this DecisionRule.
| |
Output |
Gets or sets the output of this decision rule, given
when all Antecedent conditions are met.
| |
Variables |
Gets the decision variables handled by this rule.
|
Name | Description | |
---|---|---|
Clone |
Creates a new object that is a copy of the current instance.
| |
CompareTo |
Compares this instance to another DecisionRule.
| |
Equals(Object) |
Determines whether the specified Object is equal to this instance.
(Overrides ObjectEquals(Object).) | |
Equals(DecisionRule) |
Determines whether the specified DecisionRule is equal to this instance.
| |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
FromNode |
Creates a new DecisionRule from a DecisionTree's
DecisionNode. This node must be a leaf, cannot be the root, and
should have one output value.
| |
GetEnumerator |
Returns an enumerator that iterates through a collection.
| |
GetHashCode |
Returns a hash code for this instance.
(Overrides ObjectGetHashCode.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
IsInconsistentWith |
Gets whether this rule and another rule have
the same antecedents but different outputs.
| |
Match |
Checks whether a the rule applies to a given input vector.
| |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
ToString |
Returns a String that represents this instance.
(Overrides ObjectToString.) | |
ToString(CultureInfo) |
Returns a String that represents this instance.
| |
ToString(CodificationString) |
Returns a String that represents this instance.
| |
ToString(CodificationString, CultureInfo) |
Returns a String that represents this instance.
| |
ToString(CodificationString, String, CultureInfo) |
Returns a String that represents this instance.
|
Name | Description | |
---|---|---|
Equality |
Implements the operator ==.
| |
GreaterThan |
Implements the operator >.
| |
Inequality |
Implements the operator !=.
| |
LessThan |
Implements the operator <.
|
Name | Description | |
---|---|---|
HasMethod |
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.) | |
IsEqual |
Compares two objects for equality, performing an elementwise
comparison if the elements are vectors or matrices.
(Defined by Matrix.) | |
SetEqualsAntecedent |
Compares two enumerables for set equality. Two
enumerables are set equal if they contain the
same elements, but not necessarily in the same
order.
(Defined by Matrix.) | |
To(Type) | Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.) | |
ToT | Overloaded.
Converts an object into another type, irrespective of whether
the conversion can be done at compile time or not. This can be
used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.) |
The simplest way to create a set of decision rules is by extracting them from an existing DecisionTree. The example below shows how to create a simple decision tree and convert it to a set of rules using its ToRules method.
// 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); // And we can use the C4.5 for learning: C45Learning teacher = new C45Learning(); // Finally induce the tree from the data: var tree = teacher.Learn(inputs, outputs); // To get the estimated class labels, we can use int[] predicted = tree.Decide(inputs); // The classification error (0.0266) can be computed as double error = new ZeroOneLoss(outputs).Loss(predicted); // 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 =: (2 <= 2.45) Iris-versicolor =: (2 > 2.45) && (3 <= 1.75) && (0 <= 7.05) && (1 <= 2.85) Iris-versicolor =: (2 > 2.45) && (3 <= 1.75) && (0 <= 7.05) && (1 > 2.85) Iris-versicolor =: (2 > 2.45) && (3 > 1.75) && (0 <= 5.95) && (1 > 3.05) Iris-virginica =: (2 > 2.45) && (3 <= 1.75) && (0 > 7.05) Iris-virginica =: (2 > 2.45) && (3 > 1.75) && (0 > 5.95) Iris-virginica =: (2 > 2.45) && (3 > 1.75) && (0 <= 5.95) && (1 <= 3.05) ";