svm参数c和g matlab论坛,[转载]关于SVM参数c&g选取的总结帖[matlab-libsvm]

function [bestacc,bestc,bestg] =

SVMcg(train_label,train,cmin,cmax,gmin,gmax,v,cstep,gstep,accstep)

%SVMcg cross validation by faruto

%Email:farutoliyang@gmail.com QQ:516667408

http://blog.sina.com.cn/faruto BNU

%last modified 2009.8.23

%Super Moderator @ www.ilovematlab.cn

%% about the parameters of SVMcg

if nargin < 10

accstep = 1.5;

end

if nargin < 8

accstep = 1.5;

cstep = 1;

gstep = 1;

end

if nargin < 7

accstep = 1.5;

v = 3;

cstep = 1;

gstep = 1;

end

if nargin < 6

accstep = 1.5;

v = 3;

cstep = 1;

gstep = 1;

gmax = 5;

end

if nargin < 5

accstep = 1.5;

v = 3;

cstep = 1;

gstep = 1;

gmax = 5;

gmin = -5;

end

if nargin < 4

accstep = 1.5;

v = 3;

cstep = 1;

gstep = 1;

gmax = 5;

gmin = -5;

cmax = 5;

end

if nargin < 3

accstep = 1.5;

v = 3;

cstep = 1;

gstep = 1;

gmax = 5;

gmin = -5;

cmax = 5;

cmin = -5;

end

%% X:c Y:g cg:acc

[X,Y] = meshgrid(cmin:cstep:cmax,gmin:gstep:gmax);

[m,n] = size(X);

cg = zeros(m,n);

%% record acc with different c & g,and find the

bestacc with the smallest c

bestc = 0;

bestg = 0;

bestacc = 0;

basenum = 2;

for i = 1:m

for j = 1:n

cmd = ['-v ',num2str(v),' -c ',num2str( basenum^X(i,j) ),' -g

',num2str( basenum^Y(i,j) )];

cg(i,j) = svmtrain(train_label, train, cmd);

if cg(i,j) > bestacc

bestacc = cg(i,j);

bestc = basenum^X(i,j);

bestg = basenum^Y(i,j);

end

if ( cg(i,j) == bestacc && bestc

> basenum^X(i,j) )

bestacc = cg(i,j);

bestc = basenum^X(i,j);

bestg = basenum^Y(i,j);

end

end

end

%% to draw the acc with different c & g

[C,h] = contour(X,Y,cg,60:accstep:100);

clabel(C,h,'FontSize',10,'Color','r');

xlabel('log2c','FontSize',10);

ylabel('log2g','FontSize',10);

grid on;

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MATLAB 中使用 libsvm 库进行 SVM 参数 C 和 Gamma 的调优可以使用 `libsvmtrain` 函数和 `libsvmpredict` 函数结合交叉验证来实现。以下是一个示例代码: ```matlab % 加载数据 load fisheriris X = meas(:,3:4); Y = species; % 定义参数搜索范围 Cs = 2.^(-5:15); Gammas = 2.^(-15:3); % 定义交叉验证模型 cv = cvpartition(Y,'KFold',10); % 初始化最优参数 bestC = NaN; bestGamma = NaN; bestAccuracy = 0; % 开始参数搜索 for i = 1:length(Cs) for j = 1:length(Gammas) % 定义 SVM 参数 svm_options = sprintf('-c %g -g %g', Cs(i), Gammas(j)); % 训练模型 model = libsvmtrain(Y, X, svm_options); % 计算交叉验证准确率 accuracy = sum(libsvmpredict(Y(cv.training(i)), X(cv.training(i),:), model)==Y(cv.training(i)))/sum(cv.training(i)); % 更新最优参数 if accuracy > bestAccuracy bestAccuracy = accuracy; bestC = Cs(i); bestGamma = Gammas(j); end end end % 输出最优参数 fprintf('Best C = %g, Best Gamma = %g, Cross-Validation Accuracy = %g\n',bestC,bestGamma,bestAccuracy); ``` 在这个示例代码中,我们加载了鸢尾花数据集,定义了参数搜索范围和交叉验证模型。接着,我们使用双重循环来搜索最优参数,并使用交叉验证来评估每个参数组合的准确率。最后,输出最优参数和交叉验证准确率。 需要注意的是,这个示例代码中使用的是 RBF 核函数。如果你使用的是其他核函数,需要相应地修改 SVM 参数。另外,这个示例代码中使用的是 K-Fold 交叉验证,如果你想使用其他交叉验证方法,也需要相应地修改代码。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值