|
本帖最后由 faruto 于 2011-8-3 17:28 编辑
【工具箱】
libsvm-3.1-[FarutoUltimate3.1Mcode]
其他名称:libsvm-faruto版本,libsvm-faruto加强工具箱,libsvm-farutoUltimate版本
【更新说明】
libsvm-faruto版本好久都没有更新了,近期我将代码重新整理了一下,看到注释中上次更新代码的时间是2010.01.17,才发现libsvm-faruto版本已经一年多没有添加进行更新,看着自己以前编写的一行行代码,心中不免想起过往那些逝去的岁月,想起过往的那些人,煽情的不多说,此次更新主要内容如下:
1.基于目前的最新的libsvm-3.1编写。
2.对原来的部分代码进行了重新优化。
3.添加ClassResult.m函数,方便给出各种分类准确率,以及给出判别函数的权值w、偏置b、支持向量在原始训练集中的位置索引以及alpha系数。
给出这个函数的目的是方便大家,个人感觉这个函数会对相关朋友有很大帮助。
4.重新编写说明文档TutorialForFarutoUltimate3.1.pdf
由于libsvm回归不支持多输出,本来还想实现libsvm的多输出(lssvm支持多输出,但我详细研究了一下lssvm也就是一维一维的进行回归实现的多输出,libsvm完全可以仿照这种形式实现多输出),但后来又犯懒,因为实在没有技术含量,就搁置了。如果实现有朋友需要就在后期的版本更新中添加吧。另外给大家做个预告,暑假期间我会制作一系列的关于libsvm工具箱和lssvm工具箱的视频,完全免费下载。之前在我的那个专辑期刊中做过预告,这里权当造势啦。O(∩_∩)O
ClassResult.m源代码- function CR = ClassResult(label, data, model, type)
- % by faruto
- % last modified 2011.06.09
- % type 1:输入的data为训练节
- % 2:输入的data为测试集
- % 当输入的data为训练集时,需要事先在外部将标签转换为1,-1
- %%
- if nargin < 4
- type = 1;
- end
- CR = struct();
- %%
- [plabel,acc,dec] = svmpredict(label,data,model);
- %% some info of the data set
- disp('===some info of the data set===')
- str = sprintf('#class is %d',model.nr_class);
- disp(str)
- lstr = ['类别标签为 '];
- for i = 1:size(model.Label,1)
- lstr = [lstr, num2str(model.Label(i)),' '];
- end
- disp(lstr);
- if type == 1
- str = ...
- sprintf('支持向量数目 %d,所占训练集样本数目比例 %g%% (%d/%d)',model.totalSV,model.totalSV/size(data,1)*100,model.totalSV,size(data,1));
- disp(str);
- ratio = model.totalSV/size(data,1)*100;
- if ratio >= 80
- disp('Tips:支持向量数目所占比例过大(>=80%),可以考虑重新优化参数')
- end
- else
- str = ...
- sprintf('支持向量数目 %d',model.totalSV);
- disp(str);
- end
- %% 各种分类准确率
- disp('===各种分类准确率===');
- CR.accuracy = zeros(1,size(model.Label,1)+1);
- CR.accuracy(1) = sum(plabel == label)/numel(label);
- str = ...
- sprintf('整体分类准确率 = %g%% (%d/%d)',CR.accuracy(1)*100,sum(plabel == label),numel(label));
- disp(str)
- for i = 1:numel(model.Label)
- p = 0;
- n = 0;
- for run = 1:numel(label)
- if label(run) == model.Label(i)
- if plabel(run) == model.Label(i)
- p = p + 1;
- else
- n = n + 1;
- end
- end
- end
- CR.accuracy(i+1) = p/(p+n);
- str = ...
- sprintf('第 %d 类分类准确率 = %g%% (%d/%d)',model.Label(i),CR.accuracy(i+1)*100,p,p+n);
- disp(str);
- end
- %%
- if type == 1
- [tmp index]=ismember(model.SVs,data,'rows');
- CR.SVlocation = index;
- end
- %%
- if numel(model.Label) == 2 && type == 1
- CR.b = -model.rho;
- CR.w = model.sv_coef;
- CR.alpha = zeros(size(model.sv_coef,1),1);
- for i = 1:size(model.sv_coef,1)
- CR.alpha(i) = CR.w(i)/label(CR.SVlocation(i));
- end
- end
复制代码 ClassResult.m函数测试代码- %% ClassResult_test
- % by faruto
- %% a litte clean work
- tic;
- close all;
- clear;
- clc;
- format compact;
- %%
- % load wine_test;
- % label = train_data_labels;
- % data = train_data;
- load heart_scale;
- data = heart_scale_inst;
- label = heart_scale_label;
- model = svmtrain(label,data);
- %%
- type = 1;
- CR = ClassResult(label, data, model, type)
- %%
- toc;
复制代码 测试结果- Accuracy = 86.6667% (234/270) (classification)
- ===some info of the data set===
- #class is 2
- 类别标签为 1 -1
- 支持向量数目 132,所占训练集样本数目比例 48.8889% (132/270)
- ===各种分类准确率===
- 整体分类准确率 = 86.6667% (234/270)
- 第 1 类分类准确率 = 80.8333% (97/120)
- 第 -1 类分类准确率 = 91.3333% (137/150)
- CR =
- accuracy: [0.8667 0.8083 0.9133]
- SVlocation: [132x1 double]
- b: -0.4245
- w: [132x1 double]
- alpha: [132x1 double]
- Elapsed time is 0.023713 seconds.
- >>
复制代码 【工具箱下载】
libsvm-3.1-[FarutoUltimate3.1Mcode].rar
(1.16 MB, 下载次数: 700061)
|
评分
-
查看全部评分
|