weka.classifiers.lazy
Class IBk

java.lang.Object
  |
  +--weka.classifiers.Classifier
        |
        +--weka.classifiers.lazy.IBk
All Implemented Interfaces:
java.lang.Cloneable, OptionHandler, java.io.Serializable, UpdateableClassifier, WeightedInstancesHandler

public class IBk
extends Classifier
implements OptionHandler, UpdateableClassifier, WeightedInstancesHandler

K-nearest neighbours classifier. For more information, see

Aha, D., and D. Kibler (1991) "Instance-based learning algorithms", Machine Learning, vol.6, pp. 37-66.

Valid options are:

-K num
Set the number of nearest neighbors to use in prediction (default 1)

-W num
Set a fixed window size for incremental train/testing. As new training instances are added, oldest instances are removed to maintain the number of training instances at this size. (default no window)

-I
Neighbors will be weighted by the inverse of their distance when voting. (default equal weighting)

-F
Neighbors will be weighted by their similarity when voting. (default equal weighting)

-X
Select the number of neighbors to use by hold-one-out cross validation, with an upper limit given by the -K option.

-E
When k is selected by cross-validation for numeric class attributes, minimize mean-squared error. (default mean absolute error)

-N
Turns off normalization.

Version:
$Revision: 1.29 $
Author:
Stuart Inglis (singlis@cs.waikato.ac.nz), Len Trigg (trigg@cs.waikato.ac.nz), Eibe Frank (eibe@cs.waikato.ac.nz)
See Also:
Serialized Form

Field Summary
static Tag[] TAGS_WEIGHTING
           
static int WEIGHT_INVERSE
           
static int WEIGHT_NONE
           
static int WEIGHT_SIMILARITY
           
 
Constructor Summary
IBk()
          IB1 classifer.
IBk(int k)
          IBk classifier.
 
Method Summary
 void buildClassifier(Instances instances)
          Generates the classifier.
 java.lang.String crossValidateTipText()
          Returns the tip text for this property
 java.lang.String distanceWeightingTipText()
          Returns the tip text for this property
 double[] distributionForInstance(Instance instance)
          Calculates the class membership probabilities for the given test instance.
 double getAttributeMax(int index)
          Get an attributes maximum observed value
 double getAttributeMin(int index)
          Get an attributes minimum observed value
 boolean getCrossValidate()
          Gets whether hold-one-out cross-validation will be used to select the best k value
 SelectedTag getDistanceWeighting()
          Gets the distance weighting method used.
 int getKNN()
          Gets the number of neighbours the learner will use.
 boolean getMeanSquared()
          Gets whether the mean squared error is used rather than mean absolute error when doing cross-validation.
 boolean getNoNormalization()
          Gets whether normalization is turned off.
 int getNumTraining()
          Get the number of training instances the classifier is currently using
 java.lang.String[] getOptions()
          Gets the current settings of IBk.
 int getWindowSize()
          Gets the maximum number of instances allowed in the training pool.
 java.lang.String globalInfo()
          Returns a string describing classifier
 java.lang.String KNNTipText()
          Returns the tip text for this property
 java.util.Enumeration listOptions()
          Returns an enumeration describing the available options.
static void main(java.lang.String[] argv)
          Main method for testing this class.
 java.lang.String meanSquaredTipText()
          Returns the tip text for this property
 java.lang.String noNormalizationTipText()
          Returns the tip text for this property
 void setCrossValidate(boolean newCrossValidate)
          Sets whether hold-one-out cross-validation will be used to select the best k value
 void setDistanceWeighting(SelectedTag newMethod)
          Sets the distance weighting method used.
 void setKNN(int k)
          Set the number of neighbours the learner is to use.
 void setMeanSquared(boolean newMeanSquared)
          Sets whether the mean squared error is used rather than mean absolute error when doing cross-validation.
 void setNoNormalization(boolean v)
          Set whether normalization is turned off.
 void setOptions(java.lang.String[] options)
          Parses a given list of options.
 void setWindowSize(int newWindowSize)
          Sets the maximum number of instances allowed in the training pool.
 java.lang.String toString()
          Returns a description of this classifier.
 void updateClassifier(Instance instance)
          Adds the supplied instance to the training set
 java.lang.String windowSizeTipText()
          Returns the tip text for this property
 
