LBJ2.learn
Class WekaWrapper

java.lang.Object
  extended by LBJ2.classify.Classifier
      extended by LBJ2.learn.Learner
          extended by LBJ2.learn.WekaWrapper
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class WekaWrapper
extends Learner

Translates LBJ's internal problem representation into that which can be handled by WEKA learning algorithms.

WEKA must be available on your CLASSPATH in order to use this class. WEKA source code and pre-compiled jar distributions are available at: http://www.cs.waikato.ac.nz/ml/weka/

To use this class in with clause of a learning classifier expression, the following restrictions must be recognized:

To use this class in a Java application, the following restrictions must be recognized:

So, to retrain a classifier learned with this learner, forget() must first be called. Then, retraining is accomplished with calls to either learn(Object) or Learner.learn(Object[]). Finally, doneLearning() must be called before any calls to classify(Object) or *Value(Object) methods (e.g. Classifier.discreteValue(Object)).

On the other hand, if the LBJ compiler took care of training the learning classifier and no additional training is required, calls to classify(Object) or *Value(Object) methods may be made immediately in the application. If additional training is desired, simply call either learn(Object) or Learner.learn(Object[]), but don't forget to call doneLearning() before any calls to classify(Object) or *Value(Object) methods (e.g. Classifier.discreteValue(Object)).

See Also:
Serialized Form

Nested Class Summary
static class WekaWrapper.Parameters
          Simply a container for all of WekaWrapper's configurable parameters.
 
Field Summary
protected  weka.core.FastVector attributeInfo
          Information about the features this learner takes as input is parsed from an attribute string and stored here.
protected  weka.classifiers.Classifier baseClassifier
          Stores the instance of the WEKA classifier which we are training; default is weka.classifiers.bayes.NaiveBayes.
static java.lang.String defaultAttributeString
          Default attribute string.
protected  weka.classifiers.Classifier freshClassifier
          Stores a fresh instance of the WEKA classifier for the purposes of forgetting.
protected  weka.core.Instances instances
          The main collection of Instance objects.
protected  boolean trained
          Indicates whether the doneLearning() method has been called and the forget() method has not yet been called.
 
Fields inherited from class LBJ2.learn.Learner
extractor, labeler
 
Fields inherited from class LBJ2.classify.Classifier
containingPackage, name
 
Constructor Summary
WekaWrapper()
          Empty constructor.
WekaWrapper(weka.classifiers.Classifier base)
          Partial constructor; attribute information must be provided before any learning can occur.
WekaWrapper(weka.classifiers.Classifier base, java.lang.String attributeString)
          Redirecting constructor.
WekaWrapper(java.lang.String n)
          Empty constructor.
WekaWrapper(java.lang.String n, weka.classifiers.Classifier base)
          Partial constructor; attribute information must be provided before any learning can occur.
WekaWrapper(java.lang.String n, weka.classifiers.Classifier base, java.lang.String attributeString)
          Full Constructor.
WekaWrapper(java.lang.String n, java.lang.String attributeString)
          Default Constructor.
WekaWrapper(java.lang.String n, WekaWrapper.Parameters p)
          Initializing constructor.
WekaWrapper(WekaWrapper.Parameters p)
          Initializing constructor.
 
Method Summary
 FeatureVector classify(java.lang.Object example)
          This method makes one or more decisions about a single object, returning those decisions as Features in a vector.
 void doneLearning()
          Indicates that the classifier is finished learning.
 void forget()
          Destroys the learned version of the WEKA classifier and empties the instances collection of examples.
 Classifier getExtractor()
          Returns the extractor.
 Classifier getLabeler()
          Returns the labeler.
 void initializeAttributes(java.lang.String attributeString)
          Takes attributeString and initializes this wrapper's instances collection to take those attributes.
 void learn(java.lang.Object example)
          Since WEKA classifiers cannot learn online, this method causes no actual learning to occur, it simply creates an Instance object from this example and adds it to a set of examples from which the classifier will be built once doneLearning() is called.
private  weka.core.Instance makeInstance(FeatureVector example, FeatureVector labels)
          Creates a WEKA Instance object out of a FeatureVector.
 ScoreSet scores(java.lang.Object example)
          Produces a set of scores indicating the degree to which each possible discrete classification value is associated with the given example object.
 void setExtractor(Classifier e)
          Sets the extractor.
 void setLabeler(Classifier l)
          Sets the labeler.
 void write(java.io.PrintStream out)
          Writes the settings of the classifier in use, and a string describing the classifier, if available.
 
Methods inherited from class LBJ2.learn.Learner
learn, save
 
Methods inherited from class LBJ2.classify.Classifier
allowableValues, binaryRead, binaryRead, binaryRead, binaryRead, binaryWrite, binaryWrite, classify, clone, discreteValue, discreteValueArray, getCompositeChildren, getInputType, getOutputType, realValue, realValueArray, test, toString, valueIndexOf
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

defaultAttributeString

public static final java.lang.String defaultAttributeString
Default attribute string.

See Also:
Constant Field Values

baseClassifier

protected weka.classifiers.Classifier baseClassifier
Stores the instance of the WEKA classifier which we are training; default is weka.classifiers.bayes.NaiveBayes.


freshClassifier

protected weka.classifiers.Classifier freshClassifier
Stores a fresh instance of the WEKA classifier for the purposes of forgetting.


attributeInfo

protected weka.core.FastVector attributeInfo
Information about the features this learner takes as input is parsed from an attribute string and stored here. This information is crucial in the task of interfacing with the WEKA algorithms, and must be present before the learn(Object) method can be called.

