Click or drag to resize
Accord.NET (logo)

DecisionRule Class

Decision Rule.
Inheritance Hierarchy
SystemObject
  Accord.MachineLearning.DecisionTrees.RulesDecisionRule

Namespace:  Accord.MachineLearning.DecisionTrees.Rules
Assembly:  Accord.MachineLearning (in Accord.MachineLearning.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
public class DecisionRule : ICloneable, 
	IEnumerable, IEquatable<DecisionRule>, IComparable<DecisionRule>
Request Example View Source

The DecisionRule type exposes the following members.

Constructors
Properties
  NameDescription
Public propertyAntecedents
Gets the Antecedent expressions that must be fulfilled in order for this rule to be applicable.
Public propertyCount
Gets the number of antecedents contained in this DecisionRule.
Public propertyOutput
Gets or sets the output of this decision rule, given when all Antecedent conditions are met.
Public propertyVariables
Gets the decision variables handled by this rule.
Top
Methods
  NameDescription
Public methodClone
Creates a new object that is a copy of the current instance.
Public methodCompareTo
Compares this instance to another DecisionRule.
Public methodEquals(Object)
Determines whether the specified Object is equal to this instance.
(Overrides ObjectEquals(Object).)
Public methodEquals(DecisionRule)
Determines whether the specified DecisionRule is equal to this instance.
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodStatic memberFromNode
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.
Public methodGetEnumerator
Returns an enumerator that iterates through a collection.
Public methodGetHashCode
Returns a hash code for this instance.
(Overrides ObjectGetHashCode.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodIsInconsistentWith
Gets whether this rule and another rule have the same antecedents but different outputs.
Public methodMatch
Checks whether a the rule applies to a given input vector.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a String that represents this instance.
(Overrides ObjectToString.)
Public methodToString(CultureInfo)
Returns a String that represents this instance.
Public methodToString(CodificationString)
Returns a String that represents this instance.
Public methodToString(CodificationString, CultureInfo)
Returns a String that represents this instance.
Public methodToString(CodificationString, String, CultureInfo)
Returns a String that represents this instance.
Top
Operators
  NameDescription
Public operatorStatic memberEquality
Implements the operator ==.
Public operatorStatic memberGreaterThan
Implements the operator >.
Public operatorStatic memberInequality
Implements the operator !=.
Public operatorStatic memberLessThan
Implements the operator <.
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodSetEqualsAntecedent
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.)
Public Extension MethodTo(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.)
Public Extension MethodToTOverloaded.
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.)
Top
Examples

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)
";
See Also