de.jstacs.classifier.modelBased
Class ModelBasedClassifier

java.lang.Object
  extended by de.jstacs.classifier.AbstractClassifier
      extended by de.jstacs.classifier.AbstractScoreBasedClassifier
          extended by de.jstacs.classifier.modelBased.ModelBasedClassifier
All Implemented Interfaces:
Storable, Cloneable
Direct Known Subclasses:
SharedStructureClassifier

public class ModelBasedClassifier
extends AbstractScoreBasedClassifier

This class is the main class for all model based classifiers. The score for this class is the logarithm of the joint probability p(x,c|\lambda).

Author:
Jens Keilwagen
See Also:
Model

Nested Class Summary
 
Nested classes/interfaces inherited from class de.jstacs.classifier.AbstractScoreBasedClassifier
AbstractScoreBasedClassifier.DoubleTableResult
 
Field Summary
protected  Model[] models
          The internal Models.
 
Constructor Summary
protected ModelBasedClassifier(boolean cloneModels, Model... models)
          This constructor creates a new instance with the given Models and clones these if necessary.
  ModelBasedClassifier(Model... models)
          The default constructor that creates a new instance with the given Models.
  ModelBasedClassifier(StringBuffer xml)
          The standard constructor for the interface Storable.
 
Method Summary
 byte[] classify(Sample s)
          This method classifies all sequences of a sample and returns an array of indices of the classes to which the respective sequences are assigned with for each index i in the array 0 < i < getNumberOfClasses().
 ModelBasedClassifier clone()
           
protected  void extractFurtherClassifierInfosFromXML(StringBuffer xml)
          Extracts further information of a classifier from an XML representation.
 ResultSet getCharacteristics()
          Returns some information characterizing or describing the current instance of the classifier.
 CategoricalResult[] getClassifierAnnotation()
          Returns an array of Results of dimension AbstractClassifier.getNumberOfClasses() that contains information about the classifier and for each class.
protected  StringBuffer getFurtherClassifierInfos()
          This method returns further information of a classifier as a StringBuffer.
 String getInstanceName()
          Returns a short description of the classifier.
 Model getModel(int classIndex)
          Returns a clone of the Model for a specified class.
 NumericalResultSet getNumericalCharacteristics()
          Returns the subset of numerical values that are also returned by AbstractClassifier.getCharacteristics().
static int getPossibleLength(Model... models)
          This method returns the possible length of a classifier that would use the given Models.
protected  double getScore(Sequence seq, int i, boolean check)
          This method returns the score for a given Sequence and a given class.
 double[] getScores(Sample s)
          This method returns the scores of the classifier for any Sequence in the Sample.
protected  String getXMLTag()
          Returns the String that is used as tag for the XML representation of the classifier.
 boolean isTrained()
          This method gives information about the state of the classifier.
 boolean setNewAlphabetContainerInstance(AlphabetContainer abc)
          This method tries to set a new instance of an AlphabetContainer for the current classifier.
 void train(Sample[] s, double[][] weights)
          This method trains a classifier over an array of weighted Sample s.
 
Methods inherited from class de.jstacs.classifier.AbstractScoreBasedClassifier
check, check, classify, classify, createDefaultClassWeights, getClassWeight, getClassWeights, getNumberOfClasses, getPValue, getPValue, getResults, getScore, setClassWeights, setClassWeights, setThresholdClassWeights, test
 
Methods inherited from class de.jstacs.classifier.AbstractClassifier
evaluate, evaluateAll, getAlphabetContainer, getClassificationRate, getLength, getMeasuresForEvaluate, getMeasuresForEvaluateAll, toXML, train
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

models

protected Model[] models
The internal Models. Model 0 handles class 0; Model 1 handles class 1 ... etc.

Constructor Detail

ModelBasedClassifier

protected ModelBasedClassifier(boolean cloneModels,
                               Model... models)
                        throws IllegalArgumentException,
                               CloneNotSupportedException,
                               ClassDimensionException
This constructor creates a new instance with the given Models and clones these if necessary.

