CategoricalEntailmentEnsembleClassifier Class |
Namespace: Novacta.Analytics
The CategoricalEntailmentEnsembleClassifier type exposes the following members.
Name | Description | |
---|---|---|
CategoricalEntailmentEnsembleClassifier |
Initializes a new instance of
the CategoricalEntailmentEnsembleClassifier class
whose categorical entailments
will classify items from the space defined by the specified
feature variables by assigning categories of
the given response variable.
|
Name | Description | |
---|---|---|
Entailments |
Gets the list of categorical entailments contributing
to classification.
| |
FeatureVariables |
Gets the categorical variables defining the
feature space whose items can be classified by
this instance.
| |
ResponseVariable |
Gets the response variable whose categories
this instance exploits for classification.
|
Name | Description | |
---|---|---|
Add |
Adds to this instance a
CategoricalEntailment object having
the specified premises on feature variables,
derived response category, and truth value.
| |
AddTrained |
Adds a number of new categorical entailments
by training them together with
the entailments currently included in this instance.
Training happens on the
specified features and response categorical
variables in a given data set.
| |
Classify |
Classifies the categorical items in the specified data set.
| |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
EvaluateAccuracy |
Returns the accuracy of a predicted classification with respect
to an actual one.
| |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
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.) | |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
Train |
Initializes a new instance of the
CategoricalEntailmentEnsembleClassifier class
by training an ensemble of categorical entailments on the
specified features and response categorical
variables in a given data set.
|
Categorical entailments for maximal accuracy
Let us assume that items from a given categorical space must be classified into a set of labels. If input attributes are taken into account, and if attribute has nonempty finite domain , then can be represented as the Cartesian product .
A CategoricalEntailment instance, say , is a triple , where is a subset of , , and is a truth value. Given an instance , the entailment will classify as if and only if .
Instantiation and training
Method Train(CategoricalDataSet, IndexCollection, Int32, Int32, Boolean, Boolean) returns a CategoricalEntailmentEnsembleClassifier instance whose ensemble of categorical entailments has been trained by maximizing the accuracy of label assignments to items in a specified data set.
Classifiers can also be instantiated by calling the constructor CategoricalEntailmentEnsembleClassifier(IListCategoricalVariable, CategoricalVariable), and then adding categorical entailments by calling method Add(IListSortedSetDouble, Double, Double), or AddTrained(CategoricalDataSet, IndexCollection, Int32, Int32, Boolean, Boolean).
Empty feature or response domains are not allowed.
A premise can be empty, but in such case it is represented as matching the corresponding feature domain. Equivalently, a premise which is not a nonempty proper subset of the domain is always valid for every item in the feature space.
Classification
Property Entailments gets the list of the categorical entailments in the ensemble of a given classifier.
Given a classifier based on, say, categorical
entailments,
entailment ,
with ,
is said to vote to classify as
whenever .
Unlabeled items can thus be
classified via the collection of
Entailments by
performing majority voting procedures, where
the classification of a
given item , is defined as
satisfying
and ties are eventually resolved by selecting one of the
maximizing arguments at random.
Method Classify(CategoricalDataSet, IndexCollection) returns such classification for a given collection of items in the feature space.
Advanced scenarios
Internally, the problem of training an ensemble of categorical entailments is solved via a default Cross-Entropy context of type CategoricalEntailmentEnsembleOptimizationContext. For advanced scenarios, in which additional control on the parameters of the underlying algorithm is needed, or a different performance function should be optimized, a specialized context can be instantiated and hence exploited executing method Optimize on a SystemPerformanceOptimizer object.
In the following example, a classifier is trained on a categorical data set. Items are thus classified and the corresponding accuracy computed.
using System; using System.Collections.Generic; namespace Novacta.Analytics.CodeExamples { public class CategoricalEntailmentEnsembleClassifierExample0 { public void Main() { // Create the feature variables. CategoricalVariable f0 = new("F-0") { { 0, "A" }, { 1, "B" }, { 2, "C" }, { 3, "D" }, { 4, "E" } }; f0.SetAsReadOnly(); CategoricalVariable f1 = new("F-1") { { 0, "I" }, { 1, "II" }, { 2, "III" }, { 3, "IV" } }; f1.SetAsReadOnly(); // Create the response variable. CategoricalVariable r = new("R") { 0, 1, 2 }; r.SetAsReadOnly(); // Create a categorical data set containing // observations about such variables. List<CategoricalVariable> variables = new() { f0, f1, r }; DoubleMatrix data = DoubleMatrix.Dense( new double[20, 3] { { 0, 0, 0 }, { 0, 1, 0 }, { 0, 2, 2 }, { 0, 3, 2 }, { 1, 0, 0 }, { 1, 1, 0 }, { 1, 2, 2 }, { 1, 3, 2 }, { 2, 0, 0 }, { 2, 1, 0 }, { 2, 2, 2 }, { 2, 3, 2 }, { 3, 0, 1 }, { 3, 1, 1 }, { 3, 2, 1 }, { 3, 3, 1 }, { 4, 0, 1 }, { 4, 1, 1 }, { 4, 2, 1 }, { 4, 3, 1 } }); CategoricalDataSet dataSet = CategoricalDataSet.FromEncodedData( variables, data); // Train a classifier on the specified data set. var classifier = CategoricalEntailmentEnsembleClassifier.Train( dataSet: dataSet, featureVariableIndexes: IndexCollection.Range(0, 1), responseVariableIndex: 2, numberOfTrainedCategoricalEntailments: 3, allowEntailmentPartialTruthValues: false, trainSequentially: true); // Show the ensemble of categorical entailments // in the trained classifier. foreach (var entailment in classifier.Entailments) { Console.WriteLine(entailment); } // Classify the items in the data set. var predicted = classifier.Classify(dataSet, IndexCollection.Range(0, 1)); // Evaluate and show the accuracy of the predicted classification // with respect to the actual one. var actual = dataSet[":", 2]; var accuracy = CategoricalEntailmentEnsembleClassifier.EvaluateAccuracy( predictedDataSet: predicted, predictedResponseVariableIndex: 0, actualDataSet: actual, actualResponseVariableIndex: 0); Console.WriteLine("Accuracy: {0}", accuracy); } } } // Executing method Main() produces the following output: // // IF // F-0 IN { 3 4 } // AND // F-1 IN { 0 1 2 3 } // THEN // R IS 1 // WITH TRUTH VALUE 1 // // IF // F-0 IN { 0 1 2 } // AND // F-1 IN { 0 1 } // THEN // R IS 0 // WITH TRUTH VALUE 1 // // IF // F-0 IN { 0 1 2 } // AND // F-1 IN { 2 3 } // THEN // R IS 2 // WITH TRUTH VALUE 1 // // Accuracy: 1