MATLAB计算K近邻之升级算法
K近邻法分类待测样本点,模式识别实验内容之一,用MATLAB生成随机样本点作为样本集,用样本集将考试集分类。具体原理自己参悟,直接上源码解读。
1.前提安装专用学习库
https://www.vlfeat.org/download.html
按照提示进行操作
打开位置后运行
%%%%%%%%%%%%%%%这个是里面的m文件,直接注意文件位置,运行,路径全英文

function path = vl_setup(varargin)
% VL_SETUP Add VLFeat Toolbox to the path
% PATH = VL_SETUP() adds the VLFeat Toolbox to MATLAB path and
% returns the path PATH to the VLFeat package.
%
% VL_SETUP(‘NOPREFIX’) adds aliases to each function that do not
% contain the VL_ prefix. For example, with this option it is
% possible to use SIFT() instead of VL_SIFT().
%
% VL_SETUP(‘TEST’) or VL_SETUP(‘XTEST’) adds VLFeat unit test
% function suite. See also VL_TEST().
%
% VL_SETUP(‘QUIET’) does not print the greeting message.
%
% See also: VL_ROOT(), VL_HELP().

% Authors: Andrea Vedaldi and Brian Fulkerson

% Copyright © 2007-12 Andrea Vedaldi and Brian Fulkerson.
% All rights reserved.
%
% This file is part of the VLFeat library and is made available under
% the terms of the BSD license (see the COPYING file).

noprefix = false ;
quiet = true ;
xtest = false ;
demo = false ;

for ai=1:length(varargin)
opt = varargin{ai} ;
switch lower(opt)
case {‘noprefix’, ‘usingvl’}
noprefix = true ;
case {‘test’, ‘xtest’}
xtest = true ;
case {‘demo’}
demo = true ;
case {‘quiet’}
quiet = true ;
case {‘verbose’}
quiet = false ;
otherwise
error(‘Unknown option ‘’%s’’.’, opt) ;
end
end

% Do not use vl_root() to avoid conflicts with other VLFeat
% installations.

[a,b,c] = fileparts(mfilename(‘fullpath’)) ;
[a,b,c] = fileparts(a) ;
root = a ;

addpath(fullfile(root,‘toolbox’ )) ;
addpath(fullfile(root,‘toolbox’,‘aib’ )) ;
addpath(fullfile(root,‘toolbox’,‘geometry’ )) ;
addpath(fullfile(root,‘toolbox’,‘imop’ )) ;
addpath(fullfile(root,‘toolbox’,‘kmeans’ )) ;
addpath(fullfile(root,‘toolbox’,‘misc’ )) ;
addpath(fullfile(root,‘toolbox’,‘mser’ )) ;
addpath(fullfile(root,‘toolbox’,‘plotop’ )) ;
addpath(fullfile(root,‘toolbox’,‘quickshift’)) ;
addpath(fullfile(root,‘toolbox’,‘sift’ )) ;
addpath(fullfile(root,‘toolbox’,‘special’ )) ;
addpath(fullfile(root,‘toolbox’,‘slic’ )) ;
addpath(fullfile(root,‘toolbox’,‘gmm’ )) ;
addpath(fullfile(root,‘toolbox’,‘vlad’ )) ;
addpath(fullfile(root,‘toolbox’,‘fisher’ )) ;

if vl_isoctave()
addpath(genpath(fullfile(root,‘toolbox’,‘mex’,‘octave’))) ;
warning(‘off’, ‘Octave:possible-matlab-short-circuit-operator’) ;
pkg load image ;
else
bindir = mexext ;
if strcmp(bindir, ‘dll’), bindir = ‘mexw32’ ; end
addpath(fullfile(root,‘toolbox’,‘mex’,bindir)) ;
end

if noprefix
addpath(fullfile(root,‘toolbox’,‘noprefix’)) ;
end

if xtest
addpath(fullfile(root,‘toolbox’,‘xtest’)) ;
end

if demo
addpath(fullfile(root,‘toolbox’,‘demo’)) ;
end

if ~quiet
if exist(‘vl_version’) == 3
fprintf(‘VLFeat %s ready.\n’, vl_version) ;
else
warning(‘VLFeat does not seem to be installed correctly. Make sure that the MEX files are compiled.’) ;
end
end

if nargout == 0
clear path ;
end

######以上文件在下载后压缩包自带的,仅供提示使用
一些在MATLAB中计算K近邻的工具箱,VLFeat就是一个例子,它实现了kmeans,KDTree等多种算法。其下载地址为http://www.vlfeat.org/download.html ,下载完成之后解压,在MATLAB命令行中运行其中的toolbox文件夹下的vl_setup.m即可完成该工具箱的配置,在MATLAB命令行中输入vl_version verbose,具体如下:版本检验
—————————————————————————————————————

2.编写文件KNN,可执行mat保存,等待后面程序调用;不过也不一定都会用得到

function [ Result ] = KNN( v,Data,k )
% 对一个向量量v计算它在Data中的k近邻
% v是行向量,Data是数据集矩阵,k为近邻数

datasize = size(Data);
Result = zeros(1,k);%结果向量
Distance = zeros(datasize(1),1);%距离矩阵

%距离矩阵计算
for i = 1:datasize(1)
    tempsum = 0;
    for j = 1:datasize(2)
        tempsum = tempsum + (Data(i,j)-v(j))*(Data(i,j)-v(j));
    end
    Distance(i) = sqrt(tempsum);
end

for j = 1:datasize(1)
        index = k;
        while index>0 && Result(index)==0
            index = index-1;
        end       
        if index == 0
            Result(1) = j;
        else
            if Distance(j)<Distance(Result(index))
                while index>0 && Distance(j)<Distance(Result(index))
                    temp = Result(index);
                    Result(index) = j;
                    if index<k
                        Result(index+1) = temp;
                    end
                    index = index-1;
                end
            else
                if index<k
                    Result(index+1) = j;
                end
            end
        end            
end
end

—————————————————————————————————————————————

3.MATLAB计算K近邻之使用VLFeat求K近邻的算法案例,保存运行,最好在同一个文件路径下。

clc,clear
X = rand(2,200);%生成二百个二维列向量
kdtree = vl_kdtreebuild(X);%构建kd树

Q = rand(2,1);
[index,distance] = vl_kdtreequery(kdtree, X, Q);%返回X中与Q最近的点

[index, distance] = vl_kdtreequery(kdtree, X, Q, 'NumNeighbors', 10) ;%返回X中Q的K的近邻

scatter(Q(1),Q(2),'p')
hold on

for i = 1:100
    has = 0;
    for j = 1:10
        if index(j)==i
            has = 1;
            break;
        end
    end
    
    if has==1
        scatter(X(1,i),X(2,i),'o')
        hold on
    else
        scatter(X(1,i),X(2,i),'.')
        hold on
    end
     grid on    
end

———————————————————————————————————————
结果如图
参考链接博文:https://blog.csdn.net/john_bian/article/details/79424432

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