目录

1.软件版本

2.核心代码

3.操作步骤与仿真结论

4.参考文献

5.完整源码获得方式


1.软件版本

MATLAB2021a

2.核心代码

function [model] = svmTrain(X, Y, C, kernelFunction, ...tol, max_passes)if ~exist('tol', 'var') || isempty(tol)tol = 1e-3;
endif ~exist('max_passes', 'var') || isempty(max_passes)max_passes = 5;
end% Data parameters
m = size(X, 1);
n = size(X, 2);% Map 0 to -1
Y(Y==0) = -1;% Variables
alphas = zeros(m, 1);
b = 0;
E = zeros(m, 1);
passes = 0;
eta = 0;
L = 0;
H = 0;% Pre-compute the Kernel Matrix since our dataset is small
% (in practice, optimized SVM packages that handle large datasets
%  gracefully will _not_ do this)
%
% We have implemented optimized vectorized version of the Kernels here so
% that the svm training will run faster.
if strcmp(func2str(kernelFunction), 'linearKernel')% Vectorized computation for the Linear Kernel% This is equivalent to computing the kernel on every pair of examplesK = X*X';
elseif strfind(func2str(kernelFunction), 'gaussianKernel')% Vectorized RBF Kernel% This is equivalent to computing the kernel on every pair of examplesX2 = sum(X.^2, 2);K = bsxfun(@plus, X2, bsxfun(@plus, X2', - 2 * (X * X')));K = kernelFunction(1, 0) .^ K;
else% Pre-compute the Kernel Matrix% The following can be slow due to the lack of vectorizationK = zeros(m);for i = 1:mfor j = i:mK(i,j) = kernelFunction(X(i,:)', X(j,:)');K(j,i) = K(i,j); %the matrix is symmetricendend
end% Train
fprintf('\nTraining ...');
dots = 12;
while passes < max_passes,num_changed_alphas = 0;for i = 1:m,% Calculate Ei = f(x(i)) - y(i) using (2). % E(i) = b + sum (X(i, :) * (repmat(alphas.*Y,1,n).*X)') - Y(i);E(i) = b + sum (alphas.*Y.*K(:,i)) - Y(i);if ((Y(i)*E(i) < -tol && alphas(i) < C) || (Y(i)*E(i) > tol && alphas(i) > 0)),% In practice, there are many heuristics one can use to select% the i and j. In this simplified code, we select them randomly.j = ceil(m * rand());while j == i,  % Make sure i \neq jj = ceil(m * rand());end% Calculate Ej = f(x(j)) - y(j) using (2).E(j) = b + sum (alphas.*Y.*K(:,j)) - Y(j);% Save old alphasalpha_i_old = alphas(i);alpha_j_old = alphas(j);% Compute L and H by (10) or (11). if (Y(i) == Y(j)),L = max(0, alphas(j) + alphas(i) - C);H = min(C, alphas(j) + alphas(i));elseL = max(0, alphas(j) - alphas(i));H = min(C, C + alphas(j) - alphas(i));endif (L == H),% continue to next i. continue;end% Compute eta by (14).eta = 2 * K(i,j) - K(i,i) - K(j,j);if (eta >= 0),% continue to next i. continue;end% Compute and clip new value for alpha j using (12) and (15).alphas(j) = alphas(j) - (Y(j) * (E(i) - E(j))) / eta;% Clipalphas(j) = min (H, alphas(j));alphas(j) = max (L, alphas(j));% Check if change in alpha is significantif (abs(alphas(j) - alpha_j_old) < tol),% continue to next i. % replace anywayalphas(j) = alpha_j_old;continue;end% Determine value for alpha i using (16). alphas(i) = alphas(i) + Y(i)*Y(j)*(alpha_j_old - alphas(j));% Compute b1 and b2 using (17) and (18) respectively. b1 = b - E(i) ...- Y(i) * (alphas(i) - alpha_i_old) *  K(i,j)' ...- Y(j) * (alphas(j) - alpha_j_old) *  K(i,j)';b2 = b - E(j) ...- Y(i) * (alphas(i) - alpha_i_old) *  K(i,j)' ...- Y(j) * (alphas(j) - alpha_j_old) *  K(j,j)';% Compute b by (19). if (0 < alphas(i) && alphas(i) < C),b = b1;elseif (0 < alphas(j) && alphas(j) < C),b = b2;elseb = (b1+b2)/2;endnum_changed_alphas = num_changed_alphas + 1;endendif (num_changed_alphas == 0),passes = passes + 1;elsepasses = 0;endfprintf('.');dots = dots + 1;if dots > 78dots = 0;fprintf('\n');endif exist('OCTAVE_VERSION')fflush(stdout);end
end
fprintf(' Done! \n\n');% Save the model
idx = alphas > 0;
model.X= X(idx,:);
model.y= Y(idx);
model.kernelFunction = kernelFunction;
model.b= b;
model.alphas= alphas(idx);
model.w = ((alphas.*Y)'*X)';end

3.操作步骤与仿真结论

4.参考文献

[1] Osuna E ,  Freund R ,  Girosi F . Training svm: An application to face detection.  1997.

D224

5.完整源码获得方式

方式1:微信或者QQ联系博主

方式2:订阅MATLAB/FPGA教程,免费获得教程案例以及任意2份完整源码

【SVM】基于matlab的SVM支持向量机训练和测试仿真相关推荐

  1. 基于MATLAB的SVM的交通标志识别

    基于MATLAB的SVM的交通标志识别 摘要:本文针对三种不同的交通标识(直行.右拐和直行左拐)给出了一种基于SVM识别方法.该方法首先在分析训练集交通标识图片特点的基础上,提取它们的PHOG特征向量 ...

  2. java数字图像处理开题报告,基于MATLAB的数字图像处理算法研究与仿真开题报告...

    基于MATLAB的数字图像处理算法研究与仿真开题报告 毕 业 设 计 (2013 届) 题 目基于 MATLAB 的数字图像 处理算法研究与仿真 学 院 物理电气信息学院 专 业 通信工程 年 级 0 ...

  3. 基于Matlab使用激光雷达检测分类跟踪车辆仿真(附源码)

    目录 一.概述 二.加载数据 三.接地层分割 四.语义分割 五.簇状和边界框管接头 六.可视化设置 七.总结 八.程序 此示例演示如何使用安装在 ego 车辆上的激光雷达传感器捕获的激光雷达点云数据来 ...

  4. 如何使用matlab得出pid控制参数值,基于MATLAB的PID控制器参数整定及仿真

    基于MATLAB的PID控制器参数整定及仿真 摘要:PID控制器结构和算法简单,应用广泛,但参数整定比较复杂,在此我探讨利用MATLAB实现PID参数整定及其仿真的方法,并分析比较比例.比例积分.比例 ...

  5. 循环码差错图样matlab,基于MATLAB的(15,7)循环码的编译仿真.doc

    基于MATLAB的(15,7)循环码的编译仿真 <纠错码与差错控制> 课程设计 题目:基于MATLAB的(15,7)循环码的编译仿真 院(系) 信息科学与工程学院 专 业 通信工程专业 届 ...

  6. 基于matlab的数字下变频器的设计与仿真应用,基于MATLAB的数字下变频器的设计与仿真应用.pdf...

    基于MATLAB的数字下变频器的设计与仿真应用 中国科技论文在线 基于 Matlab 的数字下变频器的设计与仿真 朱建新,沈树群* (北京邮电大学电子工程学院,北京 100876 ) 摘要:本文通过对 ...

  7. 微弱信号相关检测程序matlab,基于MATLAB微弱信号的相关检测设计与仿真.pdf

    基于MATLAB微弱信号的相关检测设计与仿真.pdf 2010年第1期 中国高新技术企业 NO.1.2010 (总第136期) ChinaHi-TechEnterprises (Cumulativet ...

  8. 基于matlab的模糊控制器的设计与仿真,基于MATLAB的模糊控制洗衣机的设与仿真详解.doc...

    基于MATLAB的模糊控制洗衣机的设计与 仿真 卫瑶瑶,王胜红 (南京农业大学工学院,210031) 摘要:根据模糊控制的原理对传统洗衣机进行改造,设计了模糊控制系统.通过MATLAB仿真,采用取最大 ...

  9. matlab pid buck,基于MATLAB的BUCK电路设计与PID闭环仿真

    文章编号:1009-2552( 2015) 10-0155-04 DOI:10. 13274/j. cnki. hdzj. 2015. 10. 042 基于 MATLAB 的 BUCK 电路设计与 P ...

最新文章

  1. matlab练习程序(最小二乘多项式拟合)
  2. 【python】app未注册用户自动筛选
  3. jquery attr()和prop()方法的区别
  4. java 与 xml_xml与java对象转换
  5. Destoon源数据库配置文件在哪_数据库监控软件Lepus安装部署详解
  6. matlab elif,MATLAB与fftfilt相当于Python
  7. 海岸鸿蒙甲醇中8种苯系物,甲醇中8种苯系物混合溶液标准物质-8种VOC
  8. python复制、移动文件到指定文件夹
  9. python批量处理text_【RhinoPython】Rhino如何批量替换text 和Dot
  10. html在线拍照片,HTML调用相机拍照
  11. XML 用户界面语言(XUL)开发简介
  12. 《Adobe After Effects CS6中文版经典教程》——2.9 渲染合成图像
  13. 华为HCIE证书含金量
  14. kubeadm 常用命令
  15. 白杨SEO:百度算法更新大全合集49条(截至2021年9月),做百度关键词搜索排名必看!
  16. 信捷plc c 语言全局变量,三菱PLC编程中,跳转指令CJ、子程序调用CALL和中断指令有什么区别?...
  17. 超给力,一款简单又实用的免费 GitHub 加速神器
  18. 视频教程-OpenGLES萌谷手册(iOS2018版)-其他
  19. 为什么溺水事故无法“清零”?
  20. slf4j.Logger的全面讲解及e.getMessage()为何为空

热门文章

  1. 决策树桩(Decision Stump)
  2. 11.2Java员工信息管理系统
  3. 新能源锂电池行业除杂工艺介绍
  4. 计组 | 周期大杂烩 —— 指令周期、时钟周期(节拍周期)、机器周期(CPU周期)等
  5. java爬虫框架哪个好_java爬虫框架的使用
  6. Java爬虫框架WebMagic简介及使用
  7. GRR(测量系统的重复性和复现性)
  8. json引发的惨案--------饿百 同步骑手状态接口
  9. 网络安全应急响应-电子数据取证技术
  10. (三)软件测试理论(了解软件的缺陷知识)