|
EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
本帖最后由 baqiao 于 2020-4-30 13:18 编辑
. k4 ]% u; O3 D1 a$ r
4 P2 y+ R6 x. _' {7 Ex=[0 1 0 1 2 -1];y=[0 0 1 1 2 -1];z=[-1 1 1 -1 1 1];' K& l. z. O5 o
%其中,(x,y)代表二维的数据点,z 表示相应点的类型属性。3 A2 \3 Y v3 i& t' q9 P
+ \; B$ s1 { G" ^ o0 N
data=[1,0;0,1;2,2;-1,-1;0,0;1,1];% (x,y)构成的数据点
# ]! i, R5 b* L/ ^; a1 sgroups=[1;1;1;1;-1;-1];%各个数据点的标签" b. v+ w3 k, n! |0 o# S2 h
figure;
: _0 p6 ~/ Z* Dsubplot(2,2,1);
1 Z, U O- f" j! N6 `Struct1 = svmtrain(data,groups,'Kernel_Function','quadratic', 'showplot',true);%data数据,标签,核函数,训练
+ e* E4 u/ {+ D+ G; Hclasses1=svmclassify(Struct1,data,'showplot',true);%data数据分类,并显示图形9 W3 z3 @: }/ e4 x
title('二次核函数');% s9 d6 t! y2 X/ u9 W
CorrectRate1=sum(groups==classes1)/6 + J/ X1 b$ c0 ^7 @
subplot(2,2,2);& j8 M. R$ r) P, r
Struct2 = svmtrain(data,groups,'Kernel_Function','rbf', 'RBF_Sigma',0.41,'showplot',true);8 V* _7 ]+ k5 q9 p: g2 o
classes2=svmclassify(Struct2,data,'showplot',true);" T3 b4 e4 l4 c& g/ r: v6 H
title('高斯径向基核函数(核宽0.41)');
2 f0 F4 b1 ]2 X) MCorrectRate2=sum(groups==classes2)/6
6 Z b4 r+ d, r% I$ q+ R3 T) zsubplot(2,2,3);/ l. c% p7 U+ M) o) p
Struct3 = svmtrain(data,groups,'Kernel_Function','polynomial', 'showplot',true);3 H0 n* B) \, t' T0 ]6 q3 A
classes3=svmclassify(Struct3,data,'showplot',true);
" j8 N8 {, z7 mtitle('多项式核函数');( p: Z% y, ~ m) o5 G
CorrectRate3=sum(groups==classes3)/6
5 Y4 u5 J0 A8 lsubplot(2,2,4);/ h' I/ k( H' W( M: D! X1 n
Struct4 = svmtrain(data,groups,'Kernel_Function','mlp', 'showplot',true);
7 d! s* O9 l6 `1 z3 N- s7 I; @; ?classes4=svmclassify(Struct4,data,'showplot',true);; W* \+ k9 B& U0 O6 [4 z |5 I
title('多层感知机核函数');
. l8 _# m' e# I2 W6 W( X% RCorrectRate4=sum(groups==classes4)/6
0 s+ T. R. N' B& e$ C, m5 r4 ?1 B" M/ |2 I
! u+ Y. E% w5 U5 S4 e% G; f& J5 Fsvmtrain代码分析:
$ P! W1 k. j( [+ N4 S# fif plotflag %画出训练数据的点
5 N! C# [ X8 M( O# o[hAxis,hLines] = svmplotdata(training,groupIndex);
' w+ V; o+ ]; _- flegend(hLines,cellstr(groupString)); u8 ~" T5 p+ c8 F( B: ^
end: C* h( U1 ~$ g" b2 x$ e
' b" l) B8 m" u3 G/ K! L4 t% SscaleData = [];
* }/ e! t% n ~if autoScale %训练数据标准化
; Q/ V# ^0 l. H, j4 w* q* NscaleData.shift = - mean(training);
8 f5 K' ~2 k( D* r0 q3 }stdVals = std(training);8 h& I! ?3 h; H. ^0 s- T; j
scaleData.scaleFactor = 1./stdVals;# W" |- K! y( T
% leave zero-variance data unscaled:* G! w0 ^/ V; o
scaleData.scaleFactor(~isfinite(scaleData.scaleFactor)) = 1;
1 g' X* {( S! Z, t1 F3 f
% O/ X' C" l+ B. O2 ~% shift and scale columns of data matrix:
& r3 D+ A: [/ t; n+ }for c = 1:size(training, 2)& p( {6 b( v: D! Q; l. k( s
training(:,c) = scaleData.scaleFactor(c) * ...$ i) F+ ~4 N. H! U1 I
(training(:,c) + scaleData.shift(c));
. `# D M6 |( |end
; ^- p4 k0 m/ }% M8 e2 `1 |' a4 eend
$ Q, a0 T5 Q& `, x9 u- U0 y( F. M, ]5 R$ J: o
+ I7 A. B1 E; Z0 H
if strcmpi(optimMethod, 'SMO')%选择最优化算法
P$ @2 N1 }3 U7 aelse % QP and LS both need the kernel matrix:# i& f/ X6 P% L; S" x1 a; U, n
%求解出超平面的参数 (w,b): wX+b* \9 j$ {8 s$ ?8 ^* F
: P7 ?% G- ^- ^: C2 i5 Q0 p1 r
$ N/ r$ \$ T; D2 Yif plotflag %画出超平面在二维空间中的投影
F; a6 C# f' P6 j' ~ hSV = svmplotsvs(hAxis,hLines,groupString,svm_struct);7 ]0 r( }# h4 w4 w) C/ K
svm_struct.FigureHandles = {hAxis,hLines,hSV};
% J/ u$ R+ L5 E, L2 @end; ?) _5 j# S% e' u; Q2 d
6 k- j" e) i' x5 I$ C. O% Y& S9 |( V9 T6 A
svmplotsvs.m文件
( H. [. |: Q H9 \/ ?
$ ?+ U2 ?( w, dhSV = plot(sv(:,1),sv(:,2),'ko');%从训练数据中选出支持向量,加上圈标记出来
: l0 N* |6 P: o3 x J) ?8 ?% m+ l
# a( |% A/ M3 W4 olims = axis(hAxis);%获取子图的坐标空间1 L& o+ e {4 X, c
[X,Y] = meshgrid(linspace(lims(1),lims(2)),linspace(lims(3),lims(4)));%根据x和y的范围,切分成网格,默认100份; \7 M; g! @5 K3 i, f# W
Xorig = X; Yorig = Y;; ?1 B7 x; g$ p Y
4 Q) [0 f7 F0 \
% need to scale the mesh 将这些隔点标准化4 m3 \; y! U4 x: `3 `4 q3 i$ ]" K
if ~isempty(scaleData)
) @. l" h1 }2 Z, S X = scaleData.scaleFactor(1) * (X + scaleData.shift(1));0 i' k6 a+ e3 g+ L0 F
Y = scaleData.scaleFactor(2) * (Y + scaleData.shift(2));6 a( r/ [5 f! D5 Q! d8 c/ D4 s
end) ?. B( ` ~- E; S. D; \
3 g) Y; o6 T% L+ g7 Y" F. U5 s[dummy, Z] = svmdecision([X(: ),Y(: )],svm_struct); %计算这些隔点[标签,离超平面的距离]9 Z" a4 j1 R ]/ C
contour(Xorig,Yorig,reshape(Z,size(X)),[0 0],'k');%画出等高线图,这个距离投影到二维空间的等高线. ?7 ` H+ u6 S9 |2 F4 U
' P4 a5 j7 {$ @' x# H6 V' {/ R; r
svmdecision.m文件2 ~. K* |6 F& M0 v" B9 a: q1 U# S
function [out,f] = svmdecision(Xnew,svm_struct)
+ n3 Y9 U3 A% m$ v V%SVMDECISION evaluates the SVM decision function
( Q0 v! F: l1 z$ m2 U* v+ v3 Q. B+ D" p( Q, j+ G7 m1 b0 W+ j, I
% Copyright 2004-2006 The MathWorks, Inc.
) s K3 R4 U; g9 `% $Revision: 1.1.12.4 $ $Date: 2006/06/16 20:07:18 $
, G. F' @' Y! n3 M U9 F6 z o3 N* B% g
sv = svm_struct.SupportVectors;, V2 l5 b) o, W* }
alphaHat = svm_struct.Alpha;
' a) }0 ?" F! E. Ubias = svm_struct.Bias;
" K5 M: S% q& e. i# e2 Dkfun = svm_struct.KernelFunction;; P) Y" Q& D. B4 Q1 U" S
kfunargs = svm_struct.KernelFunctionArgs;, l \$ m" r9 Q9 `' E
# I% \2 Z5 ]# l: ~5 Vf = (feval(kfun,sv,Xnew,kfunargs{:})'*alphaHat(: )) + bias;%计算出距离
. R; a9 F+ Y e) T5 q* Bout = sign(f);%距离转化成标签* I7 m9 \$ f, t# n$ l+ B
% points on the boundary are assigned to class 1
6 ?: x7 ^7 j% P( R8 @: l C, wout(out==0) = 1;
) p$ W/ }* X3 W2 S8 M n! P# W
2 E6 z5 b# {! o' D: C
* b3 o3 I, a$ ] afunction K = quadratic_kernel(u,v,varargin)%核函数计算4 W1 O- J/ b& ?1 h0 E9 X I
%QUADRATIC_KERNEL quadratic kernel for SVM functions
- v1 i# f- M, m
! Z: n+ g9 m5 B* c+ ]% Copyright 2004-2008 The MathWorks, Inc.
7 r% r; H: t; `8 g& C! ^: c4 j% R) h$ U7 V1 y5 R3 l0 F
dotproduct = (u*v');: ?, V+ z7 q% Y, K7 q. v
K = dotproduct.*(1 + dotproduct);* V$ B" z' {8 L. R- |% w- H
4 q$ {2 F; Z) ]- \; T( J
5 t- D- o2 X' q+ _& t维度分析:假设输入的训练数据为m个,维度为d,记作X(m,d);显然w为w(m,1); wT*x+b
3 Q, t# }' U9 j' ]. x/ F2 b3 U, d# Z
核函数计算:k(x,y)->上公式改写成 wT*@(x)+b
4 T, ^7 e0 B" q$ j* J& ?" w1 u. b! U$ p
假设支持的向量跟训练数据保持一致,没有筛选掉一个,则支撑的数据就是归一化后的X,记作:Xst;
/ z$ q7 {0 ?9 k$ Z% d ?( J3 \( ^0 i! @' j! v
测试数据为T(n,d);9 F7 d- ~: I4 B; ?3 ^
+ O' ?, G9 r) L2 B# K* ` e3 z则核函数计算后为:(m,d)*(n,d)'=m*n;与权重和偏移中以后为: (1,m)*(m*n)=1*n;如果是训练数据作核函数处理,则m*d变成为m*m
1 Z4 l; T2 z8 X1 m; [1 \) I! m. k$ K3 l& p
这n个测试点的距离。
) k1 t1 T4 G* Q" I+ |0 v5 o$ [3 N- ~5 n
将这些隔点和其对应的超平面距离,画成等高线,得到现有图形。
0 R9 c. @$ g+ v& L# j
9 K9 b$ \& r1 I0 Q
! ?3 H2 w6 \7 X; L9 N1 R第二批测试数据:
; Y( ?' t+ r4 i$ D* A4 x
7 t8 @" ]- T/ l" Zclc;7 @% g& n! i3 U, L
clear;7 I7 e$ l/ E2 I5 W3 C
close all;
: [/ f) l8 \+ `+ U) ^( W1 e) }%rng(1); % For reproducibility# U' y3 k% i: g
r = sqrt(rand(100,1)); % Radius 0~1
& D" G3 f* V0 K4 D% e% |t = 2*pi*rand(100,1); % Angle 0~2pi
6 p2 U6 n" j4 y! n7 kdata1 = [r.*cos(t), r.*sin(t)]; % Points ~( y9 z" b: }, M) z0 u
3 V7 O/ A3 x! t) p. @r2 = sqrt(3*rand(100,1)+1); % Radius 1~43 @1 _ k& i1 o( C9 s3 ^4 n$ \
t2 = 2*pi*rand(100,1); % Angle 0~2pi( Y: U1 w$ C$ k6 F) N. ]% Q
data2 = [r2.*cos(t2), r2.*sin(t2)]; % points m; G8 f) e- m3 |: [1 |
. F0 ^3 N4 _9 C- e& dfigure;1 m4 {; Z! p' a1 Y7 a
plot(data1(:,1),data1(:,2),'r.','MarkerSize',15) t8 i6 [+ ?% `! z' r# o
hold on( }) u- d; ?, W6 K6 R0 \$ c
plot(data2(:,1),data2(:,2),'b.','MarkerSize',15)
# r9 _7 B+ x; J9 o+ Tezpolar(@(x)1);ezpolar(@(x)2);# `) t* g. y" `/ ~( i3 Q4 E8 m
axis equal* V. O( I5 I0 b4 D% N) G1 e* R b! ]' v
hold off1 [* R; \8 f" u# K a, F# l L4 p
0 l3 x! v% g0 V _- Q# D2 ]%Put the data in one matrix, and make a vector of classifications.& C& @7 Z j: S
data3 = [data1;data2];%标签 +1 -18 \4 U5 E9 R8 c9 Z- h# }* ]6 R
theclass = ones(200,1);7 O3 m) [; z6 E; n6 L
theclass(1:100) = -1;
- g1 U# u" F# X5 F% ]! S3 l# J, X/ ?# O% ]
9 i6 I, z1 `/ h7 C. p% r^2(r的平方) KMatrix = exp(-gamma*r2);7 x* s; ~1 z, d1 I& s
function KMatrix = getKRBF(X, Y, gamma)%rbf核函数 w1 O& |6 X( Q8 o- M9 P2 _. ^
r2 = repmat( sum(X.^2,2), 1, size(Y,1) ) ...7 A) i. z/ Y2 t0 R& K+ Q+ Q1 E
+ repmat( sum(Y.^2,2), 1, size(X,1) )'- 2*X*Y'
1 `- I5 E o* D& p& \! I c%K(x,y) m*n' h* _- ?5 w% E6 q6 |$ e. h
%sum(X.^2,2) m*d -> m*1 -> repmat -> m*n( H* T+ H* K' ]( p
% n*d -> n*1 -> n*m -> m*n& j8 }* O! v: Z" \; r+ h# B# i5 o: Y8 p
%m*n
2 j! }& T5 [ ] a) ^3 X9 q* {" W
1 c) e" e- n* K8 a# d0 m7 Q& p1 w% XVec表示X向量。||XVec||表示向量长度。 r表示两点距离。r^2表示r的平方。
2 o0 C5 U$ P7 Z. Q% k(XVec,YVec) = exp(-1/(2*sigma^2)*(r^2)) = exp(-gamma*r^2) & Z; D3 o; w; f
% 公式-1 这里, gamma=1/(2*sigma^2)是参数, r=||XVec-YVec|| 实际上,可看作是计算2个点X与Y的相似性。
" ^. e2 q* Z. s
+ h0 ~ L. p: y! I. N0 ~0 }; E! u# f; F9 `# p3 [
由之前对核函数的定义(见统计学习方法定义7.6):
8 s6 u2 N0 S$ U设χ是输入空间(欧氏空间或离散集合),Η为特征空间(希尔伯特空间),如果存在一个从χ到Η的映射 $ `: u$ i, I/ u( }
& K% P$ d% T2 mφ(x): χ→Η
! @6 h6 q% E: k) a+ O. M7 w7 z使得对所有的x,z∈χ,函数Κ(x,z)=φ(x)∙φ(z),
. J/ [7 u: J3 @9 \5 H+ d0 f7 v3 h则称Κ(x,z)为核函数,φ(x)为映射函数,φ(x)∙φ(z)为x,z映射到特征空间上的内积。 2 P" w5 ]6 k( M! y4 G
由于映射函数十分复杂难以计算,在实际中,通常都是使用核函数来求解内积,计算复杂度并没有增加,映射函数仅仅作为一种逻辑映射,表征着输入空间到特征空间的映射关系。例如:
1 v0 [ f! N8 X/ C$ O8 x设输入空间χ:
,
4 @0 B9 i* t8 ^" G- u% h1 M" c- d0 C: D o
9 w' ]2 `1 |7 a1 {6 f3 t映射函数φ(x)= < X,X > =
' P! t2 N" I7 p: C/ e' N核函数Κ(x,z)=
L2 t0 t$ X7 I1 B! `9 T8 e那么,取两个样例x=(1,2,3),z=(4,5,6)分别通过映射函数和核函数计算内积过程如下: 8 o" N6 P" \: V
φ(x)=(1,2,3,2,4,6,3,6,9) # D' n% O$ s! o1 r$ W) p$ |
φ(z)=(16,20,24,20,25,30,24,30,36)
6 r$ b4 B/ _3 o# x- C ?φ(x)∙φ(z)=16+40+72+40+100+180+72+180+324=1024 " X9 E# q* m5 l4 Y! r
而直接通过Κ(x,z)计算得[(4+10+18)]^2=1024
$ b' \8 U; C: ^1 Z两者相比,核函数的计算量显然要比映射函数小太多了。
! N) F& V& {9 U' ]4 C/ X4 a8 B8 |" u9 G1 f
- k4 T6 z7 ~. ^7 _7 Z( {( \$ N9 ?+ z5 |- z' O( |2 r2 j
|
|