/* File name: knn.h Created by: Ljubomir Buturovic Created: 12/31/2002 Purpose: declarations for k-NN classification methods. */ /* Copyright 2004 Ljubomir J. Buturovic Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #define KNN_METHOD_VOTING 1 #define KNN_METHOD_VOLUMETRIC 2 struct knn_options { int dist; int k; /* number of neighbors */ int method; /* voting or volumetric k-NN */ }; /* Calculate Mahalanobis distance between 'd'-dimensional vectors 'x' and 'y' using matrix 'ax' in IMSL Symmetric Storage Mode. 'mxv' and 'mxe' are work vectors of length 'd'. Must be preallocated. */ float mahalanobis(float *x, float *y, int d, float *ax, float *mxv, float *mxe); /* Classify 'vector' from 'category' using k-NN method, 'dset' and distance measure 'dist'. Return the assigned class. Break ties pseudo-randomly. NOTE: `category' is only used for logging, not computation. In case of failure, return -1 and set 'errc'. */ int knn(float *vector, int category, struct dataset *dset, int k, int dist, int *errc, FILE *fdbg); /* Classify 'vector' using bagging k-NN method with 'nmodels', 'dset' and distance measure 'dist'. Return the assigned class. Ties are resolved pseudo-randomly. */ int knn_bagging(float *vector, int tcl, struct dataset *dset, int nmodels, int bag_size, int k, int dist, int *errc, FILE *fdbg);