Here is an example of a valid attribute string: nom_SimpleLabel_1,2,3,:str_First:nom_Second_a,b,c,d,r,t,:num_Third:

nom stands for "Nominal", i.e. the feature SimpleLabel was declared as discrete, and had the value list {"1","2","3"}.

str stands for "Stirng", i.e. the feature First was declared to be discrete, but was not provided with a value list. When using the WekaWrapper, it is best to provide value lists whenever possible, because very few WEKA classifiers can handle string attributes.

num stands for "Numerical", i.e. the feature Third was declared to be real.


instances

protected weka.core.Instances instances
The main collection of Instance objects.


trained

protected boolean trained
Indicates whether the doneLearning() method has been called and the forget() method has not yet been called.

Constructor Detail

WekaWrapper

public WekaWrapper()
Empty constructor. Instantiates this wrapper with the default learning algorithm: weka.classifiers.bayes.NaiveBayes. Attribute information must be provided before any learning can occur.


WekaWrapper

public WekaWrapper(weka.classifiers.Classifier base)
Partial constructor; attribute information must be provided before any learning can occur.

Parameters:
base - The classifier to be used in this system.

WekaWrapper

public WekaWrapper(weka.classifiers.Classifier base,
                   java.lang.String attributeString)
Redirecting constructor.

Parameters:
base - The classifier to be used in this system.
attributeString - The string describing the types of attributes example objects will have.

WekaWrapper

public WekaWrapper(WekaWrapper.Parameters p)
Initializing constructor. Sets all member variables to their associated settings in the WekaWrapper.Parameters object.

Parameters:
p - The settings of all parameters.

WekaWrapper

public WekaWrapper(java.lang.String n)
Empty constructor. Instantiates this wrapper with the default learning algorithm: weka.classifiers.bayes.NaiveBayes. Attribute information must be provided before any learning can occur.

Parameters:
n - The name of the classifier.

WekaWrapper

public WekaWrapper(java.lang.String n,
                   weka.classifiers.Classifier base)
Partial constructor; attribute information must be provided before any learning can occur.

Parameters:
base - The classifier to be used in this system.

WekaWrapper

public WekaWrapper(java.lang.String n,
                   java.lang.String attributeString)
Default Constructor. Instantiates this wrapper with the default learning algorithm: weka.classifiers.bayes.NaiveBayes.

Parameters:
n - The name of the classifier.
attributeString - The string describing the types of attributes example objects will have.

WekaWrapper

public WekaWrapper(java.lang.String n,
                   WekaWrapper.Parameters p)
Initializing constructor. Sets all member variables to their associated settings in the WekaWrapper.Parameters object.

Parameters:
n - The name of the classifier.
p - The settings of all parameters.

WekaWrapper

public WekaWrapper(java.lang.String n,
                   weka.classifiers.Classifier base,
                   java.lang.String attributeString)
Full Constructor.

Parameters:
n - The name of the classifier
base - The classifier to be used in this system.
attributeString - The string describing the types of attributes example objects will have.
Method Detail

initializeAttributes

public void initializeAttributes(java.lang.String attributeString)
Takes attributeString and initializes this wrapper's instances collection to take those attributes.


getLabeler

public Classifier getLabeler()
Returns the labeler.

Overrides:
getLabeler in class Learner

setLabeler

public void setLabeler(Classifier l)
Sets the labeler.

Overrides:
setLabeler in class Learner
Parameters:
l - A labeling classifier.

getExtractor

public Classifier getExtractor()
Returns the extractor.

Overrides:
getExtractor in class Learner

setExtractor

public void setExtractor(Classifier e)
Sets the extractor.

Overrides:
setExtractor in class Learner
Parameters:
e - A feature extracting classifier.

learn

public void learn(java.lang.Object example)
Since WEKA classifiers cannot learn online, this method causes no actual learning to occur, it simply creates an Instance object from this example and adds it to a set of examples from which the classifier will be built once doneLearning() is called.

Specified by:
learn in class Learner
Parameters:
example - An example of the desired learned classifier's behavior.

classify

public FeatureVector classify(java.lang.Object example)
This method makes one or more decisions about a single object, returning those decisions as Features in a vector.

Specified by:
classify in class Classifier
Parameters:
example - The object to make decisions about.
Returns:
A vector of Features about the input object.

forget

public void forget()
Destroys the learned version of the WEKA classifier and empties the instances collection of examples.

Specified by:
forget in class Learner

makeInstance

private weka.core.Instance makeInstance(FeatureVector example,
                                        FeatureVector labels)
Creates a WEKA Instance object out of a FeatureVector.


scores

public ScoreSet scores(java.lang.Object example)
Produces a set of scores indicating the degree to which each possible discrete classification value is associated with the given example object.

Specified by:
scores in class Learner
Parameters:
example - The object to make decisions about.
Returns:
A set of scores indicating the degree to which each possible discrete classification value is associated with the given example object.

write

public void write(java.io.PrintStream out)
Writes the settings of the classifier in use, and a string describing the classifier, if available.

Specified by:
write in class Learner
Parameters:
out - The output stream.

doneLearning

public void doneLearning()
Indicates that the classifier is finished learning. This method must be called if the WEKA classifier is to learn anything. Since WEKA classifiers cannot learn online, all of the training examples must be gathered and committed to first. This method invokes the WEKA classifier's buildClassifier(Instances) method.

Overrides:
doneLearning in class Learner