weka.classifiers.meta
Class HND

java.lang.Object
  extended byweka.classifiers.Classifier
      extended byweka.classifiers.meta.HND
All Implemented Interfaces:
java.lang.Cloneable, NestedDichotomy, OptionHandler, Randomizable, java.io.Serializable

public class HND
extends Classifier
implements NestedDichotomy, OptionHandler

Class to create levelwise NDs with respect to a given hierarchy of classes. Valid options are:

-P classname
Specify the full class name of a ClassHierarchyParser (default: weka.core.converters.ClassTreeArffFileParser).

-H classHierarchy
A String specifying the class-hierarchy in a valid manner with respect to the specified ClassHierarchyParser (required).

-L property-file
A String specifying the filename providing levelwise options for the respective ND and for selection of attributes (optional).
A key for ND options is supposed to be of form "leveln" for level n. Levelwise properties will overwrite the general options for ND or select attributes for the respective level only.
A key for levelwise selection of attributes is supposed to be of form "removen" for level n. The property for this key is supposed to be a string providing the options for the Filter Remove.

Options after "--" are passed to the inherent NDs.

Author:
Arthur Zimek
See Also:
Serialized Form

Field Summary
private  java.util.Map childrenHNDs
          The HNDs covering the instances at deeper levels, if there are such.
private  ClassHierarchyParser classHierarchyParser
          The default parser is a ClassTreeArffFileParser, suitable for a hierarchy encoded within a single String.
private  ClassHierarchy hierarchy
          The hierarchy of classes for the current and deeper levels.
private  int LEVEL
          The level of this HND in the hierarchy (root is 0).
static java.lang.String LEVEL_PROPERTY
          Key-prefix for the property level.
private  ND nd
          The ND to classify the instances at the currentlevel
private  java.lang.String[] ndOptions
          The options to be set for each ND.
private  java.util.Properties properties
          The properties for levelwise settings of the respective ND.
private  java.lang.String propertyFilename
          The filename of the property-file as set via setOptions.
static java.lang.String REMOVE_FILTER_PROPERTY
          Key-prefix for the property filter, for levelwise selection of attributes.
static java.util.regex.Pattern WHITESPACE
          The pattern for separation of entries in the option-line of a property-file.
 
Fields inherited from class weka.classifiers.Classifier
m_Debug
 
Constructor Summary
  HND()
          Default constructor creates a HND without hierarchy for level 0.
private HND(ClassHierarchy hierarchy, java.util.Properties properties, int level)
          The constructor to be used by the buildLevelwiseClassifier(Instances) method provides a hierarchy.
 
Method Summary
private  void appendDescription(java.lang.StringBuffer description)
          Appends a description of all children HNDs of this HND to the given description.
 void buildClassifier(Instances data)
          Builds the classifier for the given training data.
private  void buildLevelwiseClassifier(Instances data)
          Build levelwise NDs with respect to the specified hierarchy of classes.
 double[] distributionForInstance(Instance instance)
          Returns a class probability estimation for the given instance.
private  void distributionForInstance(Instance instance, java.util.Map classDistribution, double factor)
          Computes levelwise class probability estimation for the given instance and fills in the respective values to the classDistribution, using the factor (accumulated class probability for the respective superclass).
 java.lang.String[] getOptions()
          Returns the current settings of the Classifier.
 int getSeed()
          Returns the current seed of the ND.
 void initND(java.lang.String[] ndOptions)
          Sets the given options to the ND.
 java.util.Enumeration listOptions()
          Returns a list of the available options.
static void main(java.lang.String[] args)
          Main method for testing this class.
 void setOptions(java.lang.String[] options)
          Parses a given list of options.
 void setSeed(int seed)
          Sets the given seed to the ND and recursively to NDs of children HNDs.
 java.lang.String toString()
          Returns a description of this classifier.
 
Methods inherited from class weka.classifiers.Classifier
classifyInstance, debugTipText, forName, getDebug, makeCopies, setDebug
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

LEVEL_PROPERTY

public static final java.lang.String LEVEL_PROPERTY
Key-prefix for the property level.

See Also:
Constant Field Values

REMOVE_FILTER_PROPERTY

public static final java.lang.String REMOVE_FILTER_PROPERTY
Key-prefix for the property filter, for levelwise selection of attributes.

See Also:
Constant Field Values

WHITESPACE

public static final java.util.regex.Pattern WHITESPACE
The pattern for separation of entries in the option-line of a property-file. All single whitespace characters are valid separators.


nd

private ND nd
The ND to classify the instances at the currentlevel


properties

private java.util.Properties properties
The properties for levelwise settings of the respective ND.


propertyFilename

private java.lang.String propertyFilename
The filename of the property-file as set via setOptions.


LEVEL

private final int LEVEL
The level of this HND in the hierarchy (root is 0).


ndOptions

private java.lang.String[] ndOptions
The options to be set for each ND.


childrenHNDs

private java.util.Map childrenHNDs
The HNDs covering the instances at deeper levels, if there are such. Remains null for a HND covering the base-classes (leaves of the hierarchy). Is set during the method buildLevelwiseCLassifier. A superclasses-String is key for the according HND.


