DecisionSet Class |
Namespace: Accord.MachineLearning.DecisionTrees.Rules
The DecisionSet type exposes the following members.
Name | Description | |
---|---|---|
DecisionSet |
Initializes a new instance of the DecisionSet class.
| |
DecisionSet(IEnumerableDecisionRule) |
Initializes a new instance of the DecisionSet class.
|
Name | Description | |
---|---|---|
Count |
Gets the number of rules in this set.
| |
NumberOfClasses |
Gets the number of classes expected and recognized by the classifier.
(Inherited from ClassifierBaseTInput, TClasses.) | |
NumberOfInputs |
Gets the number of inputs accepted by the model.
(Inherited from TransformBaseTInput, TOutput.) | |
NumberOfOutputs |
Gets the number of outputs generated by the model.
(Inherited from TransformBaseTInput, TOutput.) | |
OutputClasses | Obsolete.
Obsolete. Please use NumberOfClasses instead.
|
Name | Description | |
---|---|---|
Add |
Adds a new DecisionRule to the set.
| |
AddRange |
Adds a collection of new DecisionRules to the set.
| |
Clear |
Removes all rules from this set.
| |
Compute |
Computes the decision output for a given input.
| |
Decide(TInput) |
Computes class-label decisions for a given set of input vectors.
(Inherited from ClassifierBaseTInput, TClasses.) | |
Decide(Double) |
Computes a class-label decision for a given input.
(Overrides ClassifierBaseTInput, TClassesDecide(TInput).) | |
Decide(TInput, TClasses) |
Computes a class-label decision for a given input.
(Inherited from ClassifierBaseTInput, TClasses.) | |
Decide(TInput, Boolean) |
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBaseTInput.) | |
Decide(TInput, Double) |
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBaseTInput.) | |
Decide(TInput, Int32) |
Computes class-label decisions for the given input.
(Inherited from MulticlassClassifierBaseTInput.) | |
Decide(TInput, Double) |
Computes a class-label decision for a given input.
(Inherited from MulticlassClassifierBaseTInput.) | |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
FromDecisionTree |
Creates a new DecisionSet from a DecisionTree.
| |
GetEnumerator |
Returns an enumerator that iterates through a collection.
| |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
Remove |
Removes a given rule from the set.
| |
ToMultilabel |
Views this instance as a multi-label classifier,
giving access to more advanced methods, such as the prediction
of one-hot vectors.
(Inherited from MulticlassClassifierBaseTInput.) | |
ToString |
Returns a String that represents this instance.
(Overrides ObjectToString.) | |
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.
| |
Transform(TInput) |
Applies the transformation to an input, producing an associated output.
(Inherited from ClassifierBaseTInput, TClasses.) | |
Transform(TInput) |
Applies the transformation to a set of input vectors,
producing an associated set of output vectors.
(Inherited from TransformBaseTInput, TOutput.) | |
Transform(TInput, TClasses) |
Applies the transformation to an input, producing an associated output.
(Inherited from ClassifierBaseTInput, TClasses.) | |
Transform(TInput, Boolean) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.) | |
Transform(TInput, Double) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.) | |
Transform(TInput, Int32) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.) | |
Transform(TInput, Boolean) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.) | |
Transform(TInput, Double) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.) | |
Transform(TInput, Double) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.) | |
Transform(TInput, Int32) |
Applies the transformation to an input, producing an associated output.
(Inherited from MulticlassClassifierBaseTInput.) |
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.) | |
SetEqualsDecisionRule |
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.) |
Decision rule sets can be created from DecisionTrees using their ToRules method. An example is shown below.
// 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); // Create a teaching algorithm: var teacher = new C45Learning() { new DecisionVariable("sepal length", DecisionVariableKind.Continuous), new DecisionVariable("sepal width", DecisionVariableKind.Continuous), new DecisionVariable("petal length", DecisionVariableKind.Continuous), new DecisionVariable("petal width", DecisionVariableKind.Continuous), }; // Use the learning algorithm to induce a new tree: DecisionTree 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 =: (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) ";
// In this example, we will be using a modified version of the famous Play Tennis // example by Tom Mitchell (1998), where some values have been replaced by missing // values. We will use NaN double values to represent values missing from the data. // Note: this example uses DataTables to represent the input data, // but this is not required. The same could be performed using plain // double[][] matrices and vectors instead. DataTable data = new DataTable("Tennis Example with Missing Values"); data.Columns.Add("Day", typeof(string)); data.Columns.Add("Outlook", typeof(string)); data.Columns.Add("Temperature", typeof(string)); data.Columns.Add("Humidity", typeof(string)); data.Columns.Add("Wind", typeof(string)); data.Columns.Add("PlayTennis", typeof(string)); data.Rows.Add("D1", "Sunny", "Hot", "High", "Weak", "No"); data.Rows.Add("D2", null, "Hot", "High", "Strong", "No"); data.Rows.Add("D3", null, null, "High", null, "Yes"); data.Rows.Add("D4", "Rain", "Mild", "High", "Weak", "Yes"); data.Rows.Add("D5", "Rain", "Cool", null, "Weak", "Yes"); data.Rows.Add("D6", "Rain", "Cool", "Normal", "Strong", "No"); data.Rows.Add("D7", "Overcast", "Cool", "Normal", "Strong", "Yes"); data.Rows.Add("D8", null, "Mild", "High", null, "No"); data.Rows.Add("D9", null, "Cool", "Normal", "Weak", "Yes"); data.Rows.Add("D10", null, null, "Normal", null, "Yes"); data.Rows.Add("D11", null, "Mild", "Normal", null, "Yes"); data.Rows.Add("D12", "Overcast", "Mild", null, "Strong", "Yes"); data.Rows.Add("D13", "Overcast", "Hot", null, "Weak", "Yes"); data.Rows.Add("D14", "Rain", "Mild", "High", "Strong", "No"); // Create a new codification codebook to convert // the strings above into numeric, integer labels: var codebook = new Codification() { DefaultMissingValueReplacement = Double.NaN }; // Learn the codebook codebook.Learn(data); // Use the codebook to convert all the data DataTable symbols = codebook.Apply(data); // Grab the training input and output instances: string[] inputNames = new[] { "Outlook", "Temperature", "Humidity", "Wind" }; double[][] inputs = symbols.ToJagged(inputNames); int[] outputs = symbols.ToArray<int>("PlayTennis"); // Create a new learning algorithm var teacher = new C45Learning() { Attributes = DecisionVariable.FromCodebook(codebook, inputNames) }; // Use the learning algorithm to induce a new tree: DecisionTree tree = teacher.Learn(inputs, outputs); // To get the estimated class labels, we can use int[] predicted = tree.Decide(inputs); // The classification error (~0.214) 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, "PlayTennis", System.Globalization.CultureInfo.InvariantCulture); // The output should be: string expected = @"No =: (Outlook == Sunny) No =: (Outlook == Rain) && (Wind == Strong) Yes =: (Outlook == Overcast) Yes =: (Outlook == Rain) && (Wind == Weak) ";