bijnum
Class BIJknn

java.lang.Object
  |
  +--bijnum.BIJknn

public class BIJknn
extends java.lang.Object

k Nearest Neighborhood (brute-force) implementation. You can create a kNN object, add datasets to it, and classify vectors. Copyright (c) 1999-2003, Michael Abramoff. All rights reserved.


Field Summary
 float[] classset
          The corresponding classification for each vector in the dataset, a 1xM vector.
 float[][] dataset
          The dataset, a MxN matrix of N-dimensional vectors.
static int inserts
           
protected  float[] kDistances
          Some variables that I do not want to allocate for every element.
protected  int[] kIndices
           
 int n
          The number of different classes in classset.
 
Constructor Summary
BIJknn(float[][] dataset, float[] classset, int n)
           
BIJknn(int n)
           
 
Method Summary
 void add(float[][] extradataset, float[] extraclassset)
          Add extra data and classification to this knn.
 float[] classify(float[][] unknown, int k)
          Find the classification of M (N dimensional) vectors unknown in the dataset.
protected static float distance(float[] a, float[] b)
          Return the distance according to some Minkowski metric.
 void prune(int k)
          Cleans the dataset using Wilson's pruning algorithm.
 void search(int[] kIndices, float[] kDistances, float[] v, int k)
          Find the k-nearest neighbors of vector v (N dimensional) in dataset and put indices in kIndices, distances in kDistances.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

dataset

public float[][] dataset
The dataset, a MxN matrix of N-dimensional vectors.

classset

public float[] classset
The corresponding classification for each vector in the dataset, a 1xM vector.

n

public int n
The number of different classes in classset.

inserts

public static int inserts

kDistances

protected float[] kDistances
Some variables that I do not want to allocate for every element.

kIndices

protected int[] kIndices
Constructor Detail

BIJknn

public BIJknn(int n)

BIJknn

public BIJknn(float[][] dataset,
              float[] classset,
              int n)
Method Detail

add

public void add(float[][] extradataset,
                float[] extraclassset)
         throws java.lang.Exception
Add extra data and classification to this knn.
Parameters:
extradataset - an extra dataset
extraclassset - an extra class set.

classify

public float[] classify(float[][] unknown,
                        int k)
Find the classification of M (N dimensional) vectors unknown in the dataset.
Parameters:
unknown - a MxN matrix of M vectors to be classified in the dataset.
k - the number of neighbors to use for classification
Returns:
classification a vector that will contain the nearest neighbor classificiations for the corresponding vectors in unknown.

search

public void search(int[] kIndices,
                   float[] kDistances,
                   float[] v,
                   int k)
Find the k-nearest neighbors of vector v (N dimensional) in dataset and put indices in kIndices, distances in kDistances.
Parameters:
kIndices - a int[] of length k that will contain the indices to the k closest neighbors of v
kDistances - a float[] of length k that will contain the distances to the k closest neighbors of v.
v - the vector to classify
k - the number of neighbors to use for classification

prune

public void prune(int k)
Cleans the dataset using Wilson's pruning algorithm. Classifies each point in the dataset (k=3) and removes it if its class is not the same as the classification by its k neighbors.

distance

protected static float distance(float[] a,
                                float[] b)
Return the distance according to some Minkowski metric. This implements Euclidean distance. To save time, the POWER(1/k) is not performed.
Parameters:
a - a float[] vector containing a point
b - a float[] vector containing another point.
Returns:
the Minkowski distance between a and b.