|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
一、前言2 k' z! E5 P& r5 G0 W% G. |+ P
支持向量数据描述(Support Vector Data Description,SVDD)是一种单值分类算法,能够实现目标样本和非目标样本的区分,算法的具体描述可以参考以下文献:
o4 y8 I: T- E/ {; k$ Y(1)Tax D M J, Duin R P W. Support vector domain description[J]. Pattern recognition letters, 1999, 20(11-13): 1191-1199.
; v |" r7 d% e0 S: t(2)Tax D M J, Duin R P W. Support vector data description[J]. Machine learning, 2004, 54(1): 45-66.
- m6 m4 i; G0 \" A8 g( `! F6 N2 w' X. C/ N
台湾大学林智仁 (Lin Chih-Jen) 教授等开发设计的 libsvm 工具箱提供了SVDD算法的MATLAB接口,其中两个关键参数 c 和 g 直接影响SVDD的单值分类结果。笔者在此基础上,通过引入鲸鱼优化算法(Whale Optimization Algorithm,WOA),实现对 libsvm 工具箱中的SVDD算法的参数优化。
6 h3 q0 @8 u! \5 ~WOA的具体描述可以参考以下文献:4 `6 e! B7 L7 f" F3 w, c
(1)Mirjalili S, Lewis A. The whale optimization algorithm[J]. Advances in engineering software, 2016, 95: 51-67.
' p; x. \" x/ D( Z" {
5 E w/ S" ?. B
8 e7 c7 y) [6 d) O1 w该算法的提出者已经把代码开源在mathworks。
# C# r- W) p/ S7 h
, J G4 {5 f3 o9 i! ~0 H 注:(1)笔者已把 libsvm工具箱的svmtrain和svmpredict函数的名字分别改为libsvmtrain和libsvmpredict。
4 F+ e3 Q. G: D" y (2)WOA算法和其他群智能优化算法一样,容易陷入局部最优,若寻优结果出现异常,可以尝试多运行几次。( v! h! w, Y+ w6 x( G6 \- `
' F* s* h7 G) {0 D* B- I# p# t二、例子1 (libsvm 工具箱提供的heart_scale data)5 [2 H4 R! ^; a1 |$ Z- N* q& E
9 w F3 R' M' s# o: R2 B
1. 数据说明
. _- B& l- g' G$ F( h 该数据集共有13个属性,270个样本,包括120个正样本和150个负样本。在该例子中,把正样本作为训练集,标签为1;负样本作为测试集,标签为-1。
. t# J: c# l" R: [' V% U0 z9 @; D- l8 W0 v9 m, x0 b0 r ]* u$ p
2. 主程序代码
4 v. [1 k5 c0 x/ a7 [
5 l; {" i# d2 U, {: N1 F- clc
- clear all
- close all
- addpath(genpath(pwd))
- global traindata trainlabel
- % heart_scale data
- [traindata, testdata, trainlabel, testlabel] = prepareData;
- % Parameter setting of WOA
- agent = 10; % Number of search agents
- iteration = 20; % Maximum numbef of iterations
- lb = [10^-3,2^-4]; % Lower bound of 'c' and 'g'
- ub = [10^0,2^4]; % Upper bound of 'c' and 'g'
- dim = 2; % Number of Parameter
- fobj = @woa_obj; % Objective function
- % Parameter optimization using WOA
- [Best_score,Best_pos,~] = WOA(agent,iteration,lb,ub,dim,fobj);
- % Train SVDD hypersphere using the optimal parameters
- cmd = ['-s 5 -t 2 ','-c ',num2str(Best_pos(1,1)),' -g ', ...
- num2str(Best_pos(1,2)),' -q'];
- model = libsvmtrain(trainlabel, traindata, cmd);
- % Test
- [predictlabel,accuracy,~] = libsvmpredict(testlabel, testdata, model);
% p$ s1 G% m% v: j6 A3 z' U$ {4 C5 a5 H ' ?, u3 V9 C& |3 R; r. w
}' l& f' q2 i; J$ p
最后一次迭代的结果以及最终的分类结果:
, w+ q/ C7 J- f# j! l
3 ]& P8 m5 l, s& Q# r8 x5 a; i- ans =
- 19.0000 0.0667
- Accuracy = 80% (96/120) (classification)
- Accuracy = 66.6667% (80/120) (classification)
- Accuracy = 60% (72/120) (classification)
- Accuracy = 80% (96/120) (classification)
- Accuracy = 53.3333% (64/120) (classification)
- Accuracy = 54.1667% (65/120) (classification)
- Accuracy = 42.5% (51/120) (classification)
- Accuracy = 35% (42/120) (classification)
- Accuracy = 80% (96/120) (classification)
- Accuracy = 35% (42/120) (classification)
- ans =
- 20.0000 0.0667
- Accuracy = 100% (150/150) (classification)
c( H2 \3 O0 v4 d % e# W2 y0 P" S; `! v9 u
% \. L) T- z ]! ^, z) K
可以看出,利用优化后的参数建立的SVDD模型,训练集的正确率为93.33%,测试集的正确率为100%。
& B' i, L' B* o) O6 i6 C$ {$ }& {, F) Q5 Y# ?
三、例子2 (工业过程数据) m) Y* ~9 S- U" d
- c# E0 ^& `) F; p) Q
1. 数据说明2 k4 r- t+ R' s* Z6 w4 |- r& q
采用某工业过程数据,该数据集共有10个属性,训练集有400个正样本,测试集有80个样本(前40个样本为正样本,后40个样本为负样本)。
; w1 k2 B2 V0 g5 H* u; J/ k; J. F8 `/ F' H( ?/ }0 n
2. 主程序代码
# U# W, k) C" i
; [- H7 i- H+ p: U& J/ M- clc
- clear all
- addpath(genpath(pwd))
- global traindata trainlabel
- % Industrial process data
- load ('.\data\data_2.mat')
- % Parameter setting of WOA
- agent = 10; % Number of search agents
- iteration = 30; % Maximum numbef of iterations
- lb = [10^-3,2^-7]; % Lower bound of 'c' and 'g'
- ub = [10^0,2^7]; % Upper bound of 'c' and 'g'
- dim = 2; % Number of Parameter
- fobj = @woa_obj; % Objective function
- % Parameter optimization using WOA
- [Best_score,Best_pos,~] = WOA(agent,iteration,lb,ub,dim,fobj);
- % Train SVDD hypersphere using the optimal parameters
- cmd = ['-s 5 -t 2 ','-c ',num2str(Best_pos(1,1)),' -g ', ...
- num2str(Best_pos(1,2)),' -q'];
- model = libsvmtrain(trainlabel, traindata, cmd);
- % Test
- [predictlabel,accuracy,~] = libsvmpredict(testlabel, testdata, model);
- % Visualize the results
- plotResult(testlabel,predictlabel)
2 [7 l \) Z, d 3 h9 ^$ M) ^/ M+ U5 z+ s9 Q" p4 n- B
: N" a4 A+ @$ Z2 u最后一次迭代的结果以及最终的分类结果:
( |2 y0 t4 |# {$ @
/ F) i# Q/ n% G) E) w- Accuracy = 99.5% (398/400) (classification)
- Accuracy = 99.25% (397/400) (classification)
- Accuracy = 99.75% (399/400) (classification)
- Accuracy = 99.75% (399/400) (classification)
- Accuracy = 99.5% (398/400) (classification)
- Accuracy = 99.25% (397/400) (classification)
- Accuracy = 99.75% (399/400) (classification)
- Accuracy = 99.75% (399/400) (classification)
- Accuracy = 99.5% (398/400) (classification)
- Accuracy = 99.5% (398/400) (classification)
- ans =
- 30.0000 0.0025
- Accuracy = 93.75% (75/80) (classification)4 P0 b& r) `, t, {
3 Y0 @, H3 _& z6 f U
) \" U: j3 ]+ A% G
可以看出,利用优化后的参数建立的SVDD模型,训练集的正确率为99.75%,测试集的正确率为93.75%。
/ b7 ?8 ^% [3 s/ I1 O, z5 N可视化结果如下:( @; g! n) o. i" F
* W8 K4 |! ?/ ? 6 r0 Q, C+ b9 \& B0 { B8 o( J
) m n( s8 f& e2 Q* L
, d6 [& B. d# W% G$ Y
* j7 r! I* Y7 I: g) b4 [ |
|