Click or drag to resize

CategoricalEntailmentEnsembleClassifier Class

Represents a classifier that exploits an ensemble of categorical entailments to assign labels to items from a given feature space.
Inheritance Hierarchy
SystemObject
  Novacta.AnalyticsCategoricalEntailmentEnsembleClassifier

Namespace:  Novacta.Analytics
Assembly:  Novacta.Analytics (in Novacta.Analytics.dll) Version: 2.0.0
Syntax
public class CategoricalEntailmentEnsembleClassifier

The CategoricalEntailmentEnsembleClassifier type exposes the following members.

Constructors
  NameDescription
Public methodCategoricalEntailmentEnsembleClassifier
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.
Top
Properties
  NameDescription
Public propertyEntailments
Gets the list of categorical entailments contributing to classification.
Public propertyFeatureVariables
Gets the categorical variables defining the feature space whose items can be classified by this instance.
Public propertyResponseVariable
Gets the response variable whose categories this instance exploits for classification.
Top
Methods
  NameDescription
Public methodAdd
Adds to this instance a CategoricalEntailment object having the specified premises on feature variables, derived response category, and truth value.
Public methodAddTrained
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.
Public methodClassify
Classifies the categorical items in the specified data set.
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Public methodStatic memberEvaluateAccuracy
Returns the accuracy of a predicted classification with respect to an actual one.
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 methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodStatic memberTrain
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.
Top
Remarks

Categorical entailments for maximal accuracy

Let us assume that items from a given categorical space LaTeX equation must be classified into a set LaTeX equation of labels. If LaTeX equation input attributes are taken into account, and if attribute LaTeX equation has nonempty finite domain LaTeX equation, then LaTeX equation can be represented as the Cartesian product LaTeX equation.

A CategoricalEntailment instance, say LaTeX equation, is a triple LaTeX equation, where LaTeX equation is a subset of LaTeX equation, LaTeX equation, and LaTeX equation is a truth value. Given an instance LaTeX equation, the entailment will classify LaTeX equation as LaTeX equation if and only if LaTeX equation.

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, LaTeX equation categorical entailments, entailment LaTeX equation, with LaTeX equation, is said to vote to classify LaTeX equation as LaTeX equation whenever LaTeX equation. Unlabeled items can thus be classified via the collection of Entailments by performing majority voting procedures, where the classification LaTeX equation of a given item LaTeX equation, is defined as satisfying

LaTeX equation

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.

Examples

In the following example, a classifier is trained on a categorical data set. Items are thus classified and the corresponding accuracy computed.

Exploiting a classifier based on an ensemble of categorical entailments
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
See Also