Matlab的libsvm的安装

最关键的是compilers的选择(对于把Microsoft visual stdio 2005或者其他的编译器安装在自定义目录下的这一步非常关键)  以下是步骤:
>> mex -setup % 这是必须的
Please choose your compiler for building external interface (MEX) files: 

Would you like mex to locate installed compilers [y]/n? %这次是选择编译器,输入n,因为你的是自定义的
%出现下面的选项:
Select a compiler: 
[1] Borland C++ Compiler (free command line tools) 5.5 
[2] Borland C++Builder 6.0 
[3] Borland C++Builder 5.0 
[4] Compaq Visual Fortran 6.1 
[5] Compaq Visual Fortran 6.6 
[6] Intel C++ 9.1 (with Microsoft Visual C++ 2005 linker) 
[7] Intel Visual Fortran 9.1 (with Microsoft Visual C++ 2005 linker) 
[8] Intel Visual Fortran 9.0 (with Microsoft Visual C++ 2005 linker) 
[9] Intel Visual Fortran 9.0 (with Microsoft Visual C++ .NET 2003 linker) 
[10] Lcc-win32 C 2.4.1 
[11] Microsoft Visual C++ 6.0 
[12] Microsoft Visual C++ .NET 2003 
[13] Microsoft Visual C++ 2005 
[14] Microsoft Visual C++ 2005 Express Edition 
[15] Open WATCOM C++ 1.3 

[0] None 
Compiler: 13%这次选择13,或许你也可以用其他的
The default location for Microsoft Visual C++ 2005 compilers is C:\Program Files\Microsoft Visual Studio 8, 
but that directory does not exist on this machine.  

Use C:\Program Files\Microsoft Visual Studio 8 anyway [y]/n? %这次它说要使用默认的路径  继续选择n
Please enter the location of your compiler: [C:\Program Files\Microsoft Visual Studio 8] %它的意思是让你输入编译器的位置,输入:D:\Program Files\Microsoft Visual Studio 8
% 下面就是确认了
Please verify your choices: 

Compiler: Microsoft Visual C++ 2005  
Location: D:\Program Files\Microsoft Visual Studio 8 

Are these correct?([y]/n): y
*************************************************************************** 
  Warning: MEX-files generated using Microsoft Visual C++ 2005 require 
           that Microsoft Visual Studio 2005 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. 
           You can find more information about this at: 
           http://www.mathworks.com/support/solutions/data/1-2223MW.html 
*************************************************************************** 

Trying to update options file: C:\Documents and Settings\Administrator\Application Data\MathWorks\MATLAB\R2007a\mexopts.bat 
From template:              D:\PROGRA~2\MATLAB\R2007a\bin\win32\mexopts\msvc80opts.bat 

Done . . . %   ok,done!
>> make
>> 
%至此彻底完成,
%以下是测试
>>load heart_scale.mat
>>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); % 
Accuracy = 86.6667% (234/270) (classification)%  done

 

这个帖子很完整的讲了如何使用mex命令安装libsvm。但是当你看完这个帖子之后,你可能还是无法安装成功(比如我之前就因为无法安装而发帖求助),下面我来补充一下在学会使用mex命令之后可能会出现的问题。
第一:无法找到compiler
>> make
  Error: Could not find the compiler "cl" on the DOS path. 
         Use mex -setup to configure your environment properly. 


  E:\BIN\MEX.PL: Error: Unable to locate compiler. 

