您的位置:首页 > 编程语言 > MATLAB

在matlab下测试libsvm工具箱

2015-08-29 23:14 609 查看

一、libsvm工具箱的简介

SVM由Vapnik首先提出(Boser,Guyon and Vapnik,1992;Cortes and Vapnik,1995;Vapnik,1995,1998)。

SVM的主要思想是建立一个超平面作为决策曲面,使得正例和反例之间的隔离边缘被最大化。

SVM的优点:

1、通用性(能够在各种函数集中构造函数)

2、鲁棒性(不需要微调)

3、有效性(在解决实际问题中属于最好的方法之一)

4、计算简单(方法的实现只需要利用简单的优化技术)

5、理论上完善(基于VC推广理论的框架)

SVM工具箱:种类很多,公认的最好用的是libsvm by 林智仁[台大] (http://www.csie.ntu.edu.tw/~cjlin/libsvm/index.html)

二、Libsvm-matlab工具箱的安装

1、给matlab指定编译器mex –setup %let you select or change the compiler configuration



2、make将c写的文件转换成matlab相应的接口

make的结果



目的:将libsvm-3.11\matlab 中 libsvmwrite.c 等 C++文件编译成 libsvmread.mexw32 等matlab文件,这样就可以在command window中被直接调用了。

3、注意事项

(1)、安装时需要把libsvm-mat作为当前的工作目录,在后面使用时并将其加为工作目录(setpath…)。





(2)、电脑里需要有相应的编译器,可以用自带的,最好装一个VC。

4、svmtrain函数相关参数说明

%通过训练集来训练模型

svmtrain( … );

model =

svmtrain(train_label, train_matrix, [‘libsvm_options’]);

-train_label:

An m by 1 vector of training labels (type must be double).

-train_matrix:

An m by n matrix of m training instances with n features.

It can be dense or sparse (type must be double).

-libsvm_options:

A string of training options in the same format as that of LIBSVM.

-libsvm_options:

A string of training options in the same format as that of LIBSVM.

有关libsvm的参数选项:

Options:可用的选项即表示的涵义如下

  -s svm类型:SVM设置类型(默认0)

  0 – C-SVC

  1 –v-SVC

  2 –一类SVM

  3 – e -SVR

  4 – v-SVR

  -t 核函数类型:核函数设置类型(默认2)

  0 – 线性:u’v

  1 – 多项式:(r*u’v + coef0)^degree

  2 – RBF函数:exp(-r|u-v|^2)

  3 –sigmoid:tanh(r*u’v + coef0)

  -d degree:核函数中的degree设置(针对多项式核函数)(默认3)

  -g r(gama):核函数中的gamma函数设置(针对多项式/rbf/sigmoid核函数)(默认1/ k)

  -r coef0:核函数中的coef0设置(针对多项式/sigmoid核函数)((默认0)

  -c cost:设置C-SVC,e -SVR和v-SVR的参数(损失函数)(默认1)

  -n nu:设置v-SVC,一类SVM和v- SVR的参数(默认0.5)

  -p p:设置e -SVR 中损失函数p的值(默认0.1)

  -m cachesize:设置cache内存大小,以MB为单位(默认40)

  -e eps:设置允许的终止判据(默认0.001)

  -h shrinking:是否使用启发式,0或1(默认1)

  -wi weight:设置第几类的参数C为weight?C(C-SVC中的C)(默认1)

  -v n: n-fold交互检验模式,n为fold的个数,必须大于等于2

其中-g选项中的k是指输入数据中的属性数。option -v 随机地将数据剖分为n部分并计算交互检验准确度和均方根误差。以上这些参数设置可以按照SVM的类型和核函数所支持的参数进行任意组合,如果设置的参数在函数或SVM类型中没有也不会产生影响,程序不会接受该参数;如果应有的参数设置不正确,参数将采用默认值。

The ‘svmtrain’ function returns a model which can be used for future prediction.

5、svmpredict函数相关参数说明

%对测试集进行预测

svmpredict( … );

[predicted_label, accuracy, decision_values/prob_estimates]

= svmpredict(test_label, test_matrix, model, [‘libsvm_options’]);

-test_label:

An m by 1 vector of prediction labels. If labels of test

data are unknown, simply use any random values. (type must be double)

-testmatrix:

An m by n matrix of m testing instances with n features.

It can be dense or sparse. (type must be double)

-model:

The output of svmtrain.

-libsvm_options:

A string of testing options in the same format as that of LIBSVM.

decision_values/prob_estimates:回归相关问题中才会涉及到

三、试验结果

[heart_scale_label,heart_scale_inst]=libsvmread(‘heart_scale’);

model = svmtrain(heart_scale_label,heart_scale_inst, ‘-c 1 -g 0.07’);

[predict_label, accuracy, dec_values] =svmpredict(heart_scale_label, heart_scale_inst, model); % test the trainingdata

如果出现一行:Accuracy = 86.6667% (234/270) (classification)。就说明成功了。就可以在matlab中使用svm了。

作者:Jacky_Ponder,转载或分享请注明出处。QQ:2814152689
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  matlab svm