博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sequential minimal optimization,SMO for SVM, (MATLAB code)
阅读量:4962 次
发布时间:2019-06-12

本文共 3846 字,大约阅读时间需要 12 分钟。

function model = SMOforSVM(X, y, C )%sequential minimal optimization,SMOtol = 0.001; maxIters = 3000;global i1 i2 K Alpha  M1 m1 w b[m, n] = size(X);K = (X*X');Alpha = zeros(m,1); w = 0; b = 0;flag =1;iters = 1;while flag >0 & iters < maxIters     [i1,i2,m1,M1] = selectWorkSet(y, C);    if m1 - M1 <= tol        break;    end    solveOptimization(X, y, C)    iters = iters +1;endmodel.alpha = Alpha;id = find(Alpha < C & Alpha >0);% b = mean(y(id)' - (y.*Alpha)'*K(:, id));id = id(1);b = y(id)' - (y.*Alpha)'*K(:, id);w= (y.*Alpha)'* X;model.w = w;model.b = b;end%Selecting working set Bfunction [i1,i2,m1,M1]=selectWorkSet(y, C)global  K AlphaI_up =find ((Alpha < C & y == 1) | (Alpha > 0 &  y == -1));I_low = find( (Alpha < C & y == -1) | (Alpha > 0 &  y == 1));yGradient = - y.* (((y * y').* K) * Alpha - 1);[m1 , i1] = max(yGradient(I_up)); [M1 , i2] = min(yGradient(I_low)); i1 = I_up (i1);i2 = I_low(i2);end%Solving the two-variables optimization problemfunction solveOptimization(X, y, C)global Alpha K i1 i2 Ealpha1_old = Alpha(i1);alpha2_old = Alpha(i2);y1 = y(i1);y2 = y(i2);% x1 = X(i1,:)';% x2 = X(i2,:)';beta11 = K(i1,i1); beta22 = K(i2,i2); beta12 = K(i1,i2);id  =[1: length(Alpha)];id([i1 i2]) = [];beta1 = sum( y(id).*Alpha(id).*K(id,i1));beta2 = sum( y(id).*Alpha(id).*K(id,i2));E = beta1 - beta2 + alpha1_old * y1 * (beta11 - beta12) +alpha2_old*y2 * (beta12 - beta22) - y1 + y2;kk = beta11 + beta22 - 2 * beta12;alpha2_new_unc = alpha2_old + (y2 * E)/kk;if y1 ~= y2    L = max([0 , alpha2_old - alpha1_old]);    H = min([C, C - alpha1_old + alpha2_old]);else    L = max([0 , alpha1_old + alpha2_old - C]);    H = min([C,  alpha1_old + alpha2_old]);endif  alpha2_new_unc > H    alpha2_new = H;elseif  alpha2_new_unc < L    alpha2_new = L;else    alpha2_new = alpha2_new_unc ;endalpha1_new =  alpha1_old + y1 * y2 * (alpha2_old - alpha2_new);Alpha(i1) = alpha1_new;Alpha(i2) = alpha2_new;% for i=1:length(E)%     E(i) = sum(y .* Alphas .* K(i,:)) - b - y(i);% end% % % E1 = E(i1);% E2 = E(i2);% % b1 = E1 + y1 * (a1 - alph1) * K(i1,i1) + y2 * (a2 - alph2) * K(i1,i2) - b;% b2 = E2 + y1 * (a1 - alph1) * K(i1,i2) + y2 * (a2 - alph2) * K(i2,i2) - b;% % if b1 == b2%     b = b1;% else%     b = mean([b1 b2]);% end% w = w - y1 * (alpha1_new -alpha1_old) * X(i1,:)' - y2 * (alpha2_new -alpha2_old) * X(i2,:)';end

  

clearX = []; Y=[];figure;% Initialize training data to empty; will get points from user% Obtain points froom the user:trainPoints=X;trainLabels=Y;clf;axis([-5 5 -5 5]);if isempty(trainPoints)    % Define the symbols and colors we'll use in the plots later    symbols = {
'o','x'}; classvals = [-1 1]; trainLabels=[]; hold on; % Allow for overwriting existing plots xlim([-5 5]); ylim([-5 5]); for c = 1:2 title(sprintf('Click to create points from class %d. Press enter when finished.', c)); [x, y] = getpts; plot(x,y,symbols{c},'LineWidth', 2, 'Color', 'black'); % Grow the data and label matrices trainPoints = vertcat(trainPoints, [x y]); trainLabels = vertcat(trainLabels, repmat(classvals(c), numel(x), 1)); endendC = 10;par = SMOforSVM(trainPoints, trainLabels , C );p=length(par.b); m=size(trainPoints,2); if m==2% for i=1:p% plot(X(lc(i)-l(i)+1:lc(i),1),X(lc(i)-l(i)+1:lc(i),2),'bo')% hold on% end k = -par.w(1)/par.w(2); b0 = - par.b/par.w(2); bdown=(-par.b-1)/par.w(2); bup=(-par.b+1)/par.w(2); for i=1:p hold on h = refline(k,b0(i)); set(h, 'Color', 'r') hdown=refline(k,bdown(i)); set(hdown, 'Color', 'b') hup=refline(k,bup(i)); set(hup, 'Color', 'b') end endxlim([-5 5]); ylim([-5 5]);

 以上代码结果写的比较粗糙,可能不稳定,我重新贴了一个新的代码:

http://www.cnblogs.com/huadongw/p/4994657.html

转载于:https://www.cnblogs.com/huadongw/p/4601377.html

你可能感兴趣的文章
kettle 数据库连接失败
查看>>
ListView失去焦点选中行不能高亮显示的问题解决
查看>>
# jsp及servlet学习笔记
查看>>
Kconfig详解
查看>>
(四)hadoop系列之__hadoop搭建(单机配置)
查看>>
nodejs爬虫数据存入mysql
查看>>
sphinx2.8.8的配置文件
查看>>
Visual Studio 2019 正式版 更新内容
查看>>
4、下行短信发送WebService、下行短信发送服务 -功能详细设计 --短信平台
查看>>
Failure to find com.oracle:ojdbc6:jar
查看>>
文本去重-----awk或者uniq
查看>>
Android学习笔记三:Intent实现页面跳转
查看>>
Django下JWT的使用
查看>>
React Native 的组件之底部导航栏 TabBarIOS(一)
查看>>
聊聊、SpringBoot 上传文件大小
查看>>
WF 学习笔记 (1) - 浅谈 WF 和 MVC 架构
查看>>
Monkey脚本API简介
查看>>
Linux学习笔记 之 Linux软件的安装与卸载
查看>>
在ASP.NET中,IE与Firefox下载文件带汉字名时乱码的解决方法
查看>>
Atitit.android播放smb 网络邻居视频文件解决方案
查看>>