移动学习网 导航

支持向量机的matlab代码 谁有模糊支持向量机的matlab程序,跪求

2024-05-12m.verywind.com
请问在matlab中如何实现支持向量机(SVM)算法?~

matlab自带svmtrain,进去看help,照着例子做就懂了

云盘链接已私信发送给你了,你看一下是不是你需要的
如果还有哪位朋友想要,请点“赞”此条回答以后,在下面的评论里留下您的联系方式
学无止境,希望回答能给你带来帮助,如果满意请采纳,不满意的话请继续追问。

如果是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 = false; usePoly = false;
setMLP = false; useMLP = 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 = [0;ones(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

  • 人工智能要做实验了,题目是数字识别,用matlab实现svm算法,就是支持向量...
  • 答:使用Matlab中的LIBSVM工具箱。

  • Matlab LIBSVM和LSSVM有什么区别
  • 答:这两个意义完全不一样,lssvm是最小二乘支持向量机,是一种算法 libsvm是一个支持向量机的工具集合,一个库,所以两个概念完全不同哦

  • 支持向量机基本原理 matlab程序及其应用
  • 答:支持向量机基本原理 matlab程序及其应用 这几天进行计算机实习,老师就给了这个课题,对于我们这些连matlab考试都觉得难的人来说简直就是赶鸭子上架,只能求助大神了,哪位大神可以通俗的讲讲svm的原理,以及怎么运用它,还有... 这几天进行...

  • 支持向量机及Python代码实现
  • 答:回答:支持向量机及Python代码实现  做机器学习的一定对支持向量机(supportvectormachine-SVM)颇为熟悉,因为在深度学习出现之前,SVM一直霸占着机器学习老大哥的位子。他的理论很优美,各种变种改进版本也很多,比如latent...

  • matlab中lsim结果怎么使用
  • 答:1、首先lsim是针对线性是不变模型,给定任意输入,得到输出响应,系统模型为状态方程时,同时还可以得到状态轨迹。2、当输出y的行数与u的长度相同,列数与输出个数相同。3、当状态x的行数与u的长度相同,列数与状态的个...

  • matlab15.0b中怎么实现自带支持向量机
  • 答:BPNN可以用matlab里的神经网络工具箱,GUI的界面或者matlab源程序都可以 SVM推荐用Libsvm或Lssvm,网上都有免费下载额

  • matlab中real和complex有什么作用,及设置方法
  • 答:c = complex(a,b)表示c = a + bi X = real(Z),表示,取Z的实数部分

  • 求python支持向量机数据设置标签代码
  • 答:以下是使用Python中的Scikit-learn库实现支持向量机(SVM)模型的数据设置标签代码示例:from sklearn import svm 假设有以下三个样本的数据:X = [[0, 0], [1, 1], [2, 2]]y = [0, 1, 1] # 对应每个数据...

  • 如何在matlab中定义n维向量?
  • 答:向量的相关知识2011-12-29 向量的定义 8 2007-04-08 支持向量机的matlab代码 74 2009-08-09 关于平面向量的公式 3027 2010-12-24 向量三角形题 30 2012-07-06 对任意两个非零向量αβ,定义α※β=α·β/β·β 81...

  • matlab2018 错误使用 svmtrain (line 230)
  • 答:新版本中svmtrain已经被废弃了,用fitcsvm代替了。你装了libsvm,但是matlab默认的是软件自带的那个svmtrain,所以会提示报错,你把名字改了之后要添加到路径中才可以正常使用

    户户网菜鸟学习
    联系邮箱
    返回顶部
    移动学习网