Parameters:
cloneModels - a switch to decide whether to clone the Model or not
models - the Models
Throws:
IllegalArgumentException - if the Models do not describe a common domain of sequences
CloneNotSupportedException - if at least one Model could not be cloned
ClassDimensionException - if the number of classes is below 2
See Also:
AbstractScoreBasedClassifier.AbstractScoreBasedClassifier(AlphabetContainer, int, int, double)

ModelBasedClassifier

public ModelBasedClassifier(Model... models)
                     throws IllegalArgumentException,
                            CloneNotSupportedException,
                            ClassDimensionException
The default constructor that creates a new instance with the given Models.

Parameters:
models - the Models
Throws:
IllegalArgumentException - if the Models do not describe a common domain of sequences
CloneNotSupportedException - if at least one Model could not be cloned
ClassDimensionException - if the number of classes is below 2
See Also:
ModelBasedClassifier(boolean, Model...)

ModelBasedClassifier

public ModelBasedClassifier(StringBuffer xml)
                     throws NonParsableException
The standard constructor for the interface Storable. Constructs a ModelBasedClassifier out of its XML representation.

Parameters:
xml - the XML representation as StringBuffer
Throws:
NonParsableException - if the ModelBasedClassifier could not be reconstructed out of the XML representation (the StringBuffer could not be parsed)
See Also:
AbstractScoreBasedClassifier.AbstractScoreBasedClassifier(StringBuffer), Storable
Method Detail

getPossibleLength

public static int getPossibleLength(Model... models)
                             throws IllegalArgumentException
This method returns the possible length of a classifier that would use the given Models.

Parameters:
models - the Models that will be tested
Returns:
the length of a classifier that would use the given models
Throws:
IllegalArgumentException - if no classifier could be created since the Models have incompatible lengths

clone

public ModelBasedClassifier clone()
                           throws CloneNotSupportedException
Overrides:
clone in class AbstractScoreBasedClassifier
Throws:
CloneNotSupportedException

getCharacteristics

public ResultSet getCharacteristics()
                             throws Exception
Description copied from class: AbstractClassifier
Returns some information characterizing or describing the current instance of the classifier. This could be for instance the number of edges for a Bayesian network or an image showing some representation of the model of a class. The set of characteristics should always include the XML representation of the classifier. The corresponding result type is StorableResult.

Overrides:
getCharacteristics in class AbstractClassifier
Returns:
the characteristics of the current instance of the classifier
Throws:
Exception - if some of the characteristics could not be defined
See Also:
StorableResult, AbstractClassifier.getNumericalCharacteristics(), ResultSet.ResultSet(de.jstacs.results.Result[][])

getInstanceName

public String getInstanceName()
Description copied from class: AbstractClassifier
Returns a short description of the classifier.

Specified by:
getInstanceName in class AbstractClassifier
Returns:
a short description of the classifier

getModel

public Model getModel(int classIndex)
               throws CloneNotSupportedException
Returns a clone of the Model for a specified class.

Parameters:
classIndex - the index of the specified class
Returns:
a clone of the Model of the specified class
Throws:
CloneNotSupportedException - if the Model could not be cloned
See Also:
Model.clone()

getNumericalCharacteristics

public NumericalResultSet getNumericalCharacteristics()
                                               throws Exception
Description copied from class: AbstractClassifier
Returns the subset of numerical values that are also returned by AbstractClassifier.getCharacteristics().

Specified by:
getNumericalCharacteristics in class AbstractClassifier
Returns:
the numerical characteristics
Throws:
Exception - if some of the characteristics could not be defined

isTrained

public boolean isTrained()
Description copied from class: AbstractClassifier
This method gives information about the state of the classifier.

Specified by:
isTrained in class AbstractClassifier
Returns:
true if the classifier is trained and therefore able to classify sequences, otherwise false

setNewAlphabetContainerInstance

public final boolean setNewAlphabetContainerInstance(AlphabetContainer abc)
Description copied from class: AbstractClassifier
This method tries to set a new instance of an AlphabetContainer for the current classifier.
This instance has to be consistent with the underlying instance of an AlphabetContainer.

This method can be very useful to save time.

Overrides:
setNewAlphabetContainerInstance in class AbstractClassifier
Parameters:
abc - the alphabets
Returns:
true if the new AlphabetContainer could be set, false otherwise
See Also:
AbstractClassifier.getAlphabetContainer(), AlphabetContainer.checkConsistency(AlphabetContainer)

train

public void train(Sample[] s,
                  double[][] weights)
           throws Exception
Description copied from class: AbstractClassifier
This method trains a classifier over an array of weighted Sample s. That is why the following has to be fulfilled: This method should work non-incrementally as the method AbstractClassifier.train(Sample...).

This method should check that the Samples are defined over the underlying alphabet and length.

Specified by:
train in class AbstractClassifier
Parameters:
s - an array of Samples
weights - the weights for the Samples
Throws:
Exception - if the weights are incorrect or the training did not succeed
See Also:
AbstractClassifier.train(Sample...)

getFurtherClassifierInfos

protected StringBuffer getFurtherClassifierInfos()
Description copied from class: AbstractClassifier
This method returns further information of a classifier as a StringBuffer. This method is used by the method AbstractClassifier.toXML() and should not be made public.

Overrides:
getFurtherClassifierInfos in class AbstractScoreBasedClassifier
Returns:
further information of a classifier as a StringBuffer
See Also:
AbstractClassifier.toXML()

getScore

protected double getScore(Sequence seq,
                          int i,
                          boolean check)
                   throws Exception
Description copied from class: AbstractScoreBasedClassifier
This method returns the score for a given Sequence and a given class.

Specified by:
getScore in class AbstractScoreBasedClassifier
Parameters:
seq - the Sequence
i - the index of the class
check - the switch to decide whether to check AlphabetContainer and the length of the Sequence or not
Returns:
the score for a given Sequence and a given class
Throws:
NotTrainedException - if the classifier is not trained
IllegalArgumentException - if something is wrong with the Sequence seq
Exception - if something went wrong

getScores

public double[] getScores(Sample s)
                   throws Exception
Description copied from class: AbstractScoreBasedClassifier
This method returns the scores of the classifier for any Sequence in the Sample. The scores are stored in the array according to the index of the Sequence in the Sample.

Only for 2-class-classifiers.

Overrides:
getScores in class AbstractScoreBasedClassifier
Parameters:
s - the Sample
Returns:
the array of scores
Throws:
Exception - if something went wrong

classify

public byte[] classify(Sample s)
                throws Exception
Description copied from class: AbstractClassifier
This method classifies all sequences of a sample and returns an array of indices of the classes to which the respective sequences are assigned with for each index i in the array 0 < i < getNumberOfClasses().

Overrides:
classify in class AbstractClassifier
Parameters:
s - the sample to be classified
Returns:
an array of class assignments
Throws:
Exception - if something went wrong during the classification

getXMLTag

protected String getXMLTag()
Description copied from class: AbstractClassifier
Returns the String that is used as tag for the XML representation of the classifier. This method is used by the methods AbstractClassifier.fromXML(StringBuffer) and AbstractClassifier.toXML().

Specified by:
getXMLTag in class AbstractClassifier
Returns:
the String that is used as tag for the XML representation of the classifier

extractFurtherClassifierInfosFromXML

protected void extractFurtherClassifierInfosFromXML(StringBuffer xml)
                                             throws NonParsableException
Description copied from class: AbstractClassifier
Extracts further information of a classifier from an XML representation. This method is used by the method AbstractClassifier.fromXML(StringBuffer) and should not be made public.

Overrides:
extractFurtherClassifierInfosFromXML in class AbstractScoreBasedClassifier
Parameters:
xml - the XML representation as StringBuffer
Throws:
NonParsableException - if the information could not be parsed out of the XML representation (the StringBuffer could not be parsed)
See Also:
AbstractClassifier.fromXML(StringBuffer)

getClassifierAnnotation

public CategoricalResult[] getClassifierAnnotation()
Description copied from class: AbstractClassifier
Returns an array of Results of dimension AbstractClassifier.getNumberOfClasses() that contains information about the classifier and for each class.

res[0] = new CategoricalResult( "classifier", "the kind of classifier", getInstanceName() );
res[1] = new CategoricalResult( "class info 0", "some information about the class", "info0" );
res[2] = new CategoricalResult( "class info 1", "some information about the class", "info1" );
...

Specified by:
getClassifierAnnotation in class AbstractClassifier
Returns:
an array of Results that contains information about the classifier