MATLAB Implementation of binary classification SVM

Source: Internet
Author: User
Tags svm

MATLAB is used to implement binary classification SVM. The optimization technology uses the quadprog function provided by Matlab.

Only to check what you have learned, more familiar; not to show off. There is not much time to use more optimization methods.

Function Model = svm0311 (data, options) % svm0311 solution 2 SVM method, optimized using the Matlab Optimization Toolbox quadprog function to implement % by lifeiteng Email: lifeiteng0422@gmail.com % reference: stptool % Pattern Recognition and machine learning p333 7.32-7.37% input aruments % ------------------------------------------- ticdata = C2S (data); [dim, num_data] = size (data. x); If nargin <2, Options = []; else Options = C2S (options); endif ~ Isfield (options, 'ker'), options. Ker = 'linear '; endif ~ Isfield (options, 'arg '), options. Arg = 1; endif ~ Isfield (options, 'C'), options. c = inf; endif ~ Isfield (options, 'norm'), options. Norm = 1; endif ~ Isfield (options, 'mu '), options. MU = 1e-12; endif ~ Isfield (options, 'eps '), options. EPS = 1e-12; endx = data. x; t = data. y; T (t = 2) =-1; % set up QP task % -------------------------- K = x' * X; t = t' * t; % note that T is a horizontal volume H = K. * t; save ('h0311. mat ', 'H') H = H + options. mu * eye (SIZE (h); F =-ones (num_data, 1); aeq = T; beq = 0; lB = zeros (num_data, 1 ); UB = options. C * ones (num_data, 1); X0 = zeros (num_data, 1); qp_options = optimset ('display', 'off'); [Alpha, fval, exitflag] = quadprog (H, F, [], [], aeq, beq, LB, UB, x0, qp_options); inx_sv = find (alpha> options. EPS); % compute bias % ------------------------ % Take boundary (f (x) = +/-1) Support Vectors 0 <Alpha <cb = 0; inx_bound = find (alpha> options. EPS & Alpha <(options. c-Options. EPS); Nm = length (inx_bound); for n = 1: Nm TMP = 0; for M = 1: length (inx_sv) % prml7.37 TMP = TMP + alpha (inx_sv (M) * t (inx_sv (M) * K (inx_bound (N), inx_sv (m )); end B = B + T (inx_bound (N)-TMP; endb = B/Nm; model. B = B; % ------------------------------------- W = zeros (dim, 1); for I = 1: num_data W = W + alpha (I) * t (I) * X (:, i); % PRML 7.29 endmargin = 1/norm (w); % ------------------------------------------- % the interface used for drawing and other models is consistent with that used by stprtool. alpha = alpha (inx_sv); Model. sv. X = data. X (:, inx_sv); Model. sv. y = data. Y (inx_sv); Model. sv. signature = inx_sv; model. nSv = length (inx_sv); Model. margin = margin; model. exitflag = exitflag; model. options = options; model. kercnt = num_data * (num_data + 1)/2; model. trnerr = cerror (data. y, svmclass (data. x, model); Model. fun = 'svmclass'; model. W = model. sv. x * model. alpha; % used CPU timemodel. cputime = toc; return;


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.