?? main_svm_one_class.m
字號:
% 支持向量機Matlab工具箱1.0 - One-Class SVM, 一類支持向量機
% 使用平臺 - Matlab6.5
% 版權所有:陸振波,海軍工程大學
% 電子郵件:luzhenbo@yahoo.com.cn
% 個人主頁:http://luzhenbo.88uu.com.cn
% 參數文獻:Chih-Chung Chang, Chih-Jen Lin. "LIBSVM: a Library for Support Vector Machines"
%
% Support Vector Machine Matlab Toolbox 1.0 - One-Class Support Vector Machine
% Platform : Matlab6.5 / Matlab7.0
% Copyright : LU Zhen-bo, Navy Engineering University, WuHan, HuBei, P.R.China, 430033
% E-mail : luzhenbo@yahoo.com.cn
% Homepage : http://luzhenbo.88uu.com.cn
% Reference : Chih-Chung Chang, Chih-Jen Lin. "LIBSVM: a Library for Support Vector Machines"
%
% Solve the quadratic programming problem - "quadprog.m"
clc
clear
%close all
% ------------------------------------------------------------%
% 定義核函數及相關參數
nu = 0.2; % nu -> [0,1] 在支持向量數與錯分樣本數之間進行折衷
% 支持向量機的 nu 參數(取值越小,異常點就越少)
ker = struct('type','linear');
%ker = struct('type','ploy','degree',3,'offset',1);
%ker = struct('type','gauss','width',500);
%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 = 100
randn('state',1);
x1 = randn(2,floor(n*0.95));
x2 = 5+randn(2,ceil(n*0.05));
X = [x1,x2]; % 訓練樣本,d×n的矩陣,n為樣本個數,d為樣本維數
figure;
plot(x1(1,:),x1(2,:),'bx',x2(1,:),x2(2,:),'k.');
axis([-5 8 -5 8]);
title('One-Class SVM');
hold on;
% ------------------------------------------------------------%
% 訓練支持向量機
tic
svm = svmTrain('svm_one_class',X,[],ker,nu);
t_train = toc
% svm 支持向量機(結構體變量)
% the following fields:
% type - 支持向量機類型 {'svc_c','svc_nu','svm_one_class','svr_epsilon','svr_nu'}
% ker - 核參數
% x - 訓練樣本,d×n的矩陣,n為樣本個數,d為樣本維數
% y - 訓練目標,值為[]
% a - 拉格朗日乘子,1×n的矩陣
% ------------------------------------------------------------%
% 尋找支持向量
a = svm.a;
epsilon = 1e-10; % 如果小于此值則認為是0
i_sv = find(abs(a)>epsilon); % 支持向量下標
plot(X(1,i_sv),X(2,i_sv),'ro');
% ------------------------------------------------------------%
% 測試輸出
[x1,x2] = meshgrid(-5:0.05:6,-5:0.05:6);
[rows,cols] = size(x1);
nt = rows*cols; % 測試樣本數
Xt = [reshape(x1,1,nt);reshape(x2,1,nt)];
tic
Yd = svmSim(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 + -