SVM感知机知识点记录及相关实验

上机器学习课时记录的SVM思维导图:
在这里插入图片描述
实验时老师给我们发了一些代码让我们研究并且补全…

线性可分SVM

下面是相关实验中实现的实验代码,这一部分调用了matlab 的相关函数,自己弄懂了以后又学习别人的代码,用matlab的等高线contour函数绘制了分界线:

clear;
close all;
%% 使用SVM(支持向量机)分割两类点并画出图形
XY1 = 2 + rand(100,2); % 随机产生1002列在2-3之间的点
XY2 = 3+ rand(100,2);% 随机产生1002列在3-4之间的点
XY = [XY1;XY2]; % 合并两点
Classify =[zeros(100,1);ones(100,1)]; % 第一类点用0表示,第二类点用1表示
Sample = 2+ 2*rand(100,2); % 测试点
figure(1);
h(1)=plot(XY1(:,1),XY1(:,2),'r*'); % 第一类点用红色表示
hold on;
h(2)=plot(XY2(:,1),XY2(:,2),'b*'); % 第二类点用蓝色表示
%% 训练SVM
SVM = fitcsvm(XY,Classify);
%% 给测试点分类,并作出最大间隔超平面(一条直线)

d = 0.02;
[x1Grid,x2Grid] = meshgrid(min(Sample(:,1)):d:max(Sample(:,1)),...  %基于向量 x 和 y 中包含的坐标返回二维网格坐标
    min(Sample(:,2)):d:max(Sample(:,2)));
xGrid = [x1Grid(:),x2Grid(:)];

[~,score] = predict(SVM,xGrid); 
pre=predict(SVM,Sample);
sort1_index = pre == 0. % 测试数据属于第一类的序号(bool 数组)
sort2_index = pre == 1. % 测试数据属于第二类的序号(bool 数组)
test_sort1 = Sample(sort1_index,:) % 测试数据属于第一类的点
test_sort2 = Sample(sort2_index,:) % 测试数据属于第二类的点



% figure;
h(3)=plot(test_sort1(:,1),test_sort1(:,2),'ro'); % 第一类测试点用红色表示
hold on;
h(4)=plot(test_sort2(:,1),test_sort2(:,2),'bo'); % 第二类测试点用蓝色表示
hold on;
h(5) = plot(XY(SVM.IsSupportVector,1),XY(SVM.IsSupportVector,2),'ko');  %圈出支持向量


%% 尝试画出结果
hold on;
scoreGrid = reshape(score(:,2),size(x1Grid,1),size(x2Grid,2));
contour(x1Grid,x2Grid,scoreGrid,[0 0],'k'); %利用矩阵的等高线图进行分类区分
legend('train data 1','train data 2','test data 1','test data 2','Support Vectors','分界线');
%%%%%%%% 

最后绘制出来的图像如下所示:
在这里插入图片描述

非线性SVM-高斯核

%% 高斯核训练SVM

%% 生成在单位圆盘上均匀分布的 100 个点。
rng('default'); % For reproducibility
r = sqrt(rand(100,1)); % Radius
t = 2*pi*rand(100,1);  % Angle
data1 = [r.*cos(t), r.*sin(t)]; % Points
%% 生成在环形空间中均匀分布的 100 个点。
r2 = sqrt(3*rand(100,1)+1); % Radius
t2 = 2*pi*rand(100,1);      % Angle
data2 = [r2.*cos(t2), r2.*sin(t2)]; % points
%% 绘图
figure;
plot(data1(:,1),data1(:,2),'r.','MarkerSize',15)
hold on
plot(data2(:,1),data2(:,2),'b.','MarkerSize',15)
ezpolar(@(x)1);ezpolar(@(x)2);
axis equal
hold off
%% 建立分类向量
data3 = [data1;data2];
theclass = ones(200,1);
theclass(1:100) = -1;

%% Train the SVM Classifier 训练分类器
cl = fitcsvm(data3,theclass,'KernelFunction','rbf',...
    'BoxConstraint',Inf,'ClassNames',[-1,1]);

%% Predict scores over the grid
d = 0.02;
[x1Grid,x2Grid] = meshgrid(min(data3(:,1)):d:max(data3(:,1)),...  %基于向量 x 和 y 中包含的坐标返回二维网格坐标
    min(data3(:,2)):d:max(data3(:,2)));
xGrid = [x1Grid(:),x2Grid(:)];
[~,scores] = predict(cl,xGrid); %predict(categoryClassifier,I) returns the predicted label index and score for the input image.

%% Plot the data and the decision boundary
figure;
h(1:2) = gscatter(data3(:,1),data3(:,2),theclass,'rb','.');
hold on
ezpolar(@(x)1);
h(3) = plot(data3(cl.IsSupportVector,1),data3(cl.IsSupportVector,2),'ko'); %画出支持向量SV
contour(x1Grid,x2Grid,reshape(scores(:,2),size(x1Grid)),[0 0],'k'); %利用矩阵的等高线图进行区分
legend(h,{'-1','+1','Support Vectors'});
axis equal
hold off

高斯核训练SVM的效果图如下所示,图例中data1是原先生成时的分界线,data2圈起来的是支持向量,data3训练后用等高线绘制出来的分界线。(个人理解)
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值