|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectweka.classifiers.Classifier
weka.classifiers.lazy.IBk
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.
Nested Class Summary | |
private class |
IBk.NeighborList
|
private class |
IBk.NeighborNode
|
Field Summary | |
protected int |
m_ClassType
The class attribute type |
protected boolean |
m_CrossValidate
Whether to select k by cross validation |
protected int |
m_DistanceWeighting
Whether the neighbours should be distance-weighted |
protected boolean |
m_DontNormalize
True if normalization is turned off |
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 double[] |
m_Max
The maximum values for numeric attributes. |
protected boolean |
m_MeanSquared
Whether to minimise mean squared error rather than mean absolute error when cross-validating on numeric prediction tasks |
protected double[] |
m_Min
The minimum values for numeric attributes. |
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
|
static int |
WEIGHT_INVERSE
|
static int |
WEIGHT_NONE
|
static int |
WEIGHT_SIMILARITY
|
Fields inherited from class weka.classifiers.Classifier |
m_Debug |
Constructor Summary | |
IBk()
IB1 classifer. |
|
IBk(int k)
IBk classifier. |
Method Summary | |
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 |
protected double |
difference(int index,
double val1,
double val2)
Computes the difference between two given attribute values. |
protected double |
distance(Instance first,
Instance second)
Calculates the distance between two instances |
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. |
protected IBk.NeighborList |
findNeighbors(Instance instance)
Build the list of nearest k neighbors to 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 |
protected void |
init()
Initialise scheme variables. |
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. |
protected double[] |
makeDistribution(IBk.NeighborList neighborlist)
Turn the list of nearest neighbors into a probability distribution |
java.lang.String |
meanSquaredTipText()
Returns the tip text for this property |
java.lang.String |
noNormalizationTipText()
Returns the tip text for this property |
protected double |
norm(double x,
int i)
Normalizes a given value of a numeric attribute. |
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 |
protected void |
updateMinMax(Instance instance)
Updates the minimum and maximum values for all the attributes based on a new instance. |
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 |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
protected Instances m_Train
protected int m_NumClasses
protected int m_ClassType
protected double[] m_Min
protected double[] m_Max
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 boolean m_DontNormalize
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 double m_NumAttributesUsed
Constructor Detail |
public IBk(int k)
k
- the number of nearest neighbors to use for predictionpublic IBk()
Method Detail |
public java.lang.String globalInfo()
public 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)
public 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 int getNumTraining()
public double getAttributeMin(int index) throws java.lang.Exception
java.lang.Exception
public double getAttributeMax(int index) throws java.lang.Exception
java.lang.Exception
public java.lang.String noNormalizationTipText()
public boolean getNoNormalization()
public void setNoNormalization(boolean v)
v
- Value to assign to DontNormalize.public void buildClassifier(Instances instances) throws java.lang.Exception
buildClassifier
in class Classifier
instances
- set of instances serving as training data
java.lang.Exception
- if the classifier has not been generated successfullypublic void updateClassifier(Instance instance) throws java.lang.Exception
updateClassifier
in interface UpdateableClassifier
instance
- the instance to add
java.lang.Exception
- if instance could not be incorporated
successfullypublic double[] distributionForInstance(Instance instance) throws java.lang.Exception
distributionForInstance
in class Classifier
instance
- the instance to be classified
java.lang.Exception
- if an error occurred during the predictionpublic java.util.Enumeration listOptions()
listOptions
in interface OptionHandler
listOptions
in class Classifier
public void setOptions(java.lang.String[] options) throws java.lang.Exception
-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)
setOptions
in interface OptionHandler
setOptions
in class Classifier
options
- the list of options as an array of strings
java.lang.Exception
- if an option is not supportedpublic java.lang.String[] getOptions()
getOptions
in interface OptionHandler
getOptions
in class Classifier
public java.lang.String toString()
protected void init()
protected double distance(Instance first, Instance second)
protected double difference(int index, double val1, double val2)
protected double norm(double x, int i)
x
- the value to be normalizedi
- the attribute's indexprotected void updateMinMax(Instance instance)
instance
- the new instanceprotected IBk.NeighborList findNeighbors(Instance instance)
instance
- the instance to search for neighbours of
protected double[] makeDistribution(IBk.NeighborList neighborlist) throws java.lang.Exception
neighborlist
- the list of nearest neighboring instances
java.lang.Exception
protected void crossValidate()
public static void main(java.lang.String[] argv)
argv
- should contain command line options (see setOptions)
|
|||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |