weka.core.metrics
Class LearnableMetric

java.lang.Object
  extended byweka.core.metrics.Metric
      extended byweka.core.metrics.LearnableMetric
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable
Direct Known Subclasses:
BarHillelMetric, BarHillelMetricMatlab, KL, WeightedDotP, WeightedEuclidean, WeightedMahalanobis, XingMetric

public abstract class LearnableMetric
extends Metric

Interface to distance metrics that can be learned

See Also:
Serialized Form

Field Summary
protected  double[] m_attrWeights
          Weights of individual attributes
 Classifier m_classifier
           
protected  java.lang.String m_classifierClassName
           
protected  boolean m_classifierRequiresNominalClass
          Certain classifiers may use non-nominal class attributes
protected  int m_numPosDiffInstances
          The maximum number of same-class examples to construct diff-instances from
protected  double m_posNegDiffInstanceRatio
          Proportion of different-class versus same-class diff-instances
protected  boolean m_trainable
          True if metric learning is used.
 
Fields inherited from class weka.core.metrics.Metric
m_attrIdxs, m_classIndex, m_numAttributes
 
Constructor Summary
LearnableMetric()
           
 
Method Summary
 java.lang.Object clone()
          Create a copy of this metric
abstract  Instance createDiffInstance(Instance instance1, Instance instance2)
          Create an instance with features corresponding to components of the two given instances
abstract  Instance getCentroidInstance(Instances instances, boolean fastMode, boolean normalized)
          Given a cluster of instances, return the centroid of that cluster
 boolean getExternal()
          Get the value of m_external
abstract  double[] getGradients(Instance instance1, Instance instance2)
          Get the values of the partial derivates for the metric components for a particular instance pair
 int getNumPosDiffInstances()
          Set the number of positive instances to be used for training
 double getPosNegDiffInstanceRatio()
          Get the ratio of positive and negative instances to be used for training
 boolean getTrainable()
          Get the value of metricTraining
 double[] getWeights()
          Get the feature weights
abstract  void learnMetric(Instances data)
          Train the distance metric.
 double[] meanOrMode(Instances insts)
          Fast version of meanOrMode - streamlined from Instances.meanOrMode for efficiency Does not check for missing attributes, assumes numeric attributes, assumes Sparse instances
 void normalizeInstanceWeighted(Instance inst)
          Normalizes the values of an Instance utilizing feature weights
abstract  void resetMetric()
          Reset all values that have been learned
 void setExternal(boolean external)
          Set the value of m_external
 void setNumPosDiffInstances(int numPosInstances)
          Set the number of positive instances to be used for training
 void setPosNegDiffInstanceRatio(double ratio)
          Set the ratio of positive and negative instances to be used for training
 void setTrainable(boolean metricTraining)
          Set the value of metricTraining
 void setWeights(double[] _weights)
          Set the feature weights
 void useClassifier(java.lang.String classifierClassName, boolean classifierRequiresNominalClass)
          switch from calculating the metric to pair-space classification
 void useNoClassifier()
          switch from using a classifier in difference-space to vanilla L-1 norm distance
 boolean usesClassifier()
          Is this metric defined in vanilla space, or difference space?
 
Methods inherited from class weka.core.metrics.Metric
buildMetric, buildMetric, buildMetric, distance, distanceNonWeighted, forName, getAttrIdxs, getAttrIdxsWithoutLastClass, getAttrIndxs, getClassIndex, getNumAttributes, isDistanceBased, length, normalizeInstance, setAttrIdxs, setAttrIdxs, setClassIndex, similarity, similarityNonWeighted
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_attrWeights

protected double[] m_attrWeights
Weights of individual attributes


m_numPosDiffInstances

protected int m_numPosDiffInstances
The maximum number of same-class examples to construct diff-instances from


m_posNegDiffInstanceRatio

protected double m_posNegDiffInstanceRatio
Proportion of different-class versus same-class diff-instances


m_classifierClassName

protected java.lang.String m_classifierClassName

m_classifier

public Classifier m_classifier

m_classifierRequiresNominalClass

protected boolean m_classifierRequiresNominalClass
Certain classifiers may use non-nominal class attributes


