Class AbstractClassifier

    • Field Detail

      • modelContext

        protected InstancesHeader modelContext
        Header of the instances of the data stream
      • trainingWeightSeenByModel

        protected double trainingWeightSeenByModel
        Sum of the weights of the instances trained by this model
      • randomSeed

        protected int randomSeed
        Random seed used in randomizable learners
      • randomSeedOption

        protected IntOption randomSeedOption
        Option for randomizable learners to change the random seed
      • classifierRandom

        public Random classifierRandom
        Random Generator used in randomizable learners
    • Constructor Detail

      • AbstractClassifier

        public AbstractClassifier()
        Creates an classifier and setups the random seed option if the classifier is randomizable.
    • Method Detail

      • prepareForUseImpl

        public void prepareForUseImpl​(TaskMonitor monitor,
                                      ObjectRepository repository)
        Description copied from class: AbstractOptionHandler
        This method describes the implementation of how to prepare this object for use. All classes that extends this class have to implement prepareForUseImpl and not prepareForUse since prepareForUse calls prepareForUseImpl.
        Specified by:
        prepareForUseImpl in class AbstractOptionHandler
        Parameters:
        monitor - the TaskMonitor to use
        repository - the ObjectRepository to use
      • getVotesForInstance

        public double[] getVotesForInstance​(Example<Instance> example)
        Description copied from interface: Learner
        Predicts the class memberships for a given instance. If an instance is unclassified, the returned array elements must be all zero.
        Specified by:
        getVotesForInstance in interface Learner<Example<Instance>>
        Parameters:
        example - the instance to be classified
        Returns:
        an array containing the estimated membership probabilities of the test instance in each class
      • getVotesForInstance

        public abstract double[] getVotesForInstance​(Instance inst)
        Description copied from interface: Classifier
        Predicts the class memberships for a given instance. If an instance is unclassified, the returned array elements must be all zero.
        Specified by:
        getVotesForInstance in interface Classifier
        Parameters:
        inst - the instance to be classified
        Returns:
        an array containing the estimated membership probabilities of the test instance in each class
      • getPredictionForInstance

        public Prediction getPredictionForInstance​(Instance inst)
        Description copied from interface: Classifier
        Gets the reference to the header of the data stream. The header of the data stream is extended from WEKA Instances. This header is needed to know the number of classes and attributes
        Specified by:
        getPredictionForInstance in interface Classifier
        Returns:
        the reference to the data stream header
      • setModelContext

        public void setModelContext​(InstancesHeader ih)
        Description copied from interface: Learner
        Sets the reference to the header of the data stream. The header of the data stream is extended from WEKA Instances. This header is needed to know the number of classes and attributes
        Specified by:
        setModelContext in interface Learner<Example<Instance>>
        Parameters:
        ih - the reference to the data stream header
      • getModelContext

        public InstancesHeader getModelContext()
        Description copied from interface: Learner
        Gets the reference to the header of the data stream. The header of the data stream is extended from WEKA Instances. This header is needed to know the number of classes and attributes
        Specified by:
        getModelContext in interface Learner<Example<Instance>>
        Returns:
        the reference to the data stream header
      • setRandomSeed

        public void setRandomSeed​(int s)
        Description copied from interface: Learner
        Sets the seed for random number generation.
        Specified by:
        setRandomSeed in interface Learner<Example<Instance>>
        Parameters:
        s - the seed
      • trainingHasStarted

        public boolean trainingHasStarted()
        Description copied from interface: Learner
        Gets whether training has started.
        Specified by:
        trainingHasStarted in interface Learner<Example<Instance>>
        Returns:
        true if training has started
      • trainingWeightSeenByModel

        public double trainingWeightSeenByModel()
        Description copied from interface: Learner
        Gets the sum of the weights of the instances that have been used by this learner during the training in trainOnInstance
        Specified by:
        trainingWeightSeenByModel in interface Learner<Example<Instance>>
        Returns:
        the weight of the instances that have been used training
      • resetLearning

        public void resetLearning()
        Description copied from interface: Learner
        Resets this learner. It must be similar to starting a new learner from scratch.
        Specified by:
        resetLearning in interface Learner<Example<Instance>>
      • trainOnInstance

        public void trainOnInstance​(Instance inst)
        Description copied from interface: Classifier
        Trains this learner incrementally using the given example.
        Specified by:
        trainOnInstance in interface Classifier
        Parameters:
        inst - the instance to be used for training
      • getModelMeasurements

        public Measurement[] getModelMeasurements()
        Description copied from interface: Learner
        Gets the current measurements of this learner.
        Specified by:
        getModelMeasurements in interface Learner<Example<Instance>>
        Returns:
        an array of measurements to be used in evaluation tasks
      • getDescription

        public void getDescription​(StringBuilder out,
                                   int indent)
        Description copied from interface: MOAObject
        Returns a string representation of this object. Used in AbstractMOAObject.toString to give a string representation of the object.
        Specified by:
        getDescription in interface MOAObject
        Parameters:
        out - the stringbuilder to add the description
        indent - the number of characters to indent
      • getSublearners

        public Learner[] getSublearners()
        Description copied from interface: Learner
        Gets the learners of this ensemble. Returns null if this learner is a single learner.
        Specified by:
        getSublearners in interface Learner<Example<Instance>>
        Returns:
        an array of the learners of the ensemble
      • getSubClassifiers

        public Classifier[] getSubClassifiers()
        Description copied from interface: Classifier
        Gets the classifiers of this ensemble. Returns null if this learner is a single learner.
        Specified by:
        getSubClassifiers in interface Classifier
        Returns:
        an array of the learners of the ensemble
      • trainOnInstance

        public void trainOnInstance​(Example<Instance> example)
        Description copied from interface: Learner
        Trains this learner incrementally using the given example.
        Specified by:
        trainOnInstance in interface Learner<Example<Instance>>
        Parameters:
        example - the instance to be used for training
      • correctlyClassifies

        public boolean correctlyClassifies​(Instance inst)
        Description copied from interface: Classifier
        Gets whether this classifier correctly classifies an instance. Uses getVotesForInstance to obtain the prediction and the instance to obtain its true class.
        Specified by:
        correctlyClassifies in interface Classifier
        Parameters:
        inst - the instance to be classified
        Returns:
        true if the instance is correctly classified
      • getClassNameString

        public String getClassNameString()
        Gets the name of the attribute of the class from the header.
        Returns:
        the string with name of the attribute of the class
      • getClassLabelString

        public String getClassLabelString​(int classLabelIndex)
        Gets the name of a label of the class from the header.
        Parameters:
        classLabelIndex - the label index
        Returns:
        the name of the label of the class
      • getAttributeNameString

        public String getAttributeNameString​(int attIndex)
        Gets the name of an attribute from the header.
        Parameters:
        attIndex - the attribute index
        Returns:
        the name of the attribute
      • getNominalValueString

        public String getNominalValueString​(int attIndex,
                                            int valIndex)
        Gets the name of a value of an attribute from the header.
        Parameters:
        attIndex - the attribute index
        valIndex - the value of the attribute
        Returns:
        the name of the value of the attribute
      • contextIsCompatible

        public static boolean contextIsCompatible​(InstancesHeader originalContext,
                                                  InstancesHeader newContext)
        Returns if two contexts or headers of instances are compatible.

        Two contexts are compatible if they follow the following rules:
        Rule 1: num classes can increase but never decrease
        Rule 2: num attributes can increase but never decrease
        Rule 3: num nominal attribute values can increase but never decrease
        Rule 4: attribute types must stay in the same order (although class can move; is always skipped over)

        Attribute names are free to change, but should always still represent the original attributes.
        Parameters:
        originalContext - the first context to compare
        newContext - the second context to compare
        Returns:
        true if the two contexts are compatible.
      • resetLearningImpl

        public abstract void resetLearningImpl()
        Resets this classifier. It must be similar to starting a new classifier from scratch.

        The reason for ...Impl methods: ease programmer burden by not requiring them to remember calls to super in overridden methods. Note that this will produce compiler errors if not overridden.
      • trainOnInstanceImpl

        public abstract void trainOnInstanceImpl​(Instance inst)
        Trains this classifier incrementally using the given instance.

        The reason for ...Impl methods: ease programmer burden by not requiring them to remember calls to super in overridden methods. Note that this will produce compiler errors if not overridden.
        Parameters:
        inst - the instance to be used for training
      • getModelMeasurementsImpl

        protected abstract Measurement[] getModelMeasurementsImpl()
        Gets the current measurements of this classifier.

        The reason for ...Impl methods: ease programmer burden by not requiring them to remember calls to super in overridden methods. Note that this will produce compiler errors if not overridden.
        Returns:
        an array of measurements to be used in evaluation tasks
      • getModelDescription

        public abstract void getModelDescription​(StringBuilder out,
                                                 int indent)
        Returns a string representation of the model.
        Parameters:
        out - the stringbuilder to add the description
        indent - the number of characters to indent
      • modelAttIndexToInstanceAttIndex

        protected static int modelAttIndexToInstanceAttIndex​(int index,
                                                             Instance inst)
        Gets the index of the attribute in the instance, given the index of the attribute in the learner.
        Parameters:
        index - the index of the attribute in the learner
        inst - the instance
        Returns:
        the index in the instance
      • modelAttIndexToInstanceAttIndex

        protected static int modelAttIndexToInstanceAttIndex​(int index,
                                                             Instances insts)
        Gets the index of the attribute in a set of instances, given the index of the attribute in the learner.
        Parameters:
        index - the index of the attribute in the learner
        insts - the instances
        Returns:
        the index of the attribute in the instances