?? nu_svc.m
字號:
%****************************************************************************************************
% 支持向量機 Nu-SVC, Nu二類分類算法
clc
clear
close all
% ------------------------------------------------------------%
% 定義核函數及相關參數
nu = 0.2; % nu -> (0,1] 在支持向量數與錯分樣本數之間進行折衷
ker = struct('type','linear');
%ker = struct('type','ploy','degree',3,'offset',1);
%ker = struct('type','gauss','width',1);
%ker = struct('type','tanh','gamma',1,'offset',0);
% ker - 核參數(結構體變量)
% the following fields:
% type - linear : k(x,y) = x'*y
% poly : k(x,y) = (x'*y+c)^d
% gauss : k(x,y) = exp(-0.5*(norm(x-y)/s)^2)
% tanh : k(x,y) = tanh(g*x'*y+c)
% degree - Degree d of polynomial kernel (positive scalar).
% offset - Offset c of polynomial and tanh kernel (scalar, negative for tanh).
% width - Width s of Gauss kernel (positive scalar).
% gamma - Slope g of the tanh kernel (positive scalar).
% ------------------------------------------------------------%
% 構造兩類訓練樣本
n = 50;
randn('state',3);
x1 = randn(n,2);
y1 = ones(n,1);
x2 = 5+randn(n,2);
y2 = -ones(n,1);
figure(2);
plot(x1(:,1),x1(:,2),'bx',x2(:,1),x2(:,2),'k.');
hold on;
X = [x1;x2]; % 訓練樣本,n×d的矩陣,n為樣本個數,d為樣本維數
Y = [y1;y2]; % 訓練目標,n×1的矩陣,n為樣本個數,值為+1或-1
% ------------------------------------------------------------%
% 訓練支持向量機
tic
svm = Nu_SVC_Train(X,Y,nu,ker);
t_train = toc
% svm 支持向量機(結構體變量)
% the following fields:
% ker - 核參數
% x - 訓練樣本
% y - 訓練目標;
% a - 拉格朗日乘子
% ------------------------------------------------------------%
% 尋找支持向量
a = svm.a;
epsilon = 1e-8; % 如果小于此值則認為是0
i_sv = find(a>epsilon); % 支持向量下標
plot(X(i_sv,1),X(i_sv,2),'ro');
% ------------------------------------------------------------%
% 測試輸出
[x1,x2] = meshgrid(-2:0.05:7,-2:0.05:7);
[rows,cols] = size(x1);
nt = rows*cols; % 測試樣本數
Xt = [reshape(x1,nt,1),reshape(x2,nt,1)];
tic
Yd = Nu_SVC_Sim(svm,Xt); % 測試輸出
t_sim = toc
Yd = reshape(Yd,rows,cols);
contour(x1,x2,Yd,[0 0],'m'); % 分類面
hold off;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -