weka.core.metrics
Class Metric

java.lang.Object
  extended byweka.core.metrics.Metric
All Implemented Interfaces:
java.lang.Cloneable, java.io.Serializable
Direct Known Subclasses:
LearnableMetric

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

Abstract Metric class

See Also:
Serialized Form

Field Summary
protected  int[] m_attrIdxs
          indeces of attributes which the metric works on
protected  int m_classIndex
          index of the class attribute
protected  int m_numAttributes
          number of attributes
 
Constructor Summary
Metric()
           
 
Method Summary
abstract  void buildMetric(Instances data)
          Create a new metric for operating on specified instances
abstract  void buildMetric(int numAttributes)
          Generates a new Metric with a specified number of attributes.
abstract  void buildMetric(int numAttributes, java.lang.String[] options)
          Generates a new Metric.
 java.lang.Object clone()
          Create a copy of this metric
abstract  double distance(Instance instance1, Instance instance2)
          Returns a distance value between two instances.
abstract  double distanceNonWeighted(Instance instance1, Instance instance2)
          Returns distance between two instances without using the weights.
static Metric forName(java.lang.String metricName, java.lang.String[] options)
          Creates a new instance of a metric given it's class name and (optional) arguments to pass to it's setOptions method.
 int[] getAttrIdxs(Instances instances)
          This function takes instances, and returns an array of integers 0..(num_attributes-1)
 int[] getAttrIdxsWithoutLastClass(Instances instances)
          It is often the case that last attribute of the data is the class.
 int[] getAttrIndxs()
          Returns an array of attribute incece which will be used by the metric
 int getClassIndex(int classIndex)
          Get the index of the attribute is the class attribute
 int getNumAttributes()
          Get the number of attributes that the metric uses
abstract  boolean isDistanceBased()
          The computation of a metric can be either based on distance, or on similarity
static double length(Instance instance)
          Get the norm-2 length of an instance assuming all attributes are numeric
 void normalizeInstance(Instance inst)
          Normalizes the values of a normal Instance
 void setAttrIdxs(int[] attrIdxs)
          Specifies a list of attributes which will be used by the metric
 void setAttrIdxs(int startIdx, int endIdx)
          Specifies an interval of attributes which will be used by the metric
 void setClassIndex(int classIndex)
          Specify which attribute is the class attribute
abstract  double similarity(Instance instance1, Instance instance2)
          Returns a similarity estimate between two instances.
abstract  double similarityNonWeighted(Instance instance1, Instance instance2)
          Returns similarity value between two instances without using the weights.
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_numAttributes

protected int m_numAttributes
number of attributes


m_attrIdxs

protected int[] m_attrIdxs
indeces of attributes which the metric works on


m_classIndex

protected int m_classIndex
index of the class attribute

Constructor Detail

Metric

public Metric()
Method Detail

buildMetric

public abstract void buildMetric(int numAttributes)
                          throws java.lang.Exception
Generates a new Metric with a specified number of attributes. Has to initialize all fields of the metric with default values.

Parameters:
numAttributes - the number of attributes that the metric will work on
Throws:
java.lang.Exception - if the distance metric has not been generated successfully.

buildMetric

public abstract void buildMetric(int numAttributes,
                                 java.lang.String[] options)
                          throws java.lang.Exception
Generates a new Metric. Has to initialize all fields of the metric with default values

Parameters:
numAttributes - the number of attributes that the metric will work on
options - an array of options suitable for passing to setOptions. May be null.
Throws:
java.lang.Exception - if the distance metric has not been generated successfully.

buildMetric

public abstract void buildMetric(Instances data)
                          throws java.lang.Exception
Create a new metric for operating on specified instances

Parameters:
data - instances that the metric will be used on
Throws:
java.lang.Exception

setAttrIdxs

public void setAttrIdxs(int[] attrIdxs)
Specifies a list of attributes which will be used by the metric


getAttrIndxs

public int[] getAttrIndxs()
Returns an array of attribute incece which will be used by the metric

Returns:
an array of attribute indices

setAttrIdxs

public void setAttrIdxs(int startIdx,
                        int endIdx)
Specifies an interval of attributes which will be used by the metric


distance

public abstract double distance(Instance instance1,
                                Instance instance2)
                         throws java.lang.Exception
Returns a distance value between two instances.

Parameters:
instance1 - First instance.
instance2 - Second instance.
Throws:
java.lang.Exception - if distance could not be estimated.

similarity

public abstract double similarity(Instance instance1,
                                  Instance instance2)
                           throws java.lang.Exception
Returns a similarity estimate between two instances.

Parameters:
instance1 - First instance.
instance2 - Second instance.
Throws:
java.lang.Exception - if similarity could not be estimated.

similarityNonWeighted

public abstract double similarityNonWeighted(Instance instance1,
                                             Instance instance2)
                                      throws java.lang.Exception
Returns similarity value between two instances without using the weights.

Parameters:
instance1 - First instance.
instance2 - Second instance.
Throws:
java.lang.Exception - if similarity could not be estimated.

distanceNonWeighted

public abstract double distanceNonWeighted(Instance instance1,
                                           Instance instance2)
                                    throws java.lang.Exception
Returns distance between two instances without using the weights.

Parameters:
instance1 - First instance.
instance2 - Second instance.
Throws:
java.lang.Exception - if similarity could not be estimated.

getAttrIdxsWithoutLastClass

public int[] getAttrIdxsWithoutLastClass(Instances instances)
It is often the case that last attribute of the data is the class. This function takes instances, and returns an array of integers 0..(num_attributes-1 - 1) to exclude the class attribute

Returns:
array of integer indeces of attributes, excluding last one which is the class index

getAttrIdxs

public int[] getAttrIdxs(Instances instances)
This function takes instances, and returns an array of integers 0..(num_attributes-1)

Returns:
array of integer indeces of attributes

setClassIndex

public void setClassIndex(int classIndex)
Specify which attribute is the class attribute


getClassIndex

public int getClassIndex(int classIndex)
Get the index of the attribute is the class attribute


getNumAttributes

public int getNumAttributes()
Get the number of attributes that the metric uses


isDistanceBased

public abstract boolean isDistanceBased()
The computation of a metric can be either based on distance, or on similarity


length

public static double length(Instance instance)
Get the norm-2 length of an instance assuming all attributes are numeric


normalizeInstance

public void normalizeInstance(Instance inst)
                       throws java.lang.Exception
Normalizes the values of a normal Instance

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

forName

public static Metric forName(java.lang.String metricName,
                             java.lang.String[] options)
                      throws java.lang.Exception
Creates a new instance of a metric given it's class name and (optional) arguments to pass to it's setOptions method. If the classifier implements OptionHandler and the options parameter is non-null, the classifier will have it's options set.

Parameters:
metricName - the fully qualified class name of the metric
options - an array of options suitable for passing to setOptions. May be null.
Returns:
the newly created metric ready for use.
Throws:
java.lang.Exception - if the metric name is invalid, or the options supplied are not acceptable to the metric

clone

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