⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 opt1.m

📁 svm classification and regression code
💻 M
字号:
    % Construct the Kernel matrix    X=rand(150,2);    Y=ones(150,1);    figure(1);    hold on    plot(X(:,1),X(:,2),'*');    plot(-1,-1,2,2);    C=Inf;    n=size(X,1);    ker='poly';    global p1;    p1=0.5;    epsilon = svtol(C);    fprintf('Constructing ...\n');    H = zeros(n,n);      for i=1:n       for j=1:n          H(i,j) = svkernel(ker,X(i,:),X(j,:));       end    end    c = -zeros(n,1);      H = H+1e-10*eye(size(H));        vlb = zeros(n,1);      % Set the bounds: alphas >= 0    vub = C*ones(n,1);     %                 alphas <= C    x0 = zeros(n,1);       % The starting point is [0 0 0   0]    neqcstr = nobias(ker); % Set the number of equality constraints (1 or 0)      if neqcstr       A = Y';, b = 0;     % Set the constraint Ax = b    else       A = [];, b = [];    end    A=Y';    b=1;    % Solve the Optimisation Problem        fprintf('Optimising ...\n');    st = cputime;        [alpha lambda how] = qp(H, c, A, b, vlb, vub, x0, neqcstr);    nsv=length(find(alpha>=epsilon));    fprintf('Support Vectors : %d (%3.1f%%)\n',nsv,100*nsv/n);        Xt=[];    for i=-0.5:0.05:1.5        for j=-0.5:0.05:1.5            Xt=[Xt;i,j];        end    end        i=1;    while i<=n&alpha(i,1)<=epsilon        i=i+1;    end    i0=i;        f0=0;    for j=1:n        f0=f0-svkernel(ker,X(i0,:),X(j,:));    end            n1=size(Xt,1);    for i=1:n1        f=0;        for j=1:n            f=f-svkernel(ker,Xt(i,:),X(j,:));        end        if f<f0            Yt(i,1)=1;        else            Yt(i,1)=-1;        end    end        y1=find(Yt==1);    figure(2);    hold on    plot(Xt(y1,1),Xt(y1,2),'k+');    plot(X(:,1),X(:,2),'*');                      

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -