https://github.com/markuman/fastknn
Loop-Free KNN algorithm for GNU Octave and Matlab
https://github.com/markuman/fastknn
gnu-octave knn matlab minkowski
Last synced: about 1 year ago
JSON representation
Loop-Free KNN algorithm for GNU Octave and Matlab
- Host: GitHub
- URL: https://github.com/markuman/fastknn
- Owner: markuman
- License: mit
- Created: 2016-01-26T20:13:31.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2019-01-10T18:21:54.000Z (over 7 years ago)
- Last Synced: 2025-03-28T23:11:20.471Z (about 1 year ago)
- Topics: gnu-octave, knn, matlab, minkowski
- Language: MATLAB
- Size: 2.93 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fastKNN
Loop-Free KNN algorithm for GNU Octave and Matlab
* Origin: https://git.osuv.de/m/fastknn
* Pull Mirror: https://gitlab.com/markuman/fastknn
* Push Mirror: https://github.com/markuman/fastknn
# Dataset
Dataset taken from http://www.jiaaro.com/KNN-for-humans/
dataset = [
%weight, color, # seeds, type
303, 3, 1, 1;
370, 1, 2, 2;
298, 3, 1, 1;
277, 3, 1, 1;
377, 4, 2, 2;
299, 3, 1, 1;
382, 1, 2, 2;
374, 4, 6, 2;
303, 4, 1, 1;
309, 3, 1, 1;
359, 1, 2, 2;
366, 1, 4, 2;
311, 3, 1, 1;
302, 3, 1, 1;
373, 4, 4, 2;
305, 3, 1, 1;
371, 3, 6, 2;
];
% lousy mapping
fruit = {'Banana', 'Apple'};
color.index = {'red', 'orange', 'yellow', 'green', 'blue', 'purple'};
color.red = 1;
color.orange = 2;
color.yellow = 3;
color.green = 4;
color.blue = 5;
color.purple = 6;
UF1 = [301, color.green, 1];
UF2 = [346, color.yellow, 4];
UF3 = [290, color.red, 2];
Play with the Dataset.
normalize = @(x) (x - min(x)) / max((x - min(x))); % reduce by smallest value
# usage
`[classified, k, dist, idx] = fastKNN(trained, unknown, k, distance)`
* `classified` - result of KNN
* `k`
* nargin: the defined `k`
* nargout: information which `k` was taken _(...when `k` was automatically determined!)_
* `dist` - sorted calculated distances
* `idx` - Index to map sorted distances `dist` to input dataset `trained`
* `distance` - default = 2
* `distance == 2`: **Minkowski becomes equal Euclidean**
* `distance == 1`: **Minkowski becomes equal city block metric**
* `else`: **Minkowski distance** - https://en.wikipedia.org/wiki/Minkowski_distance
## default with Euclidean distance and automagical determine of `k`
>> fastKNN(dataset, UF1)
ans =
1
>> fruit(ans)
ans =
'Banana'