LBJ2.learn
Class SparseWeightVector

java.lang.Object
  extended by LBJ2.learn.SparseWeightVector
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
NaiveBayes.NaiveBayesVector, SparseAveragedPerceptron.AveragedWeightVector

public class SparseWeightVector
extends java.lang.Object
implements java.lang.Cloneable, java.io.Serializable

This class is used as a weight vector in sparse learning algorithms. Features are associated with Doubles and/or with double[]s representing the weights of the features they produce. Features not appearing in the vector are assumed to have the defaultWeight.

See Also:
Serialized Form

Nested Class Summary
protected  class SparseWeightVector.WeightIterator
          This class simultaneously iterates over the features in the given vector and their corresponding weights from a SparseWeightVector.
 
Field Summary
protected static double defaultWeight
          When a feature appears in an example but not in this vector, it is assumed to have this weight.
protected  java.util.HashMap weights
          The weights in the vector indexed by Feature.
 
Constructor Summary
SparseWeightVector()
          Simply instantiates weights.
SparseWeightVector(java.util.HashMap w)
          Simply initializes weights.
 
Method Summary
 void clear()
          Empties the weight map.
 java.lang.Object clone()
          Returns a copy of this SparseWeightVector in which the weights variable has been cloned deeply.
 double dot(FeatureVector example)
          Takes the dot product of this SparseWeightVector with the argument vector, using the hard coded default weight.
 double dot(FeatureVector example, double defaultW)
          Takes the dot product of this SparseWeightVector with the argument vector, using the specified default weight when one is not yet present in this vector.
 void scaledAdd(FeatureVector example)
          Self-modifying vector addition.
 void scaledAdd(FeatureVector example, double factor)
          Self-modifying vector addition where the argument vector is first scaled by the given factor.
 void scaledAdd(FeatureVector example, double factor, double defaultW)
          Self-modifying vector addition where the argument vector is first scaled by the given factor.
 java.lang.String toString()
          Converts this SparseWeightVector into a String.
 SparseWeightVector.WeightIterator weightIterator(FeatureVector example)
          Produces an iterator that accesses the data in this vector associated with the features in the argument vector.
 void write(java.io.PrintStream out)
          Outputs the contents of this SparseWeightVector into the specified PrintStream.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

defaultWeight

protected static final double defaultWeight
When a feature appears in an example but not in this vector, it is assumed to have this weight.

See Also:
Constant Field Values

weights

protected java.util.HashMap weights
The weights in the vector indexed by Feature.

Constructor Detail

SparseWeightVector

public SparseWeightVector()
Simply instantiates weights.


SparseWeightVector

public SparseWeightVector(java.util.HashMap w)
Simply initializes weights.

Parameters:
w - A map of weights.
Method Detail

weightIterator

public SparseWeightVector.WeightIterator weightIterator(FeatureVector example)
Produces an iterator that accesses the data in this vector associated with the features in the argument vector.

Parameters:
example - A vector of features extracted from an example object.
Returns:
A weight iterator.

dot

public double dot(FeatureVector example)
Takes the dot product of this SparseWeightVector with the argument vector, using the hard coded default weight.

Parameters:
example - A vector of features extracted from an example object.
Returns:
The computed dot product.

dot

public double dot(FeatureVector example,
                  double defaultW)
Takes the dot product of this SparseWeightVector with the argument vector, using the specified default weight when one is not yet present in this vector.

Parameters:
example - A vector of features extracted from an example object.
defaultW - The default weight.
Returns:
The computed dot product.

scaledAdd

public void scaledAdd(FeatureVector example)
Self-modifying vector addition.

Parameters:
example - A vector of features extracted from an example object.

scaledAdd

public void scaledAdd(FeatureVector example,
                      double factor)
Self-modifying vector addition where the argument vector is first scaled by the given factor. The default weight is used to initialize new feature weights.

Parameters:
example - A vector of features extracted from an example object.
factor - The scaling factor.

scaledAdd

public void scaledAdd(FeatureVector example,
                      double factor,
                      double defaultW)
Self-modifying vector addition where the argument vector is first scaled by the given factor.

Parameters:
example - A vector of features extracted from an example object.
factor - The scaling factor.
defaultW - An initial weight for previously unseen features.

clear

public void clear()
Empties the weight map.


toString

public java.lang.String toString()
Converts this SparseWeightVector into a String.

Overrides:
toString in class java.lang.Object
Returns:
A String holding a textual representation of this vector.

write

public void write(java.io.PrintStream out)
Outputs the contents of this SparseWeightVector into the specified PrintStream. This method merely writes the value returned by toString().

Parameters:
out - The stream to output into.

clone

public java.lang.Object clone()
Returns a copy of this SparseWeightVector in which the weights variable has been cloned deeply.

Overrides:
clone in class java.lang.Object
Returns:
A copy of this SparseWeightVector.