Class FastKNN
source code
A class which implements the KNN learning algorithm with sped up query
times.
This class follows the conventions of other classes in the qstklearn
module, with a constructor that initializes basic parameters and
bookkeeping variables, an 'addEvidence' method for adding labeled
training data individually or as a batch, and a 'query' method that
returns an estimated class for an unlabled point. Training and testing
data are in the form of numpy arrays, and classes are discrete.
In order to speed up query times, this class keeps a number of lists
which sort the training data by distance to 'anchor' points. The lists
aren't sorted until the first call to the 'query' method, after which,
the lists are kept in sorted order. Initial sort is done using pythons
'sort' (samplesort), and sorted insertions with 'insort' from the bisect
module.
|
|
__init__(self,
num_anchors,
k)
Creates a new FastKNN object that will use the given number of
anchors. |
source code
|
|
|
|
|
|
|
|
|
|
query(self,
point,
k=None,
method='mode',
slow=False,
dumdumcheck=False)
Returns class value for an unlabled point by examining its k nearest
neighbors. |
source code
|
|
resetAnchors(self,
selection_type='random')
| source code
|
Picks a new set of anchors. The anchor lists will be re-sorted upon
the next call to 'query'.
selection_type - the method to use when selecting new anchor points.
'random' performs a random permutation of the training points and picks
the first 'num_anchors' as new anchors.
|
|
Adds to the set of training data. If the anchor lists were sorted
before the call to this method, the new data will be inserted into the
anchor lists using 'bisect.insort'
data - a numpy array, either a single point (1D) or a set of points
(2D)
label - the label for data. A single value, or a list of values in the
same order as the points in data.
|
query(self,
point,
k=None,
method='mode',
slow=False,
dumdumcheck=False)
| source code
|
Returns class value for an unlabled point by examining its k nearest
neighbors. 'method' determines how the class of the unlabled point is
determined.
|