支持向量机的matlab代码

支持向量机的matlab代码,第1张

如果是7.0以上版本

>>edit svmtrain

>>edit svmclassify

>>edit svmpredict

function [svm_struct, svIndex] = svmtrain(training, groupnames, varargin)

%SVMTRAIN trains a support vector machine classifier

%

% SVMStruct = SVMTRAIN(TRAINING,GROUP) trains a support vector machine

% classifier using data TRAINING taken from two groups given by GROUP.

% SVMStruct contains information about the trained classifier that is

% used by SVMCLASSIFY for classification. GROUP is a column vector of

% values of the same length as TRAINING that defines two groups. Each

% element of GROUP specifies the group the corresponding row of TRAINING

% belongs to. GROUP can be a numeric vector, a string array, or a cell

% array of strings. SVMTRAIN treats NaNs or empty strings in GROUP as

% missing values and ignores the corresponding rows of TRAINING.

%

% SVMTRAIN(...,'KERNEL_FUNCTION',KFUN) allows you to specify the kernel

% function KFUN used to map the training data into kernel space. The

% default kernel function is the dot product. KFUN can be one of the

% following strings or a function handle:

%

% 'linear' Linear kernel or dot product

% 'quadratic' Quadratic kernel

% 'polynomial' Polynomial kernel (default order 3)

% 'rbf' Gaussian Radial Basis Function kernel

% 'mlp' Multilayer Perceptron kernel (default scale 1)

% function A kernel function specified using @,

% for example @KFUN, or an anonymous function

%

% A kernel function must be of the form

%

% function K = KFUN(U, V)

%

% The returned value, K, is a matrix of size M-by-N, where U and V have M

% and N rows respectively. If KFUN is parameterized, you can use

% anonymous functions to capture the problem-dependent parameters. For

% example, suppose that your kernel function is

%

% function k = kfun(u,v,p1,p2)

% k = tanh(p1*(u*v')+p2)

%

% You can set values for p1 and p2 and then use an anonymous function:

% @(u,v) kfun(u,v,p1,p2).

%

% SVMTRAIN(...,'POLYORDER',ORDER) allows you to specify the order of a

% polynomial kernel. The default order is 3.

%

% SVMTRAIN(...,'MLP_PARAMS',[P1 P2]) allows you to specify the

% parameters of the Multilayer Perceptron (mlp) kernel. The mlp kernel

% requires two parameters, P1 and P2, where K = tanh(P1*U*V' + P2) and P1

% >0 and P2 <0. Default values are P1 = 1 and P2 = -1.

%

% SVMTRAIN(...,'METHOD',METHOD) allows you to specify the method used

% to find the separating hyperplane. Options are

%

% 'QP' Use quadratic programming (requires the Optimization Toolbox)

% 'LS' Use least-squares method

%

% If you have the Optimization Toolbox, then the QP method is the default

% method. If not, the only available method is LS.

%

% SVMTRAIN(...,'QUADPROG_OPTS',OPTIONS) allows you to pass an OPTIONS

% structure created using OPTIMSET to the QUADPROG function when using

% the 'QP' method. See help optimset for more details.

%

% SVMTRAIN(...,'SHOWPLOT',true), when used with two-dimensional data,

% creates a plot of the grouped data and plots the separating line for

% the classifier.

%

% Example:

% % Load the data and select features for classification

% load fisheriris

% data = [meas(:,1), meas(:,2)]

% % Extract the Setosa class

% groups = ismember(species,'setosa')

% % Randomly select training and test sets

% [train, test] = crossvalind('holdOut',groups)

% cp = classperf(groups)

% % Use a linear support vector machine classifier

% svmStruct = svmtrain(data(train,:),groups(train),'showplot',true)

% classes = svmclassify(svmStruct,data(test,:),'showplot',true)

% % See how well the classifier performed

% classperf(cp,classes,test)

% cp.CorrectRate

%

% See also CLASSIFY, KNNCLASSIFY, QUADPROG, SVMCLASSIFY.

% Copyright 2004 The MathWorks, Inc.

% $Revision: 1.1.12.1 $ $Date: 2004/12/24 20:43:35 $

% References:

% [1] Kecman, V, Learning and Soft Computing,

% MIT Press, Cambridge, MA. 2001.

% [2] Suykens, J.A.K., Van Gestel, T., De Brabanter, J., De Moor, B.,

% Vandewalle, J., Least Squares Support Vector Machines,

% World Scientific, Singapore, 2002.

% [3] Scholkopf, B., Smola, A.J., Learning with Kernels,

% MIT Press, Cambridge, MA. 2002.

%

% SVMTRAIN(...,'KFUNARGS',ARGS) allows you to pass additional

% arguments to kernel functions.

% set defaults

plotflag = false

qp_opts = []

kfunargs = {}

setPoly = falseusePoly = false

setMLP = falseuseMLP = false

if ~isempty(which('quadprog'))

useQuadprog = true

else

useQuadprog = false

end

% set default kernel function

kfun = @linear_kernel

% check inputs

if nargin <2

error(nargchk(2,Inf,nargin))

end

numoptargs = nargin -2

optargs = varargin

% grp2idx sorts a numeric grouping var ascending, and a string grouping

% var by order of first occurrence

[g,groupString] = grp2idx(groupnames)

% check group is a vector -- though char input is special...

if ~isvector(groupnames) &&~ischar(groupnames)

error('Bioinfo:svmtrain:GroupNotVector',...

'Group must be a vector.')

end

% make sure that the data is correctly oriented.

if size(groupnames,1) == 1

groupnames = groupnames'

end

% make sure data is the right size

n = length(groupnames)

if size(training,1) ~= n

if size(training,2) == n

training = training'

else

error('Bioinfo:svmtrain:DataGroupSizeMismatch',...

'GROUP and TRAINING must have the same number of rows.')

end

end

% NaNs are treated as unknown classes and are removed from the training

% data

nans = find(isnan(g))

if length(nans) >0

training(nans,:) = []

g(nans) = []

end

ngroups = length(groupString)

if ngroups >2

error('Bioinfo:svmtrain:TooManyGroups',...

'SVMTRAIN only supports classification into two groups.\nGROUP contains %d different groups.',ngroups)

end

% convert to 1, -1.

g = 1 - (2* (g-1))

% handle optional arguments

if numoptargs >= 1

if rem(numoptargs,2)== 1

error('Bioinfo:svmtrain:IncorrectNumberOfArguments',...

'Incorrect number of arguments to %s.',mfilename)

end

okargs = {'kernel_function','method','showplot','kfunargs','quadprog_opts','polyorder','mlp_params'}

for j=1:2:numoptargs

pname = optargs{j}

pval = optargs{j+1}

k = strmatch(lower(pname), okargs)%#ok

if isempty(k)

error('Bioinfo:svmtrain:UnknownParameterName',...

'Unknown parameter name: %s.',pname)

elseif length(k)>1

error('Bioinfo:svmtrain:AmbiguousParameterName',...

'Ambiguous parameter name: %s.',pname)

else

switch(k)

case 1 % kernel_function

if ischar(pval)

okfuns = {'linear','quadratic',...

'radial','rbf','polynomial','mlp'}

funNum = strmatch(lower(pval), okfuns)%#ok

if isempty(funNum)

funNum = 0

end

switch funNum %maybe make this less strict in the future

case 1

kfun = @linear_kernel

case 2

kfun = @quadratic_kernel

case {3,4}

kfun = @rbf_kernel

case 5

kfun = @poly_kernel

usePoly = true

case 6

kfun = @mlp_kernel

useMLP = true

otherwise

error('Bioinfo:svmtrain:UnknownKernelFunction',...

'Unknown Kernel Function %s.',kfun)

end

elseif isa (pval, 'function_handle')

kfun = pval

else

error('Bioinfo:svmtrain:BadKernelFunction',...

'The kernel function input does not appear to be a function handle\nor valid function name.')

end

case 2 % method

if strncmpi(pval,'qp',2)

useQuadprog = true

if isempty(which('quadprog'))

warning('Bioinfo:svmtrain:NoOptim',...

'The Optimization Toolbox is required to use the quadratic programming method.')

useQuadprog = false

end

elseif strncmpi(pval,'ls',2)

useQuadprog = false

else

error('Bioinfo:svmtrain:UnknownMethod',...

'Unknown method option %s. Valid methods are ''QP'' and ''LS''',pval)

end

case 3 % display

if pval ~= 0

if size(training,2) == 2

plotflag = true

else

warning('Bioinfo:svmtrain:OnlyPlot2D',...

'The display option can only plot 2D training data.')

end

end

case 4 % kfunargs

if iscell(pval)

kfunargs = pval

else

kfunargs = {pval}

end

case 5 % quadprog_opts

if isstruct(pval)

qp_opts = pval

elseif iscell(pval)

qp_opts = optimset(pval{:})

else

error('Bioinfo:svmtrain:BadQuadprogOpts',...

'QUADPROG_OPTS must be an opts structure.')

end

case 6 % polyorder

if ~isscalar(pval) || ~isnumeric(pval)

error('Bioinfo:svmtrain:BadPolyOrder',...

'POLYORDER must be a scalar value.')

end

if pval ~=floor(pval) || pval <1

error('Bioinfo:svmtrain:PolyOrderNotInt',...

'The order of the polynomial kernel must be a positive integer.')

end

kfunargs = {pval}

setPoly = true

case 7 % mlpparams

if numel(pval)~=2

error('Bioinfo:svmtrain:BadMLPParams',...

'MLP_PARAMS must be a two element array.')

end

if ~isscalar(pval(1)) || ~isscalar(pval(2))

error('Bioinfo:svmtrain:MLPParamsNotScalar',...

'The parameters of the multi-layer perceptron kernel must be scalar.')

end

kfunargs = {pval(1),pval(2)}

setMLP = true

end

end

end

end

if setPoly &&~usePoly

warning('Bioinfo:svmtrain:PolyOrderNotPolyKernel',...

'You specified a polynomial order but not a polynomial kernel')

end

if setMLP &&~useMLP

warning('Bioinfo:svmtrain:MLPParamNotMLPKernel',...

'You specified MLP parameters but not an MLP kernel')

end

% plot the data if requested

if plotflag

[hAxis,hLines] = svmplotdata(training,g)

legend(hLines,cellstr(groupString))

end

% calculate kernel function

try

kx = feval(kfun,training,training,kfunargs{:})

% ensure function is symmetric

kx = (kx+kx')/2

catch

error('Bioinfo:svmtrain:UnknownKernelFunction',...

'Error calculating the kernel function:\n%s\n', lasterr)

end

% create Hessian

% add small constant eye to force stability

H =((g*g').*kx) + sqrt(eps(class(training)))*eye(n)

if useQuadprog

% The large scale solver cannot handle this type of problem, so turn it

% off.

qp_opts = optimset(qp_opts,'LargeScale','Off')

% X=QUADPROG(H,f,A,b,Aeq,beq,LB,UB,X0,opts)

alpha = quadprog(H,-ones(n,1),[],[],...

g',0,zeros(n,1),inf *ones(n,1),zeros(n,1),qp_opts)

% The support vectors are the non-zeros of alpha

svIndex = find(alpha >sqrt(eps))

sv = training(svIndex,:)

% calculate the parameters of the separating line from the support

% vectors.

alphaHat = g(svIndex).*alpha(svIndex)

% Calculate the bias by applying the indicator function to the support

% vector with largest alpha.

[maxAlpha,maxPos] = max(alpha)%#ok

bias = g(maxPos) - sum(alphaHat.*kx(svIndex,maxPos))

% an alternative method is to average the values over all support vectors

% bias = mean(g(sv)' - sum(alphaHat(:,ones(1,numSVs)).*kx(sv,sv)))

% An alternative way to calculate support vectors is to look for zeros of

% the Lagrangians (fifth output from QUADPROG).

%

% [alpha,fval,output,exitflag,t] = quadprog(H,-ones(n,1),[],[],...

% g',0,zeros(n,1),inf *ones(n,1),zeros(n,1),opts)

%

% sv = t.lower <sqrt(eps) &t.upper <sqrt(eps)

else % Least-Squares

% now build up compound matrix for solver

A = [0 g'g,H]

b = [0ones(size(g))]

x = A\b

% calculate the parameters of the separating line from the support

% vectors.

sv = training

bias = x(1)

alphaHat = g.*x(2:end)

end

svm_struct.SupportVectors = sv

svm_struct.Alpha = alphaHat

svm_struct.Bias = bias

svm_struct.KernelFunction = kfun

svm_struct.KernelFunctionArgs = kfunargs

svm_struct.GroupNames = groupnames

svm_struct.FigureHandles = []

if plotflag

hSV = svmplotsvs(hAxis,svm_struct)

svm_struct.FigureHandles = {hAxis,hLines,hSV}

end

1.算术运算(arithmetic)主要指加减乘除、幂和舍入等运算

2.说明

Matlab有两种不同类型的算术运算:数组运算和矩阵运算

数组运算基于元素的运算,支持任意向量、矩阵和多维数组

矩阵运算遵循线性代数的规则

字符(.)区分矩阵运算和数组运算

数组运算和矩阵运算的加减法则相同,所以.+和.-是不必要的

运算数之一为标量时,乘法法则相同,所以.*是不必要的

运算数都为标量时,除法法则相同,所以./是不必要的

3.两种运算符

数组运算符汇总表

矩阵运算符汇总表

4.实例演示

%1_16

a=[1 23 4]

a+a %数组加法

a-a %数组减法

a.*a %数组乘法:对应元素乘积

a*a %矩阵乘法:线性代数

a./a %数组除法:对应元素相除

a/a %矩阵除法:得出单位矩阵对角线为1

a.^3 %数组幂:单个元素分别求幂

a^3 %矩阵幂:即a*a*a

a*a*a

a*5 %有标量作为计算数时,数组和矩阵乘法法则相同

a.*5 %因此可用矩阵乘法*替代数组乘法

a' %复共轭转置

a.' %转置:行数变列数

sin(a) %对a中每个元素求sin(调用函数大多执行数组运算)

[sin(1) sin(2)sin(3) sin(4)] %与上式相同

exp(a) %数组的指数运算:函数调用,同上述sin运算

expm(a) %矩阵的指数幂运算:函数后加m,matrix

(-1)^(1/2) %负数开方产生虚数单位

sqrt(-1) %同上

(5+2i)*(5-2i) %复数运算

1.介绍一些算术运算的常用函数

2.认识函数

加plus

减minus

乘times

除rpide

矩阵乘法mtimes

矩阵左除mlpide

求和sum

乘积prod

舍入round

向-inf舍入floor

向inf舍入ceil

向0舍入fix

模 *** 作mod

3.实例演示

%1_17

%freexyn

a=[1 23 4]

a+a %数组加法

plus(a,a) %数组加法(函数形式)

minus(a,a) %数组减法

times(a,a) %数组乘法:单个元素对应相乘

rpide(a,a)

mtimes(a,a) %矩阵乘法

mlpide(a,a) %矩阵除法

sum(a) %矩阵a求和:按列 *** 作

sum([1 2 3 4]) %行向量求和:所有元素加和

sum([1 2 3 4]') %转置为列向量:同为所有元素加和

prod(a) %数组a中元素求乘积:按列相乘;该函数若输入行、列向量则所有元素乘积

round(2.6) %舍入:就近舍入即四舍五入

floor(2.6) %向负无穷舍入

ceil(2.3) %向正无穷舍入

fix(2.6) %向0舍入

fix(-2.6)

mod(5,2) %余数(模 *** 作):被除数、除数

rem(5,2) %求余数(大多情况与mod相同)

mod(-5,2) %被除数为负数时,结果不一样

rem(-5,2)

1.逻辑型和逻辑运算

逻辑型(logical)数据是用数字1和0分别表示真(true)或假(false)的状态

2.认识函数

逻辑型logical

真true

假false

判断逻辑型islogical

全为真all

是否为真any

3.说明

Matlab中一些运算会返回逻辑值,表示一个条件是否被满足

可以使用这些逻辑值来索引数组或执行条件代码

逻辑运算符

逻辑真值表

4.实例演示

%1_18

a=[0 1 2 -1]

logical(a) %创建逻辑型:0为假返回逻辑0,其他非0数字都是真返回1

islogical(a)

islogical(b)

true &false %逻辑与

true | false %逻辑或

~true

1 &0

2 &0

~100

1 &&0 %短路的与:功能与“与”相同

1 || 0

% [1 0] &&[0 0] %短路的与、或只适用于标量运算

[1 0] &[0 0] %逻辑与可以用于数组

all([0 1 1]) %判断全为真

all([1 1 1])

any([0 1 1]) %判断任一为真,有一个真则结果为真,全为假结果为假

any([1 1 1])

any([0 0 0])

c=~a %a取非再赋值给c,则c为逻辑数组[1 0 0 0]

a(~mod(a,2)) %a中偶数余数取到0再取非则为真返回1,逻辑真被索引到返回相应偶数元素

true &[] %结果为空的逻辑数组,涉及到空矩阵的任何逻辑运算结果都是空逻辑数组

1.关系运算

关系运算(relational)使用“小于”,“大于”和“不等于”等运算符定量地比较运算数,比较的结果是一个逻辑数组,在关系为真的位置显示1

作者:freexyn 整理/注释:韩松岳

2.认识函数

查找元素find

3.关系运算符

关系运算符

4.实例演示

%1_19

a=[1 2 3]

a>1 %分别判断元素是否大于1,满足关系返回逻辑值1,否则0

a>a %返回0 0 0

a>=a %每个数都等于自己,返回三个1

[]>[] %关系运算中只要存在空矩阵,结果都返回空的逻辑数组

[]==[]

[]==2

%[]==[1 2 3] %报错,矩阵维度不一致无法比较(进行关系运算)

b=3+4i %复数关系运算

c=3+5i

c<=b %非等于关系的比较,只比较实部3

c==b %返回0。说明:复数关系运算中,==与~=会比较实部和虚部(上述例子中3和3、4和5),其他非等于关系的比较,只比较实部

a>1 &a<3 %将两个逻辑结果进行“与”运算,结果仍为逻辑值

tf=a>1 &a<3 %将上述结果赋值给变量tf

a(tf) %使用逻辑索引,提取矩阵中满足特定条件的元素

index=find(a>1 &a<3) %返回输入变量中条件为真的元素的线性索引

a(index) %使用线性索引,提取矩阵中满足特定条件的元素

a(a>1 &a<3)=10 %使用逻辑索引,修改满足特定条件的元素的值

1.Matlab中各类、多种运算符组合使用时的优先运算顺序

2.优先级顺序

可以任意组合使用算术运算符、关系运算符和逻辑运算符等形成的表达式进行运算,Matlab进行运算处理的顺序取决于每个运算符的优先级。在每个优先级中,运算符具有相同的优先级,并从左到右进行处理。Matlab运算符的优先级规则从最高到最低排序如下

括号 ()

转置 (.'), 幂(.^), 复共轭转置 ('), 矩阵的幂(^)

一元减的幂(.^-),一元加的幂(.^+),逻辑非的幂(.^~) 一元减的矩阵的幂(^-), 一元加的矩阵的幂(^+),逻辑非的矩阵的幂 (^~).

一元加(+),一元减(-),逻辑非(~)

乘法(.*),右除(./),左除(.),矩阵的乘法(*),矩阵的右除(/),矩阵的左除 ()

加法 (+), 减法(-)

冒号(:)

关系运算符(<),(), (>=), (==), (~=)

逻辑与(&)

逻辑或(|)

短路逻辑与(&&)

短路逻辑或 (||)

3.实例演示

%1_20

1>=1+1 %四则运算高于关系运算符

(1>=1)+1

1:2+3 %四则运算高于冒号运算符

(1:2)+3

1|1&0 %逻辑与高于逻辑或

(1|1)&0

1.基本运算中数组大小的兼容性(2016b)

2.说明

这里兼容性是指,两个大小不同的数组是否能够进行运算

两个完全相同大小的数组可以运算

其中之一是标量的两个大小不同的数组可以运算

一个是行向量,一个是列向量,可以运算

一个是矩阵,一个是具有相同行数的列向量,可以运算

一个是矩阵,一个是具有相同列数的行向量,可以运算

一个是矩阵,一个是具有相同行数和列数的三维数组,可以运算

这里的兼容性运算主要指数组的四则运算

3.实例演示

%1_21

a=[1 23 4]

b=[1 2]

c=[34]

a+a

a+2 %矩阵与标量运算:将标量扩展为前面矩阵的兼容性大小,再遵循数组四则运算

a.*2

a*2

b+c %先扩展为兼容性大小:b复制行、c复制列,再运算

a+b

a+c

a.*b

a.*c

% a*b %无法运算,不满足矩阵乘法

a*c %可以运算,但并非兼容性运算,而是满足矩阵乘法

d=cat(3,a,a) %参数3是在3维方向连接两个矩阵a和a

a+d %不同维度的数组也可兼容性运算

a.*d %可兼容性运算

bsxfun(@plus,a,d) %低版本兼容性运算函数:第一输入参数表示运算符,后面输入参数表示运算数

(第三章结束,后接第四章)

多层反馈RNN(Recurrent neural Network、循环神经网络)神经网络是一种节点定向连接成环的人工神经网络。这种网络的内部状态可以展示动态时序行为。

不同于前馈神经网络的是,RNN可以利用它内部的记忆来处理任意时序的输入序列,这让它可以更容易处理如不分段的手写识别、语音识别等。


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11694793.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存