# KNN-regression

Posted 2017.03.01 19:47

Super simple K-NN regression algorithm in MATLAB

main.m

range = 1;

xall = [1 3 5 7 9 11 13 20 21 23 24 25 30]'*range;

yall = [0 3 2 6 1 0  2  4  5  6  10  5 0]';

xs   = linspace(0, 30*range, 100)';

k    = 5;

func = @(X)(get_knnregression(X, xall, yall, k));

outs = func(xs);

figure(1); clf; hold on;

plot(xall, yall, 'ko', 'MarkerSize', 15, 'LineWidth', 2);

plot(xs, outs, 'b-', 'LineWidth', 2);

title(sprintf('KNN REGRESSION [k:%d]', k), 'FontSize', 14);

get_knnregression.m

function outs = get_knnregression(xs, xall, yall, k)

% KNN REGRESSION

nx = size(xs, 1);

dy = size(yall, 2);

outs = zeros(nx, dy);

for xidx = 1:nx % FOR ALL TRAINING DATA

x = xs(xidx, :);

% FIRST FIND K CLOSEST INDICES

[n, d]  = size(xall);

xdiffs  = xall - repmat(x, n, 1);

dsq     = zeros(n, 1);

for i = 1:d

dsq = dsq + xdiffs(:, i).^2;

end

ds = sqrt(dsq);

[~, indices] = sort(ds, 'ascend');

selidx  = indices(1:k);

% COMPUTE WEIGHTS

weights = ds(selidx);

weights = exp(- 5 * weights / max(weights));

weights = weights / sum(weights);

% THEN, SIMPLY AVERAGE THEM

outs(xidx, :) = weights'*yall(selidx, :);

end

#### 'Enginius > Matlab' 카테고리의 다른 글

 Handling 'bvh' format from OptiTrack in MATLAB  (0) 2018.06.30 2017.07.14 2017.03.01 2016.11.21 2016.02.23 2016.01.15