MATLAB计算K近邻之使用VLFeat求K近邻的算法案例

编程入门 行业动态 更新时间:2024-10-27 13:24:56

MATLAB计算K<a href=https://www.elefans.com/category/jswz/34/1765532.html style=近邻之使用VLFeat求K近邻的算法案例"/>

MATLAB计算K近邻之使用VLFeat求K近邻的算法案例

MATLAB计算K近邻之升级算法
K近邻法分类待测样本点,模式识别实验内容之一,用MATLAB生成随机样本点作为样本集,用样本集将考试集分类。具体原理自己参悟,直接上源码解读。
1.前提安装专用学习库
.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等多种算法。其下载地址为.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));endDistance(i) = sqrt(tempsum);
endfor j = 1:datasize(1)index = k;while index>0 && Result(index)==0index = index-1;end       if index == 0Result(1) = j;elseif Distance(j)<Distance(Result(index))while index>0 && Distance(j)<Distance(Result(index))temp = Result(index);Result(index) = j;if index<kResult(index+1) = temp;endindex = index-1;endelseif index<kResult(index+1) = j;endendend            
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 onfor i = 1:100has = 0;for j = 1:10if index(j)==ihas = 1;break;endendif has==1scatter(X(1,i),X(2,i),'o')hold onelsescatter(X(1,i),X(2,i),'.')hold onendgrid on    
end

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

参考链接博文:

更多推荐

MATLAB计算K近邻之使用VLFeat求K近邻的算法案例

本文发布于:2024-02-12 06:18:49,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1686582.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:近邻   算法   案例   MATLAB   VLFeat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!