Methods inherited from class weka.classifiers.Classifier
classifyInstance, debugTipText, forName, getDebug, makeCopies, setDebug
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

WEIGHT_NONE

public static final int WEIGHT_NONE
See Also:
Constant Field Values

WEIGHT_INVERSE

public static final int WEIGHT_INVERSE
See Also:
Constant Field Values

WEIGHT_SIMILARITY

public static final int WEIGHT_SIMILARITY
See Also:
Constant Field Values

TAGS_WEIGHTING

public static final Tag[] TAGS_WEIGHTING
Constructor Detail

IBk

public IBk(int k)
IBk classifier. Simple instance-based learner that uses the class of the nearest k training instances for the class of the test instances.

Parameters:
k - the number of nearest neighbors to use for prediction

IBk

public IBk()
IB1 classifer. Instance-based learner. Predicts the class of the single nearest training instance for each test instance.

Method Detail

globalInfo

public java.lang.String globalInfo()
Returns a string describing classifier

Returns:
a description suitable for displaying in the explorer/experimenter gui

KNNTipText

public java.lang.String KNNTipText()
Returns the tip text for this property

Returns:
tip text for this property suitable for displaying in the explorer/experimenter gui

setKNN

public void setKNN(int k)
Set the number of neighbours the learner is to use.

Parameters:
k - the number of neighbours.

getKNN

public int getKNN()
Gets the number of neighbours the learner will use.

Returns:
the number of neighbours.

windowSizeTipText

public java.lang.String windowSizeTipText()
Returns the tip text for this property

Returns:
tip text for this property suitable for displaying in the explorer/experimenter gui

getWindowSize

public int getWindowSize()
Gets the maximum number of instances allowed in the training pool. The addition of new instances above this value will result in old instances being removed. A value of 0 signifies no limit to the number of training instances.

Returns:
Value of WindowSize.

setWindowSize

public void setWindowSize(int newWindowSize)
Sets the maximum number of instances allowed in the training pool. The addition of new instances above this value will result in old instances being removed. A value of 0 signifies no limit to the number of training instances.

Parameters:
newWindowSize - Value to assign to WindowSize.

distanceWeightingTipText

public java.lang.String distanceWeightingTipText()
Returns the tip text for this property

Returns:
tip text for this property suitable for displaying in the explorer/experimenter gui

getDistanceWeighting

public SelectedTag getDistanceWeighting()
Gets the distance weighting method used. Will be one of WEIGHT_NONE, WEIGHT_INVERSE, or WEIGHT_SIMILARITY

Returns:
the distance weighting method used.

setDistanceWeighting

public void setDistanceWeighting(SelectedTag newMethod)
Sets the distance weighting method used. Values other than WEIGHT_NONE, WEIGHT_INVERSE, or WEIGHT_SIMILARITY will be ignored.


meanSquaredTipText

public java.lang.String meanSquaredTipText()
Returns the tip text for this property

Returns:
tip text for this property suitable for displaying in the explorer/experimenter gui

getMeanSquared

public boolean getMeanSquared()
Gets whether the mean squared error is used rather than mean absolute error when doing cross-validation.

Returns:
true if so.

setMeanSquared

public void setMeanSquared(boolean newMeanSquared)
Sets whether the mean squared error is used rather than mean absolute error when doing cross-validation.

Parameters:
newMeanSquared - true if so.

crossValidateTipText

public java.lang.String crossValidateTipText()
Returns the tip text for this property

Returns:
tip text for this property suitable for displaying in the explorer/experimenter gui

