libsvm(MATLAB版本)安装与使用

    xiaoxiao2021-03-25  129

    一.下载libsvm

    1.libsvm - 3.22 : https://github.com/cjlin1/libsvm  配套使用MATLAB2013以上,VisualStudio2015以上

    2.libsvm - 3.17 : http://download.csdn.net/download/wzh_xwjh/5648969  配套使用MATLAB2012a,VisualStudio2010

    :这里列的两个安装包是分别针对不同的MATLAB版本和编译器版本使用,否则会出现无法编译的错误。

    二.安装libsvm

    1.使用MATLAB版本的libsvm需要首先安装Visualstudio,安装过程中只需安装VisualC++即可,其他studio可以不用安装;

    2.将libsvm解压后保存到MATLAB文件下的toolbox文件夹,然后打开MATLAB:

    file - setpath - Add with Subfolders - 选中libsvm文件夹所在路径 - save - close

    3. 打开matlab,设置当前路径为C:\Program Files\MATLAB\R2012a\toolbox\libsvm-3.17\matlab, 在command window中输入“mex -setup”,按提示输入y - 1/2/3/... - y

    示例:

    >> mex -setup   Welcome to mex -setup.  This utility will help you set up   a default compiler.  For a list of supported compilers, see   http://www.mathworks.com/support/compilers/R2012a/win64.html  Please choose your compiler for building MEX-files:  Would you like mex to locate installed compilers [y]/n? y Select a compiler:  [1] Microsoft Visual C++ 2010 in g:\VS\  [0] None  Compiler: 1 Please verify your choices:  Compiler: Microsoft Visual C++ 2010   Location: g:\VS\  Are these correct [y]/n? y ***************************************************************************    Warning: MEX-files generated using Microsoft Visual C++ 2010 require             that Microsoft Visual Studio 2010 run-time libraries be              available on the computer they are run on.             If you plan to redistribute your MEX-files to other MATLAB             users, be sure that they have the run-time libraries.  ***************************************************************************  Trying to update options file: C:\Users\liuyuchen\AppData\Roaming\MathWorks\MATLAB\R2012a\mexopts.bat  From template:              C:\PROGRA~1\MATLAB\R2012a\bin\win64\mexopts\msvc100opts.bat    Done . . .  ************************************************************************** 

    4.在command window中输入“make”,完成编译

    三.运行svmtrain与svmpredict

    % 载入软件包数据 load heart_scale.mat;  %如果不加 .mat 程序默认数据为ASCII格式,出错:Error using load Number of columns on line 3 of ASCII file C:\Program Files\MATLAB\R2012a\toolbox\libsvm-3.22\heart_scale must be the same as previous lines.  data = heart_scale_inst; label = heart_scale_label; % 选取前200个数据作为训练集合,后70个数据作为测试集合 ind = 200; traindata = data(1:ind,:); trainlabel = label(1:ind,:); testdata = data(ind+1:end,:); testlabel = label(ind+1:end,:); % 利用训练集合建立分类模型 model = svmtrain(trainlabel,traindata,'-s 0 -t 2 -c 1.2 -g 2.8');

    %参数输入的意义: % -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) % -g r(gama):核函数中的gamma函数设置(针对多项式/rbf/sigmoid核函数) % -c cost:设置C-SVC,e -SVR和v-SVR的参数(损失函数)(默认1) % 分类模型model解密 model Parameters = model.Parameters Label = model.Label nr_class = model.nr_class totalSV = model.totalSV nSV = model.nSV % 利用建立的模型看其在训练集合上的分类效果 [ptrain,accuracy,prob_estimates] = svmpredict(trainlabel,traindata,model); % 预测测试集合标签 [ptest,accuracy_test,prob_estimates_test] = svmpredict(testlabel,testdata,model);  toc;

    %%%%结果

    .* optimization finished, #iter = 350 nu = 0.719833 obj = -88.802168, rho = 0.058346 nSV = 197, nBSV = 13 Total nSV = 197 model =      Parameters: [5x1 double]       nr_class: 2        totalSV: 197            rho: 0.0583          Label: [2x1 double]     sv_indices: [197x1 double]          ProbA: []          ProbB: []            nSV: [2x1 double]        sv_coef: [197x1 double]            SVs: [197x13 double] Parameters =          0     2.0000     3.0000     2.8000          0 Label =      1     -1 nr_class =      2 totalSV =    197 nSV =     89    108 Accuracy = 99.5% (199/200) (classification) Accuracy = 68.5714% (48/70) (classification) Elapsed time is 0.013768 seconds. optimization finished, #iter = 350 nu = 0.719833 obj = -88.802168, rho = 0.058346 nSV = 197, nBSV = 13 Total nSV = 197 model =      Parameters: [5x1 double]       nr_class: 2        totalSV: 197            rho: 0.0583          Label: [2x1 double]     sv_indices: [197x1 double]          ProbA: []          ProbB: []            nSV: [2x1 double]        sv_coef: [197x1 double]            SVs: [197x13 double] Parameters =          0     2.0000     3.0000     2.8000          0 Label =      1     -1 nr_class =      2 totalSV =    197 nSV =     89    108 Accuracy = 99.5% (199/200) (classification) Accuracy = 68.5714% (48/70) (classification) Elapsed time is 0.013768 seconds.

    转载请注明原文地址: https://ju.6miu.com/read-10421.html

    最新回复(0)