这是因为你的电脑里没有vc编译器(compiler)或者你没有把vc编译器的路径指定正确。遇到这种情况大家可能会直接复制
Select a compiler: 
[1] Intel C++ 11.1 (with Microsoft Visual C++ 2008 SP1 linker) 
[2] Intel Visual Fortran 11.1 (with Microsoft Visual C++ 2008 SP1 linker) 
[3] Intel Visual Fortran 11.1 (with Microsoft Visual C++ 2008 Shell linker) 
[4] Lcc-win32 C 2.4.1 
[5] Microsoft Visual C++ 6.0 
[6] Microsoft Visual C++ 2005 SP1 
[7] Microsoft Visual C++ 2008 SP1 
[8] Microsoft Visual C++ 2010 
[9] Microsoft Visual C++ 2010 Express 
[10] Open WATCOM C++ 
这里的某个选项的名称,去百度,然后下载 ,然后发现安装,然后运行 make,然后依然失败。你发现,你在“添加删除程序里”可以找到Microsoft Visual C++ 2005 SP1 ,但是,你没办法找到它的位置。
这是因为你下载的不是编译器(compiler)而是运行库!!!!!请注意,如果你没有compiler,那么请下载visual studio 系列,比如visual studio 2010,这才是编译器。visual studio都比较大,通常2G多,如果你下载的只有几M,那么你下载的可能不是编译器。


第二,安装了visual  studio,libsvm依然无法安装。
这可能是你的visual studio版本过低的缘故。类似问题可能出现在win7用户上。本人的matlab版本也比较高(matlab 2010b)之前用visual studio 2008,在warning的提示里显示,有两个tools没有按规格安装,大概意思就是不适合64为的系统,于是我删除了vs 2008,下载了vs 2010。这样安装libsvm就成功了。
在这里,我强烈建议,如果你系统是win7,如果你的matlab是2010b及更高版本,那么请使用visual studio 2010及更高版本的编译器。

第三,如何知道我的libsvm安装成功了呢?
(1)make之后没有报错就成功来了
(2)其实make的过程就是创建matlab可以识别的新文件的过程,当你打开libsvm 3.1的文件夹,同时你运行make,你会发现,原有类型为C Source的文件下,生成了类型为MEXW64的新文件。
(3)当然,可能你没有注意到这个过程,更直白的方法是运行这两个命令,看看是否可以运行(如:再论Matlab的libsvm的安装中讲的一样):
>>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);
但是你发现无法load heart_scale.mat,比如http://www.ilovematlab.cn/thread-144063-1-1.html
那是因为在这个libsvm3.1的工具箱里没有heart_scale这个数据集,再论Matlab的libsvm的安装里用的是另一个版本的libsvm,那个文件夹里是有这个数据集的,所以他可以load之后,运行上面的两个命令,所以,各位可以下载其他版本的libsvm,里面有这个heart_scale,mat,安装完libsvm3.1之后,file——open——找到heart_scale.mat,打开,然后你workspace里就有这个数据集了,之后你才可以运行
>>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);

第四,我的matlab里有svmtrain啊!!
的确,你的matlab里有svmtrain这个函数,你运行 help svmtrain 之后可以研究一下它的用法,但这个svmtrain是matalb自带的,不是libsvm里的。
如果你购买的是《matlab神经网络30个案例分析》那么你在运行12章程序的时候会发生如下问题
??? Error using ==> svmtrain at 172
Group must be a vector
你安装libsvm之后,在libsvm工具箱之下运行matlab,这时你使用的才是libsvm工具箱。这时你打开12章的那个数据集,然后你的workspace里有了相应数据之后,再运行12章的程序,才能顺利运行。运行结果是:
optimization finished, #iter = 77
nu = 0.144033
obj = -9.505833, rho = 0.384005
nSV = 37, nBSV = 0
*.*
optimization finished, #iter = 61
nu = 0.105301
obj = -5.580687, rho = 0.054779
nSV = 32, nBSV = 0
.*
optimization finished, #iter = 90
nu = 0.198508
obj = -11.897346, rho = -0.318369
nSV = 44, nBSV = 1
Total nSV = 66
Accuracy = 97.7528% (87/89) (classification)
安装了libsvm之后运行成功。

希望以上内容可以对大家有帮助!

posted @ 2015-12-09 10:02  夕月一弯  阅读(571)  评论(0编辑  收藏  举报