|
SVM算法之SMO算法:百度网盘视频链接:
视频链接:http://pan.baidu.com/s/1kV5fPg7
录制的视频是算法底层原理讲解,底层代码实现,方便大家真正掌握算法实质,开发出更加出色的算法。录制视频的初衷是:避免读者朋友利用大把时间学习已有常见算法,本系列视频旨在让读者朋友快速深入了解这些常见算法原理,有更多的时间去研究更加高大上算法(价值)。
具体链接在halcom.cn论坛,联系人QQ:3283892722
该论坛是一个学习交流平台,我会逐一的和大家分享学习。
欢迎大家录制视频,并提交给我,我来设置视频,你可在论坛进行打赏分享。
视频专用播放器:http://halcom.cn/forum.php?mod=viewthread&tid=258&extra=page%3D1
运行环境:win7+32bit+matlab2014a
x:400x2
y:400x1
- % SVM using Sequential Minimal Optimization (SMO)
- clear all;clc;
- data=csvread('LinearlySeprerableData.csv');
- data(:,1:end-1)=zscore(data(:,1:end-1));
- x = data(:,1:end-1);
- y = data(:,end);
- N = length(y);
- C = 0.5; % Concluded after Cross-Validation
- tol = 10e-5;
- alpha = zeros(N,1);
- bias = 0;
- % SMO Algorithm
- while (1)
- changed_alphas=0;
- N=size(y,1)
- for i=1:N
- Ei=sum(alpha.*y.*K(x,x(i,:),'l'))-y(i);
- if ((Ei*y(i)<-tol) && alpha(i)<C)||(Ei*y(i) > tol && (alpha(i) > 0))
- for j=[1:i-1,i+1:N]
- Ej=sum(alpha.*y.*(x*x(j,:)'))-y(j);
- alpha_iold=alpha(i);
- alpha_jold=alpha(j);
-
- if y(i)~=y(j)
- L=max(0,alpha(j)-alpha(i));
- H=min(C,C+alpha(j)-alpha(i));
- else
- L=max(0,alpha(i)+alpha(j)-C);
- H=min(C,alpha(i)+alpha(j));
- end
-
- if (L==H)
- continue
- end
-
- eta = 2*x(j,:)*x(i,:)'-x(i,:)*x(i,:)'-x(j,:)*x(j,:)';
-
- if eta>=0
- continue
- end
-
- alpha(j)=alpha(j)-( y(j)*(Ei-Ej) )/eta;
- if alpha(j) > H
- alpha(j) = H;
- end
- if alpha(j) < L
- alpha(j) = L;
- end
-
- if norm(alpha(j)-alpha_jold,2) < tol
- continue
- end
-
- alpha(i)=alpha(i)+y(i)*y(j)*(alpha_jold-alpha(j));
- b1 = bias - Ei - y(i)*(alpha(i)-alpha_iold)*x(i,:)*x(i,:)'...
- -y(j)*(alpha(j)-alpha_jold)*x(i,:)*x(j,:)';
- b2 = bias - Ej - y(i)*(alpha(i)-alpha_iold)*x(i,:)*x(j,:)'...
- -y(j)*(alpha(j)-alpha_jold)*x(j,:)*x(j,:)';
-
-
- if 0<alpha(i)<C
- bias=b1;
- elseif 0<alpha(j)<C
- bias=b2;
- else
- bias=(b1+b2)/2;
- end
- changed_alphas=changed_alphas+1;
- end
- end
- end
- if changed_alphas==0
- break
- end
- x=x((find(alpha~=0)),:);
- y=y((find(alpha~=0)),:);
- alpha=alpha((find(alpha~=0)),:);
- end
- % Weights
- % W=sum(alpha.*y.*x)
- W(1)=sum(alpha.*y.*x(:,1));
- W(2)=sum(alpha.*y.*x(:,2));
- % Bias
- bias =mean( y - x*W')
- % Support Vectors
- disp('Number of support Vectors : ')
- disp(N)
- Xsupport=x;
- Ysupport=y;
- % Accuracy and F-measure
- x=data(:,1:end-1);
- y=data(:,end);
- fx=sign(W*x'+bias)';
- [~, Accuracy, F_measure ] = confusionMatrix( y,fx )
- % Plotting the Decision Boundry
- hold on
- scatter(x(y==1,1),x(y==1,2),'b')
- scatter(x(y==-1,1),x(y==-1,2),'r')
- scatter(Xsupport(Ysupport==1,1),Xsupport(Ysupport==1,2),'.b')
- scatter(Xsupport(Ysupport==-1,1),Xsupport(Ysupport==-1,2),'.r')
- x1 = -2:0.01:2;
- fn=((-bias-W(1)*x1)/W(2));
- fn1=((-1-bias-W(1)*x1)/W(2));
- fn2=((1-bias-W(1)*x1)/W(2));
- plot(x1,fn,'r','Linewidth',2);
- plot(x1,fn1,'Linewidth',1);
- plot(x1,fn2,'Linewidth',1);
- hold off
复制代码 参考链接:
【1】SVR回归(线性,多项式、RBF)预测模型
【2】Python底层SVR代码分享:http://pan.baidu.com/s/1kU773lt
【3】MATLAB底层SVR代码分享:http://pan.baidu.com/s/1pLG3dBd
【4】MATLAB底层SVC、SVR代码分享:http://pan.baidu.com/s/1dFEL6vB
【5】http://blog.csdn.net/yclzh0522/article/details/6900707
【6】http://blog.csdn.net/luoshixian099/article/details/51227754
【7】http://www.cnblogs.com/biyeymyhj ... /07/17/2591592.html
【8】支持向量机序列讲解http://blog.pluskid.org/?page_id=683
【9】python实现,理论与代码相结合的解说http://www.cnblogs.com/vivounicorn/archive/2011/06/01/2067496.html
【10】http://www.cnblogs.com/vivounicorn/archive/2011/01/13/1934296.html
|
|