多元分类SVM(多类别SVM)程序使用说明

目录

操作流程

首先,在这个网站上下载libsvm程序包http://download.csdn.net/detail/qy20115549/9608108。下载之后,将其放到某一目录下,点击菜单栏中的设置路径(set path)。将该文件夹添加到path中,这样其他文件夹,便可直接调用了。

程序的数据格式

如下程序中,测试集为testData,其中包含test test_labels两组数据。test是用excel导入的数据,导入格式成矩阵,存放的是多维自变量(X),test_labels导入的是列矢量,存放自变量对应的标签类别(Y),注意类别必须用1,2,3等数字表示。
同理,训练集trainData我就不介绍了。

matlab程序

%保存训练集测试集
save testData test test_labels;
save trainData train train_labels;
%加载数据
load testData;
load trainData;
%数值标准化转化到0-1区间
[train,pstrain]=mapminmax(train');
pstrain.ymin=0;
pstrain.ymax=1;
[train,pstrain]=mapminmax(train,pstrain);
[test,pstest]=mapminmax(test');
pstest.ymin=0;
petest.ymax=1;
[test,pstest]=mapminmax(test,pstest);
train=train';
test=test';
%直接训练,不寻找最优,很快得到结果
%%训练模型
model = svmtrain(train_labels, train);
%在测试集中检验模型的精度
[predict_label,accuracy] = svmpredict(test_labels, test, model); 
%对于分类问题网格参数优化(c,g),类别越多越慢。
[bestacc,bestc,bestg] = SVMcgForClass(train_labels,train,-5,5,-5,5,7,0.5,0.5,4.5)%%交叉验证求c,g
cmd=['-c ',num2str(bestc),' -g ',num2str(bestg)]        %%c,g均有空格
%训练建模
%model=svmtrain(train_labels,train,cmd);
%% SVM网络预测
%[ptest_label, test_accuracy] = svmpredict(test_labels, test, model);

%对于分类问题利用PSO 来进行参数优化(c,g),一般类别比较多的情况下,很慢
pso_option.c1 = 0.5;
pso_option.c2 = 0.5;
pso_option.maxgen = 200;
pso_option.sizepop = 50;
pso_option.k = 0.6;
pso_option.wV = 1;
pso_option.wP = 1;
pso_option.v = 7;
pso_option.popcmax =200;
pso_option.popcmin = 0.1;
pso_option.popgmax = 100;
pso_option.popgmin = 0.001;
%寻优的过程
[bestacc1,bestc1,bestg1,pso_option] = psoSVMcgForClass(train_labels,train,pso_option)
cmd1= ['-c ',num2str(bestc1),' -g ',num2str(bestg1)];
model1 = svmtrain(train_labels, train,cmd1);
% [train_label1,train_accuracy1]=svmpredict(train_labels, train, model1);
%得到精度
[ptest_label1, test_accuracy1] = svmpredict(test_labels, test, model1);
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值