hierarchy

private ClassHierarchy hierarchy
The hierarchy of classes for the current and deeper levels.


classHierarchyParser

private ClassHierarchyParser classHierarchyParser
The default parser is a ClassTreeArffFileParser, suitable for a hierarchy encoded within a single String.

Constructor Detail

HND

public HND()
Default constructor creates a HND without hierarchy for level 0. The hierarchy is either to be set via setOptions(String[] or remains null, in which case any data will be rejected as invalid.


HND

private HND(ClassHierarchy hierarchy,
            java.util.Properties properties,
            int level)
The constructor to be used by the buildLevelwiseClassifier(Instances) method provides a hierarchy.

Parameters:
hierarchy - the specified hierarchy for the respective level
properties - the levelwise properties to be set
level - the current level of the hierarchy
Method Detail

initND

public void initND(java.lang.String[] ndOptions)
            throws java.lang.Exception
Sets the given options to the ND.

Parameters:
ndOptions - the options to be set to the ND. The parameter ndOptions is copied, not altered, since it is to be passed through all levels.
Throws:
java.lang.Exception - in case of invalid options for ND

buildLevelwiseClassifier

private void buildLevelwiseClassifier(Instances data)
                               throws java.lang.Exception
Build levelwise NDs with respect to the specified hierarchy of classes.

Parameters:
data - training instances
Throws:
java.lang.Exception

buildClassifier

public void buildClassifier(Instances data)
                     throws java.lang.Exception
Builds the classifier for the given training data.

Specified by:
buildClassifier in class Classifier
Parameters:
data - set of instances serving as training data
Throws:
java.lang.Exception - if the classifier has not been generated successfully
See Also:
Classifier.buildClassifier(Instances)

distributionForInstance

public double[] distributionForInstance(Instance instance)
                                 throws java.lang.Exception
Returns a class probability estimation for the given instance.

Overrides:
distributionForInstance in class Classifier
Parameters:
instance - the instance to be classified
Returns:
an array containing the estimated membership probabilities of the test instance in each class or the numeric prediction
Throws:
java.lang.Exception - if distribution could not be computed successfully
See Also:
Classifier.distributionForInstance(weka.core.Instance)

distributionForInstance

private void distributionForInstance(Instance instance,
                                     java.util.Map classDistribution,
                                     double factor)
                              throws java.lang.Exception
Computes levelwise class probability estimation for the given instance and fills in the respective values to the classDistribution, using the factor (accumulated class probability for the respective superclass).

Parameters:
instance - the instance to be classified
classDistribution - the class distribution covering all classes
factor - the class probability for the respective superclass
Throws:
java.lang.Exception

getOptions

public java.lang.String[] getOptions()
Returns the current settings of the Classifier.

Specified by:
getOptions in interface OptionHandler
Overrides:
getOptions in class Classifier
Returns:
an array of strings suitable for passing to setOptions
See Also:
OptionHandler.getOptions()

listOptions

public java.util.Enumeration listOptions()
Returns a list of the available options.

Specified by:
listOptions in interface OptionHandler
Overrides:
listOptions in class Classifier
Returns:
an enumeration of all the available options.
See Also:
OptionHandler.listOptions()

setOptions

public void setOptions(java.lang.String[] options)
                throws java.lang.Exception
Parses a given list of options. Valid options are:

-P classname
Specify the full class name of a ClassHierarchyParser (default: weka.core.converters.ClassTreeArffFileParser).

-H classHierarchy
A String specifying the class-hierarchy in a valid manner with respect to the specified ClassHierarchyParser (required).

-L property-file
A String specifying the filename providing levelwise options for the respective ND and for selection of attributes (optional).
A key for ND options is supposed to be of form "leveln" for level n. Levelwise properties will overwrite the general options for ND or select attributes for the respective level only.
A key for levelwise selection of attributes is supposed to be of form "removen" for level n. The property for this key is supposed to be a string providing the options for the Filter Remove.

Options after -- are passed to the inherent NDs.

Specified by:
setOptions in interface OptionHandler
Overrides:
setOptions in class Classifier
Parameters:
options - the list of options as an array of strings
Throws:
java.lang.Exception - if a required option is not supported
See Also:
OptionHandler.setOptions(java.lang.String[])

setSeed

public void setSeed(int seed)
Sets the given seed to the ND and recursively to NDs of children HNDs.

Specified by:
setSeed in interface Randomizable
Parameters:
seed - the seed
See Also:
Randomizable.setSeed(int)

getSeed

public int getSeed()
Returns the current seed of the ND.

Specified by:
getSeed in interface Randomizable
Returns:
the seed for the random number generation
See Also:
Randomizable.getSeed()

toString

public java.lang.String toString()
Returns a description of this classifier.

Returns:
String a description of this classifier
See Also:
Object.toString()

appendDescription

private void appendDescription(java.lang.StringBuffer description)
Appends a description of all children HNDs of this HND to the given description.

Parameters:
description - the description to which further descriptions are to be appended

main

public static void main(java.lang.String[] args)
Main method for testing this class.

Parameters:
args - the options