- 18
- 0
- 约1.81千字
- 约 2页
- 2016-07-04 发布于重庆
- 举报
定义核函数及相关参数
定义核函数及相关参数
?
C = 200;??????????????? % 拉格朗日乘子上界
?
ker =?struct(type,linear);
%ker =?struct(type,ploy,degree,3,offset,1);
%ker =?struct(type,gauss,width,1);
%ker =?struct(type,tanh,gamma,1,offset,0);
?
% ker - 核参数(结构体变量)
% the following fields:
%?? type?? - linear :? k(x,y) = x*y
%??????????? poly?? :? k(x,y) = (x*y+c)^d
%??????????? gauss? :? k(x,y) =?exp(-0.5*(norm(x-y)/s)^2)
%????????????tanh???:? k(x,y) =?tanh(g*x*y+c)
%?? degree - Degree d of polynomial kernel (positive scalar).
%?? offset - Offset c of polynomial and?tanh?kernel (scalar, negative?for?tanh).
%?? width? - Width s of Gauss kernel (positive scalar).
%?? gamma? - Slope g of the?tanh?kernel (positive scalar).
?
% ------------------------------------------------------------%
% 构造两类训练样本
?
n = 50;
randn(state,6);
x1 = randn(2,n);
y1 = ones(1,n);
x2 = 5+randn(2,n);
y2 = -ones(1,n);
?
figure(1);
plot(x1(1,:),x1(2,:),bx,x2(1,:),x2(2,:),k.);
axis([-3 8 -3 8]);
title(C-SVC)
hold on;
?
X = [x1,x2];??????? % 训练样本,d×n的矩阵,n为样本个数,d为样本维数
Y = [y1,y2];??????? % 训练目标,1×n的矩阵,n为样本个数,值为+1或-1
?
% ------------------------------------------------------------%
% 训练支持向量机
?
tic
svm = svmTrain(svc_c,X,Y,ker,C);
svm=C_SVC_Train(X,Y,C,ker);
t_train = toc
?
% svm? 支持向量机(结构体变量)
% the following fields:
%?? type - 支持向量机类型? {
svc_c,svc_nu,svm_one_class,svr_epsilon,svr_nu
}
%?? ker - 核参数
%?? x - 训练样本,d×n的矩阵,n为样本个数,d为样本维数
%?? y - 训练目标,1×n的矩阵,n为样本个数,值为+1或-1
%?? a - 拉格朗日乘子,1×n的矩阵
?
% ------------------------------------------------------------%
% 寻找支持向量
?
a = svm.a;
epsilon = 1e-8;???????????????????? % 如果小于此值则认为是0
i_sv = find(abs(a)epsilon);??????? % 支持向量下标
plot(X(1,i_sv),X(2,i_sv),ro);
?
% ------------------------------------------------------------%
% 测试输出
?
[x1,x2] = meshgrid(-2:0.1:7,-2:0.1:7);
[rows,cols] = size(x1);
nt = rows*cols;????????????????? % 测试样本数
Xt = [reshape(x1,1,nt);reshape(x2,1,nt)];
?
tic
Yd = svmSim(svm,Xt);?????????? % 测试输出
t_sim = toc
?
Yd = reshape(Yd,rows,cols);
contour(x1,x2,Yd,[0 0],m);??? % 分类面
hold off;
您可能关注的文档
最近下载
- 2025烟台南山学院单招《数学》复习提分资料(含答案详解).docx VIP
- 湖南省湘一名校2026届高三上学期12月质量检测物理试卷(含答案).pdf VIP
- 2025年上海高考数学二轮复习:热点题型4 统计与概率(六大题型)原卷版+解析.pdf VIP
- WordExcel2010中文版办公专家从入门到精通.pdf VIP
- 新教材人教A版高中数学选择性必修第三册习题答案.docx VIP
- 专题08 古诗鉴赏(解析版)备战2024年中考语文真题题源解密(全国通用.docx VIP
- 四年级奥数专题加法原理和乘法原理.docx VIP
- 实施指南(2025)《JBT 14627-2023 增材制造 点光源立体光固化工艺规范》.pptx VIP
- JB∕T 14627-2023 增材制造 点光源立体光固化工艺规范.pdf
- 传统拜师礼仪培训课件PPT.pptx VIP
原创力文档

文档评论(0)