m_trainable

protected boolean m_trainable
True if metric learning is used. Set to false to turn off metric learning

Constructor Detail

LearnableMetric

public LearnableMetric()
Method Detail

learnMetric

public abstract void learnMetric(Instances data)
                          throws java.lang.Exception
Train the distance metric. A specific metric will take care of its own training either via a metric learner or by itself.

Throws:
java.lang.Exception

useClassifier

public void useClassifier(java.lang.String classifierClassName,
                          boolean classifierRequiresNominalClass)
                   throws java.lang.Exception
switch from calculating the metric to pair-space classification

Parameters:
classifierClassName - Some classifier that classifies pairs of points
classifierRequiresNominalClass - does classifier need a nominal class attribute? Using DistributionClassifier because it actually reports a margin SMO is first, will try others as well
Throws:
java.lang.Exception

useNoClassifier

public void useNoClassifier()
switch from using a classifier in difference-space to vanilla L-1 norm distance


usesClassifier

public boolean usesClassifier()
Is this metric defined in vanilla space, or difference space?

Returns:
true if metric uses a classifier to classify L1-Norm as in-cluster or out-of-cluster

resetMetric

public abstract void resetMetric()
                          throws java.lang.Exception
Reset all values that have been learned

Throws:
java.lang.Exception

createDiffInstance

public abstract Instance createDiffInstance(Instance instance1,
                                            Instance instance2)
Create an instance with features corresponding to components of the two given instances

Parameters:
instance1 - first instance
instance2 - second instance

getGradients

public abstract double[] getGradients(Instance instance1,
                                      Instance instance2)
                               throws java.lang.Exception
Get the values of the partial derivates for the metric components for a particular instance pair

Parameters:
instance1 - the first instance
instance2 - the first instance
Throws:
java.lang.Exception

setWeights

public void setWeights(double[] _weights)
                throws java.lang.Exception
Set the feature weights

Throws:
java.lang.Exception

getWeights

public double[] getWeights()
Get the feature weights

Returns:
an array of feature weights

getCentroidInstance

public abstract Instance getCentroidInstance(Instances instances,
                                             boolean fastMode,
                                             boolean normalized)
Given a cluster of instances, return the centroid of that cluster

Parameters:
instances - data points belonging to a cluster
Returns:
a centroid instance for the given cluster

meanOrMode

public double[] meanOrMode(Instances insts)
Fast version of meanOrMode - streamlined from Instances.meanOrMode for efficiency Does not check for missing attributes, assumes numeric attributes, assumes Sparse instances


getTrainable

public boolean getTrainable()
Get the value of metricTraining

Returns:
Value of metricTraining

setTrainable

public void setTrainable(boolean metricTraining)
Set the value of metricTraining

Parameters:
metricTraining - Value of metricTraining

getExternal

public boolean getExternal()
Get the value of m_external

Returns:
Value of m_external

setExternal

public void setExternal(boolean external)
Set the value of m_external

Parameters:
external - if true, an external estimator will be used for distance

setNumPosDiffInstances

public void setNumPosDiffInstances(int numPosInstances)
Set the number of positive instances to be used for training

Parameters:
numPosInstances - the number amounts of positive examples (diff-instances)

getNumPosDiffInstances

public int getNumPosDiffInstances()
Set the number of positive instances to be used for training


setPosNegDiffInstanceRatio

public void setPosNegDiffInstanceRatio(double ratio)
Set the ratio of positive and negative instances to be used for training

Parameters:
ratio - the relative amounts of negative examples compared to positive examples. If -1, all possible negatives will be used (use with care!)

getPosNegDiffInstanceRatio

public double getPosNegDiffInstanceRatio()
Get the ratio of positive and negative instances to be used for training


normalizeInstanceWeighted

public void normalizeInstanceWeighted(Instance inst)
                               throws java.lang.Exception
Normalizes the values of an Instance utilizing feature weights

Parameters:
inst - Instance to be normalized
Throws:
java.lang.Exception

clone

public java.lang.Object clone()
Create a copy of this metric

Overrides:
clone in class Metric