|
|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
一、前言
! X& S$ P1 X2 l9 c* ^+ r 支持向量数据描述(Support Vector Data Description,SVDD)是一种单值分类算法,能够实现目标样本和非目标样本的区分,算法的具体描述可以参考以下文献:
0 j! o3 ^8 @* H5 a8 A) Z) c(1)Tax D M J, Duin R P W. Support vector domain description[J]. Pattern recognition letters, 1999, 20(11-13): 1191-1199./ |3 \3 @; |# z$ H2 a
(2)Tax D M J, Duin R P W. Support vector data description[J]. Machine learning, 2004, 54(1): 45-66.
! c5 u* `4 U6 m) }0 l" T
; ^& P& b1 g! P7 ] 台湾大学林智仁 (Lin Chih-Jen) 教授等开发设计的 libsvm 工具箱提供了SVDD算法的MATLAB接口,其中两个关键参数 c 和 g 直接影响SVDD的单值分类结果。笔者在此基础上,通过引入鲸鱼优化算法(Whale Optimization Algorithm,WOA),实现对 libsvm 工具箱中的SVDD算法的参数优化。, _& }) G4 ?. N
WOA的具体描述可以参考以下文献:) O* m5 T( g0 N F+ P
(1)Mirjalili S, Lewis A. The whale optimization algorithm[J]. Advances in engineering software, 2016, 95: 51-67.9 A! u' u, v+ E+ p+ i! u
' e) D7 A/ o! r3 c/ e
! l: S; Y, L8 L6 g7 a+ P该算法的提出者已经把代码开源在mathworks。
# ~) F7 @6 l; r# ^2 C2 S- E
- }6 S3 O2 h6 A 注:(1)笔者已把 libsvm工具箱的svmtrain和svmpredict函数的名字分别改为libsvmtrain和libsvmpredict。
7 ?3 F/ X4 a: ~. X' e U, _ (2)WOA算法和其他群智能优化算法一样,容易陷入局部最优,若寻优结果出现异常,可以尝试多运行几次。
6 n/ A+ H" \7 t+ W$ n4 }( f
. O& I/ U1 k. v% Z9 b$ c6 v二、例子1 (libsvm 工具箱提供的heart_scale data)
0 O. d1 [# c/ L
+ M$ Z2 ?: ]( o+ b% ^9 l* u1. 数据说明
; S {8 L% e; b% v, A 该数据集共有13个属性,270个样本,包括120个正样本和150个负样本。在该例子中,把正样本作为训练集,标签为1;负样本作为测试集,标签为-1。
: g" o: j; y, N* m H/ P4 r5 j. i
& P% ^6 |0 ^" q3 _* N2. 主程序代码
& C; E0 G" u# l) K! d& j. `- M( w& u/ G( R3 q( G: w( M$ I
- 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);, e/ @# M( {' e
# R! D" V: a- k7 D8 S9 E
. m3 g7 n& ]% V
最后一次迭代的结果以及最终的分类结果:8 _ ]! A: k6 y+ v$ ?
: E4 h# G0 {6 N$ _% J5 J- 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), ?+ s# S" Q" i' J$ N
- \$ X8 L/ V& \1 s1 U& P- W
: t) E" Z5 j9 E+ r可以看出,利用优化后的参数建立的SVDD模型,训练集的正确率为93.33%,测试集的正确率为100%。
+ x1 c$ k+ T9 |% X' U* h. a. y2 U5 \, ?3 S. t. A4 C' m+ Q# Q- Y- Y
三、例子2 (工业过程数据)1 d9 z; A0 N" h/ S$ [" c
( n" J% N! e6 S( y1. 数据说明1 v; n* Z% @. R2 S2 E: ~$ w2 O5 N
采用某工业过程数据,该数据集共有10个属性,训练集有400个正样本,测试集有80个样本(前40个样本为正样本,后40个样本为负样本)。
2 H+ ~2 \3 G) j
. a; J! O4 m$ g( p, p* p+ Y2. 主程序代码
. x2 G! ~2 }" q
; O3 l7 w( v' J+ A0 Y$ O- g& i4 n- 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)
~# n/ N" m; U" k& x4 { . F) I. {, s) l( h
5 ^* Z" U( k# k; y8 G+ y
最后一次迭代的结果以及最终的分类结果:
' Y& i% H r' R& J: Q7 b7 q* ]
6 D2 V9 R6 f9 d/ `( ~- 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)% C5 O5 a5 l& i5 |# L
# [7 e6 E' ~4 o& Y: `" z& a9 b
4 W( [2 `* ^$ r. V+ E可以看出,利用优化后的参数建立的SVDD模型,训练集的正确率为99.75%,测试集的正确率为93.75%。
9 ]. C, F! W2 @5 t/ Q0 j可视化结果如下:1 {& C" _* H7 w+ I* n2 ?
# f2 E- ?3 O/ G) U
+ A! @8 O" Y, w) ]% H4 c) ^6 h3 m
$ ^3 Q/ K! o/ ]6 F1 G+ I2 c+ D5 G, l, A! k
|
|