getCrossValidate

public boolean getCrossValidate()
Gets whether hold-one-out cross-validation will be used to select the best k value

Returns:
true if cross-validation will be used.

setCrossValidate

public void setCrossValidate(boolean newCrossValidate)
Sets whether hold-one-out cross-validation will be used to select the best k value

Parameters:
newCrossValidate - true if cross-validation should be used.

getNumTraining

public int getNumTraining()
Get the number of training instances the classifier is currently using


getAttributeMin

public double getAttributeMin(int index)
                       throws java.lang.Exception
Get an attributes minimum observed value

java.lang.Exception

getAttributeMax

public double getAttributeMax(int index)
                       throws java.lang.Exception
Get an attributes maximum observed value

java.lang.Exception

noNormalizationTipText

public java.lang.String noNormalizationTipText()
Returns the tip text for this property

Returns:
tip text for this property suitable for displaying in the explorer/experimenter gui

getNoNormalization

public boolean getNoNormalization()
Gets whether normalization is turned off.

Returns:
Value of DontNormalize.

setNoNormalization

public void setNoNormalization(boolean v)
Set whether normalization is turned off.

Parameters:
v - Value to assign to DontNormalize.

buildClassifier

public void buildClassifier(Instances instances)
                     throws java.lang.Exception
Generates the classifier.

Specified by:
buildClassifier in class Classifier
Parameters:
instances - set of instances serving as training data
Throws:
java.lang.Exception - if the classifier has not been generated successfully

updateClassifier

public void updateClassifier(Instance instance)
                      throws java.lang.Exception
Adds the supplied instance to the training set

Specified by:
updateClassifier in interface UpdateableClassifier
Parameters:
instance - the instance to add
Throws:
java.lang.Exception - if instance could not be incorporated successfully

distributionForInstance

public double[] distributionForInstance(Instance instance)
                                 throws java.lang.Exception
Calculates the class membership probabilities for the given test instance.

Overrides:
distributionForInstance in class Classifier
Parameters:
instance - the instance to be classified
Returns:
predicted class probability distribution
Throws:
java.lang.Exception - if an error occurred during the prediction

listOptions

public java.util.Enumeration listOptions()
Returns an enumeration describing the available options.

Specified by:
listOptions in interface OptionHandler
Overrides:
listOptions in class Classifier
Returns:
an enumeration of all the available options.

setOptions

public void setOptions(java.lang.String[] options)
                throws java.lang.Exception
Parses a given list of options. Valid options are:

-K num
Set the number of nearest neighbors to use in prediction (default 1)

-W num
Set a fixed window size for incremental train/testing. As new training instances are added, oldest instances are removed to maintain the number of training instances at this size. (default no window)

-I
Neighbors will be weighted by the inverse of their distance when voting. (default equal weighting)

-F
Neighbors will be weighted by their similarity when voting. (default equal weighting)

-X
Select the number of neighbors to use by hold-one-out cross validation, with an upper limit given by the -K option.

-E
When k is selected by cross-validation for numeric class attributes, minimize mean-squared error. (default mean absolute error)

Specified by:
setOptions in interface OptionHandler
Overrides:
setOptions in class Classifier
Parameters:
options - the list of options as an array of strings
Throws:
java.lang.Exception - if an option is not supported

getOptions

public java.lang.String[] getOptions()
Gets the current settings of IBk.

Specified by:
getOptions in interface OptionHandler
Overrides:
getOptions in class Classifier
Returns:
an array of strings suitable for passing to setOptions()

toString

public java.lang.String toString()
Returns a description of this classifier.

Overrides:
toString in class java.lang.Object
Returns:
a description of this classifier as a string.

main

public static void main(java.lang.String[] argv)
Main method for testing this class.

Parameters:
argv - should contain command line options (see setOptions)


Copyright (c) 2003 David Lindsay, Computer Learning Research Centre, Dept. Computer Science, Royal Holloway, University of London