public class IBk extends AbstractClassifier implements OptionHandler, UpdateableClassifier, WeightedInstancesHandler, TechnicalInformationHandler, AdditionalMeasureProducer
@article{Aha1991,
author = {D. Aha and D. Kibler},
journal = {Machine Learning},
pages = {37-66},
title = {Instance-based learning algorithms},
volume = {6},
year = {1991}
}
Valid options are:
-I Weight neighbours by the inverse of their distance (use when k > 1)
-F Weight neighbours by 1 - their distance (use when k > 1)
-K <number of neighbors> Number of nearest neighbours (k) used in classification. (Default = 1)
-E Minimise mean squared error rather than mean absolute error when using -X option with numeric prediction.
-W <window size> Maximum number of training instances maintained. Training instances are dropped FIFO. (Default = no window)
-X Select the number of nearest neighbours between 1 and the k value specified using hold-one-out evaluation on the training data (use when k > 1)
-A The nearest neighbour search algorithm to use (default: weka.core.neighboursearch.LinearNNSearch).
| Modifier and Type | Field and Description |
|---|---|
protected int |
m_ClassType
The class attribute type.
|
protected boolean |
m_CrossValidate
Whether to select k by cross validation.
|
protected ZeroR |
m_defaultModel
Default ZeroR model to use when there are no training instances
|
protected int |
m_DistanceWeighting
Whether the neighbours should be distance-weighted.
|
protected int |
m_kNN
The number of neighbours to use for classification (currently).
|
protected int |
m_kNNUpper
The value of kNN provided by the user.
|
protected boolean |
m_kNNValid
Whether the value of k selected by cross validation has been invalidated by a change in the training instances.
|
protected boolean |
m_MeanSquared
Whether to minimise mean squared error rather than mean absolute error when cross-validating on numeric prediction tasks.
|
protected NearestNeighbourSearch |
m_NNSearch
for nearest-neighbor search.
|
protected double |
m_NumAttributesUsed
The number of attributes the contribute to a prediction.
|
protected int |
m_NumClasses
The number of class values (or 1 if predicting numeric).
|
protected Instances |
m_Train
The training instances used for classification.
|
protected int |
m_WindowSize
The maximum number of training instances allowed.
|
static Tag[] |
TAGS_WEIGHTING
possible instance weighting methods.
|
static int |
WEIGHT_INVERSE
weight by 1/distance.
|
static int |
WEIGHT_NONE
no weighting.
|
static int |
WEIGHT_SIMILARITY
weight by 1-distance.
|
BATCH_SIZE_DEFAULT, m_BatchSize, m_Debug, m_DoNotCheckCapabilities, m_numDecimalPlaces, NUM_DECIMAL_PLACES_DEFAULT| Constructor and Description |
|---|
IBk()
IB1 classifer.
|
IBk(int k)
IBk classifier.
|
| Modifier and Type | Method and Description |
|---|---|
void |
buildClassifier(Instances instances)
Generates the classifier.
|
protected void |
crossValidate()
Select the best value for k by hold-one-out cross-validation.
|
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.
|
java.util.Enumeration<java.lang.String> |
enumerateMeasures()
Returns an enumeration of the additional measure names produced by the neighbour search algorithm, plus the chosen K in case cross-validation is enabled.
|
Capabilities |
getCapabilities()
Returns default capabilities of the classifier.
|
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.
|
double |
getMeasure(java.lang.String additionalMeasureName)
Returns the value of the named measure from the neighbour search algorithm, plus the chosen K in case cross-validation is enabled.
|
NearestNeighbourSearch |
getNearestNeighbourSearchAlgorithm()
Returns the current nearestNeighbourSearch algorithm in use.
|
int |
getNumTraining()
Get the number of training instances the classifier is currently using.
|
java.lang.String[] |
getOptions()
Gets the current settings of IBk.
|
java.lang.String |
getRevision()
Returns the revision string.
|
TechnicalInformation |
getTechnicalInformation()
Returns an instance of a TechnicalInformation object, containing detailed information about the technical background of this class, e.g., paper reference or book this class is based on.
|
int |
getWindowSize()
Gets the maximum number of instances allowed in the training pool.
|
java.lang.String |
globalInfo()
Returns a string describing classifier.
|
protected void |
init()
Initialise scheme variables.
|
java.lang.String |
KNNTipText()
Returns the tip text for this property.
|
java.util.Enumeration<Option> |
listOptions()
Returns an enumeration describing the available options.
|
static void |
main(java.lang.String[] argv)
Main method for testing this class.
|
protected double[] |
makeDistribution(Instances neighbours,
double[] distances)
Turn the list of nearest neighbors into a probability distribution.
|
java.lang.String |
meanSquaredTipText()
Returns the tip text for this property.
|
java.lang.String |
nearestNeighbourSearchAlgorithmTipText()
Returns the tip text for this property.
|
Instances |
pruneToK(Instances neighbours,
double[] distances,
int k)
Prunes the list to contain the k nearest neighbors.
|
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 |
setNearestNeighbourSearchAlgorithm(NearestNeighbourSearch nearestNeighbourSearchAlgorithm)
Sets the nearestNeighbourSearch algorithm to be used for finding nearest neighbour(s).
|
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.
|
batchSizeTipText, classifyInstance, debugTipText, distributionsForInstances, doNotCheckCapabilitiesTipText, forName, getBatchSize, getDebug, getDoNotCheckCapabilities, getNumDecimalPlaces, implementsMoreEfficientBatchPrediction, makeCopies, makeCopy, numDecimalPlacesTipText, postExecution, preExecution, run, runClassifier, setBatchSize, setDebug, setDoNotCheckCapabilities, setNumDecimalPlacesprotected Instances m_Train
protected int m_NumClasses
protected int m_ClassType
protected int m_kNN
protected int m_kNNUpper
protected boolean m_kNNValid
protected int m_WindowSize
protected int m_DistanceWeighting
protected boolean m_CrossValidate
protected boolean m_MeanSquared
protected ZeroR m_defaultModel
public static final int WEIGHT_NONE
public static final int WEIGHT_INVERSE
public static final int WEIGHT_SIMILARITY
public static final Tag[] TAGS_WEIGHTING
protected NearestNeighbourSearch m_NNSearch
protected double m_NumAttributesUsed
public IBk(int k)
k - the number of nearest neighbors to use for predictionpublic IBk()
public java.lang.String globalInfo()
public TechnicalInformation getTechnicalInformation()
getTechnicalInformation in interface TechnicalInformationHandlerpublic java.lang.String KNNTipText()
public void setKNN(int k)
k - the number of neighbours.public int getKNN()
public java.lang.String windowSizeTipText()
public int getWindowSize()
public void setWindowSize(int newWindowSize)
newWindowSize - Value to assign to WindowSize.public java.lang.String distanceWeightingTipText()
public SelectedTag getDistanceWeighting()
public void setDistanceWeighting(SelectedTag newMethod)
newMethod - the distance weighting method to usepublic java.lang.String meanSquaredTipText()
public boolean getMeanSquared()
public void setMeanSquared(boolean newMeanSquared)
newMeanSquared - true if so.public java.lang.String crossValidateTipText()
public boolean getCrossValidate()
public void setCrossValidate(boolean newCrossValidate)
newCrossValidate - true if cross-validation should be used.public java.lang.String nearestNeighbourSearchAlgorithmTipText()
public NearestNeighbourSearch getNearestNeighbourSearchAlgorithm()
public void setNearestNeighbourSearchAlgorithm(NearestNeighbourSearch nearestNeighbourSearchAlgorithm)
nearestNeighbourSearchAlgorithm - - The NearestNeighbourSearch class.public int getNumTraining()
public Capabilities getCapabilities()
getCapabilities in interface ClassifiergetCapabilities in interface CapabilitiesHandlergetCapabilities in class AbstractClassifierCapabilitiespublic void buildClassifier(Instances instances) throws java.lang.Exception
buildClassifier in interface Classifierinstances - set of instances serving as training datajava.lang.Exception - if the classifier has not been generated successfullypublic void updateClassifier(Instance instance) throws java.lang.Exception
updateClassifier in interface UpdateableClassifierinstance - the instance to addjava.lang.Exception - if instance could not be incorporated successfullypublic double[] distributionForInstance(Instance instance) throws java.lang.Exception
distributionForInstance in interface ClassifierdistributionForInstance in class AbstractClassifierinstance - the instance to be classifiedjava.lang.Exception - if an error occurred during the predictionpublic java.util.Enumeration<Option> listOptions()
listOptions in interface OptionHandlerlistOptions in class AbstractClassifierpublic void setOptions(java.lang.String[] options)
throws java.lang.Exception
-I Weight neighbours by the inverse of their distance (use when k > 1)
-F Weight neighbours by 1 - their distance (use when k > 1)
-K <number of neighbors> Number of nearest neighbours (k) used in classification. (Default = 1)
-E Minimise mean squared error rather than mean absolute error when using -X option with numeric prediction.
-W <window size> Maximum number of training instances maintained. Training instances are dropped FIFO. (Default = no window)
-X Select the number of nearest neighbours between 1 and the k value specified using hold-one-out evaluation on the training data (use when k > 1)
-A The nearest neighbour search algorithm to use (default: weka.core.neighboursearch.LinearNNSearch).
setOptions in interface OptionHandlersetOptions in class AbstractClassifieroptions - the list of options as an array of stringsjava.lang.Exception - if an option is not supportedpublic java.lang.String[] getOptions()
getOptions in interface OptionHandlergetOptions in class AbstractClassifierpublic java.util.Enumeration<java.lang.String> enumerateMeasures()
enumerateMeasures in interface AdditionalMeasureProducerpublic double getMeasure(java.lang.String additionalMeasureName)
getMeasure in interface AdditionalMeasureProduceradditionalMeasureName - the name of the measure to query for its valuejava.lang.IllegalArgumentException - if the named measure is not supportedpublic java.lang.String toString()
toString in class java.lang.Objectprotected void init()
protected double[] makeDistribution(Instances neighbours, double[] distances) throws java.lang.Exception
neighbours - the list of nearest neighboring instancesdistances - the distances of the neighborsjava.lang.Exception - if computation goes wrong or has no class attributeprotected void crossValidate()
throws java.lang.InterruptedException
java.lang.InterruptedExceptionpublic Instances pruneToK(Instances neighbours, double[] distances, int k)
neighbours - the neighbour instances.distances - the distances of the neighbours from target instance.k - the number of neighbors to keep.public java.lang.String getRevision()
getRevision in interface RevisionHandlergetRevision in class AbstractClassifierpublic static void main(java.lang.String[] argv)
argv - should contain command line options (see setOptions)