public class KNN
extends java.lang.Object
The KNN algorithm tries to find the K nearest instances in the training data, selecting the most present class. Euclidean (L2), Manhattan (L1) and HVDM distances can be used as distance function by the classifier.
Modifier and Type | Field and Description |
---|---|
protected int |
inputAtt
Number of input attributes
|
protected Attribute[] |
inputs
Inputs attributes
|
protected int |
nClasses
Number of classes.
|
protected int[] |
nInstances
Number of instances of each classes.
|
protected boolean[] |
nulls
Missing values of a instance
|
protected Attribute |
output
Output attribute
|
protected int[] |
prediction
Test predicted classes.
|
protected int[] |
realClass
Real test classes.
|
protected java.lang.String |
relation
Relation string.
|
protected long |
seed
Random seed.
|
protected Instance |
temp
Temporal instance.
|
protected double[][] |
testData
Test input data.
|
protected int[] |
testOutput
Test output data.
|
protected double[][] |
trainData
Training input data.
|
protected int[] |
trainOutput
Training output data.
|
protected int[] |
trainPrediction
Training predicted classes.
|
protected int[] |
trainRealClass
Real training classes.
|
Constructor and Description |
---|
KNN(Instance[] trainI,
Instance[] testI)
The main method of the class
|
Modifier and Type | Method and Description |
---|---|
protected double |
euclideanDistance(double[] instance1,
double[] instance2)
Calculates the Euclidean distance between two instances
|
protected int |
evaluate(double[] example)
Evaluates a instance to predict its class.
|
void |
execute()
Executes the classification of train and test data sets
|
void |
executeReference()
Executes the classification of reference and test data sets
|
int[] |
getPredictions()
Returns the predicted classes for each test instance.
|
protected double |
manhattanDistance(double[] instance1,
double[] instance2)
Calculates the Manhattan distance between two instances
|
protected void |
normalizeTest()
This function builds the data matrix for test data and normalizes inputs values
|
protected void |
normalizeTrain()
This function builds the data matrix for training data and normalizes inputs values
|
static java.lang.String |
printInstance(int[] instance)
Generates a string with the contents of the instance
|
protected Instance temp
protected int inputAtt
protected Attribute[] inputs
protected Attribute output
protected boolean[] nulls
protected double[][] trainData
protected int[] trainOutput
protected double[][] testData
protected int[] testOutput
protected java.lang.String relation
protected int nClasses
protected int[] nInstances
protected long seed
protected int[] realClass
protected int[] prediction
protected int[] trainRealClass
protected int[] trainPrediction
public int[] getPredictions()
public void execute()
protected int evaluate(double[] example)
example
- Instance evaluatedprotected void normalizeTrain()
protected void normalizeTest()
public void executeReference()
protected double euclideanDistance(double[] instance1, double[] instance2)
instance1
- First instanceinstance2
- Second instanceprotected double manhattanDistance(double[] instance1, double[] instance2)
instance1
- First instanceinstance2
- Second instancepublic static java.lang.String printInstance(int[] instance)
instance
- a Instance to print.