Class MathExpressionClassifier

  • All Implemented Interfaces:
    Serializable, Cloneable, weka.classifiers.Classifier, weka.classifiers.UpdateableClassifier, weka.core.BatchPredictor, weka.core.CapabilitiesHandler, weka.core.CapabilitiesIgnorer, weka.core.CommandlineRunnable, weka.core.OptionHandler, weka.core.RevisionHandler

    public class MathExpressionClassifier
    extends weka.classifiers.AbstractClassifier
    implements weka.classifiers.UpdateableClassifier
    Simple classifier that uses a pre-defined formula that can make use of attribute values using their names.
    Grammar:
    expr_list ::= '=' expr_list expr_part | expr_part ;
    expr_part ::= expr ;

    expr ::= ( expr )

    # data types
    | number
    | string
    | boolean
    | date

    # constants
    | true
    | false
    | pi
    | e
    | now()
    | today()

    # negating numeric value
    | -expr

    # comparisons
    | expr < expr
    | expr <= expr
    | expr > expr
    | expr >= expr
    | expr = expr
    | expr != expr (or: expr <> expr)

    # boolean operations
    | ! expr (or: not expr)
    | expr & expr (or: expr and expr)
    | expr | expr (or: expr or expr)
    | if[else] ( expr , expr (if true) , expr (if false) )
    | ifmissing ( variable , expr (default value if variable is missing) )
    | isNaN ( expr )

    # arithmetics
    | expr + expr
    | expr - expr
    | expr * expr
    | expr / expr
    | expr ^ expr (power of)
    | expr % expr (modulo)
    ;

    # numeric functions
    | abs ( expr )
    | sqrt ( expr )
    | cbrt ( expr )
    | log ( expr )
    | log10 ( expr )
    | exp ( expr )
    | sin ( expr )
    | sinh ( expr )
    | cos ( expr )
    | cosh ( expr )
    | tan ( expr )
    | tanh ( expr )
    | atan ( expr )
    | atan2 ( exprY , exprX )
    | hypot ( exprX , exprY )
    | signum ( expr )
    | rint ( expr )
    | floor ( expr )
    | pow[er] ( expr , expr )
    | ceil ( expr )
    | min ( expr1 , expr2 )
    | max ( expr1 , expr2 )
    | year ( expr )
    | month ( expr )
    | day ( expr )
    | hour ( expr )
    | minute ( expr )
    | second ( expr )
    | weekday ( expr )
    | weeknum ( expr )

    # string functions
    | substr ( expr , start [, end] )
    | left ( expr , len )
    | mid ( expr , start , len )
    | right ( expr , len )
    | rept ( expr , count )
    | concatenate ( expr1 , expr2 [, expr3-5] )
    | lower[case] ( expr )
    | upper[case] ( expr )
    | trim ( expr )
    | matches ( expr , regexp )
    | trim ( expr )
    | len[gth] ( str )
    | find ( search , expr [, pos] )
    | replace ( str , pos , len , newstr )
    | substitute ( str , find , replace [, occurrences] )
    ;

    Notes:
    - Variables are either all upper case letters (e.g., "ABC") or any character apart from "]" enclosed by "[" and "]" (e.g., "[Hello World]").
    - 'start' and 'end' for function 'substr' are indices that start at 1.
    - Index 'end' for function 'substr' is excluded (like Java's 'String.substring(int,int)' method)
    - Line comments start with '#'.
    - Semi-colons (';') or commas (',') can be used as separator in the formulas,
    e.g., 'pow(2,2)' is equivalent to 'pow(2;2)'
    - dates have to be of format 'yyyy-MM-dd' or 'yyyy-MM-dd HH:mm:ss'
    - times have to be of format 'HH:mm:ss' or 'yyyy-MM-dd HH:mm:ss'
    - the characters in square brackets in function names are optional:
    e.g. 'len("abc")' is the same as 'length("abc")'

    A lot of the functions have been modeled after LibreOffice:
    https://help.libreoffice.org/Calc/Functions_by_Category

    Additional functions:
    - env(String): String
    First argument is the name of the environment variable to retrieve.
    The result is the value of the environment variable.

    Additional procedures:
    - println(...)
    One or more arguments are printed as comma-separated list to stdout.
    If no argument is provided, a simple line feed is output.


    Valid options are:

     -expression <string>
      The expression to use.
      (default: 1.0)
     -output-debug-info
      If set, classifier is run in debug mode and
      may output additional info to the console
     -do-not-check-capabilities
      If set, classifier capabilities are not checked before classifier is built
      (use with caution).
     -num-decimal-places
      The number of decimal places for the output of numbers in the model (default 2).
    Version:
    $Revision$
    Author:
    fracpete (fracpete at waikato dot ac dot nz)
    See Also:
    Serialized Form
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected adams.parser.MathematicalExpressionText m_Expression
      the expression.
      protected adams.parser.MathematicalExpression m_Parser
      the expression parser to use.
      • Fields inherited from class weka.classifiers.AbstractClassifier

        BATCH_SIZE_DEFAULT, m_BatchSize, m_Debug, m_DoNotCheckCapabilities, m_numDecimalPlaces, NUM_DECIMAL_PLACES_DEFAULT
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void buildClassifier​(weka.core.Instances data)
      Builds the classifier on the training data.
      double classifyInstance​(weka.core.Instance instance)
      Classifies the given test instance.
      String expressionTipText()
      Returns the tip text for this property.
      weka.core.Capabilities getCapabilities()
      Returns the Capabilities of this classifier.
      adams.parser.MathematicalExpressionText getExpression()
      Returns the mathematical expression to evaluate.
      String[] getOptions()
      returns the options of the current setup
      protected adams.parser.MathematicalExpression getParser()
      Returns the parser to use.
      String getRevision()
      Returns the revision string.
      String globalInfo()
      Returns a string describing classifier
      Enumeration listOptions()
      Gets an enumeration describing the available options.
      static void main​(String[] args)
      Main method for running this class.
      void setExpression​(adams.parser.MathematicalExpressionText value)
      Sets the mathematical expression to evaluate.
      void setOptions​(String[] options)
      Parses the options for this object.
      String toString()
      Returns a string representation of the built model.
      void updateClassifier​(weka.core.Instance instance)
      Does nothing.
      • Methods inherited from class weka.classifiers.AbstractClassifier

        batchSizeTipText, debugTipText, distributionForInstance, distributionsForInstances, doNotCheckCapabilitiesTipText, forName, getBatchSize, getDebug, getDoNotCheckCapabilities, getNumDecimalPlaces, implementsMoreEfficientBatchPrediction, makeCopies, makeCopy, numDecimalPlacesTipText, postExecution, preExecution, run, runClassifier, setBatchSize, setDebug, setDoNotCheckCapabilities, setNumDecimalPlaces
    • Field Detail

      • m_Expression

        protected adams.parser.MathematicalExpressionText m_Expression
        the expression.
      • m_Parser

        protected transient adams.parser.MathematicalExpression m_Parser
        the expression parser to use.
    • Constructor Detail

      • MathExpressionClassifier

        public MathExpressionClassifier()
        Constructor.
    • Method Detail

      • globalInfo

        public String globalInfo()
        Returns a string describing classifier
        Returns:
        a description suitable for displaying in the explorer/experimenter gui
      • listOptions

        public Enumeration listOptions()
        Gets an enumeration describing the available options.
        Specified by:
        listOptions in interface weka.core.OptionHandler
        Overrides:
        listOptions in class weka.classifiers.AbstractClassifier
        Returns:
        an enumeration of all the available options.
      • getOptions

        public String[] getOptions()
        returns the options of the current setup
        Specified by:
        getOptions in interface weka.core.OptionHandler
        Overrides:
        getOptions in class weka.classifiers.AbstractClassifier
        Returns:
        the current options
      • setOptions

        public void setOptions​(String[] options)
                        throws Exception
        Parses the options for this object.
        Specified by:
        setOptions in interface weka.core.OptionHandler
        Overrides:
        setOptions in class weka.classifiers.AbstractClassifier
        Parameters:
        options - the options to use
        Throws:
        Exception - if setting of options fails
      • setExpression

        public void setExpression​(adams.parser.MathematicalExpressionText value)
        Sets the mathematical expression to evaluate.
        Parameters:
        value - the expression
      • getExpression

        public adams.parser.MathematicalExpressionText getExpression()
        Returns the mathematical expression to evaluate.
        Returns:
        the expression
      • expressionTipText

        public String expressionTipText()
        Returns the tip text for this property.
        Returns:
        tip text for this property suitable for displaying in the GUI or for listing the options.
      • getCapabilities

        public weka.core.Capabilities getCapabilities()
        Returns the Capabilities of this classifier. Maximally permissive capabilities are allowed by default. Derived classifiers should override this method and first disable all capabilities and then enable just those capabilities that make sense for the scheme.
        Specified by:
        getCapabilities in interface weka.core.CapabilitiesHandler
        Specified by:
        getCapabilities in interface weka.classifiers.Classifier
        Overrides:
        getCapabilities in class weka.classifiers.AbstractClassifier
        Returns:
        the capabilities of this object
        See Also:
        Capabilities
      • getParser

        protected adams.parser.MathematicalExpression getParser()
        Returns the parser to use.
        Returns:
        the parser
      • buildClassifier

        public void buildClassifier​(weka.core.Instances data)
                             throws Exception
        Builds the classifier on the training data.
        Specified by:
        buildClassifier in interface weka.classifiers.Classifier
        Parameters:
        data - the data to use
        Throws:
        Exception - if training fails
      • updateClassifier

        public void updateClassifier​(weka.core.Instance instance)
                              throws Exception
        Does nothing.
        Specified by:
        updateClassifier in interface weka.classifiers.UpdateableClassifier
        Parameters:
        instance -
        Throws:
        Exception
      • classifyInstance

        public double classifyInstance​(weka.core.Instance instance)
                                throws Exception
        Classifies the given test instance. The instance has to belong to a dataset when it's being classified.
        Specified by:
        classifyInstance in interface weka.classifiers.Classifier
        Overrides:
        classifyInstance in class weka.classifiers.AbstractClassifier
        Parameters:
        instance - the instance to be classified
        Returns:
        the predicted most likely class for the instance or Instance.missingValue() if no prediction is made
        Throws:
        Exception - if an error occurred during the prediction
      • getRevision

        public String getRevision()
        Returns the revision string.
        Specified by:
        getRevision in interface weka.core.RevisionHandler
        Overrides:
        getRevision in class weka.classifiers.AbstractClassifier
        Returns:
        the revision
      • toString

        public String toString()
        Returns a string representation of the built model.
        Overrides:
        toString in class Object
        Returns:
        the model string
      • main

        public static void main​(String[] args)
        Main method for running this class.
        Parameters:
        args - the commandline parameters