Liblinear自带多种软件接口,本篇中使用的是Liblinear文件夹下的matlab文件夹中的内容,目的是将用C编写的文件编译成matlab可调用的程序。
一、准备工作
Liblinear的下载地址http://www.csie.ntu.edu.tw/~cjlin/liblinear/
我使用的是最新版本liblinear-1.93
matlab使用版本的是R2011b
二、Liblinear的安装过程
1.在matlab中设子路径。打开matlab,点击File->SetPath->Add withSubfolders,找到liblinear-1.93\matlab所在文件夹,点击save,点击close。
2.在matlab的command window中输入mex -setup,选择合适的编译器,具体如下(其中绿字部分是需要手动输入的,其他是自动生成的):
K>> 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/R2011b/win32.html
Please choose your compiler for building MEX-files:
Would you like mex to locate installed compilers [y]/n? y
Select a compiler:
[1] Lcc-win32 C 2.4.1 in C:\PROGRA~1\MATLAB\R2011b\sys\lcc
[2] Microsoft Visual C++ 2010 in F:\应用程序\VISUAL STUDIO
[0] None
Compiler: 2
Please verify your choices:
Compiler: Microsoft Visual C++ 2010
Location: F:\应用程序\VISUAL STUDIO
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\william\AppData\Roaming\MathWorks\MATLAB\R2011b\mexopts.bat
From template: C:\PROGRA~1\MATLAB\R2011b\bin\win32\mexopts\msvc100opts.bat
Done . . .
**************************************************************************
Warning: The MATLAB C and Fortran API has changed to support MATLAB
variables with more than 2^32-1 elements. In the near future
you will be required to update your code to utilize the new
API. You can find more information about this at:
http://www.mathworks.com/support/solutions/en/data/1-5C27B9/?solution=1-5C27B9
Building with the -largeArrayDims option enables the new API.
**************************************************************************
3.修改当前路径。将matlab的current folder的目录该成liblinear-1.93\matlab所在文件夹
4.在matlab的command window中输入make,如果正确的话,会如下所示:
K>> make
在当前目录下生成下图中的文件
特别是生成train.mexw32和predict.mexw32.
到这一步,Liblinear安装成功
三、在matlab中测试Liblinear是否能用
model = train(svm_category', sparseTrainMatrix)
[output_label, accuracy] = predict(svm_category', sparseTestMatrix, model);
train中参数:svm_category'为列向量,表示training set的class label,假设为n维
sparseTrainMatrix为n*m维矩阵,n表示training set中样本的个数,m表示每个样本中feature的个数
model的内容为
predict中参数和train中取的类似,本例中直接把training set作为testing set重新预测了一下,model为train中输出的结构体
output_label输出的是testing set的class label的预测值
accuracy是预测准确度
到此结束。(注:具体数据可参考Andrew Ng的CS229课程problem set2的第二题)