Matlab中配置LibSVM 总结
1.參考網(wǎng)站:
libsvm庫(kù)下載:http://www.csie.ntu.edu.tw/~cjlin/libsvm/
視頻:http://v.youku.com/v_showMini/id_XMjc2NTY3MzYw_ft_131.html?
詳解:http://www.matlabsky.com/thread-11925-1-1.html
2.操作流程:
請(qǐng)注意:詳細(xì)操作流程請(qǐng)參考上面的“詳解”網(wǎng)站,這里只說大框架和詳解里沒有提到的問題。
A.設(shè)置path
File->set path ->add with subfolders->加入libsvm-3.11文件夾的路徑
B. 在matlab中編譯
目的:將libsvm-3.11\matlab 中?libsvmwrite.c 等 C++文件編譯成 libsvmread.mexw32 等matlab文件,這樣就可以在command window中被直接調(diào)用了。
注意:在最外面的Readme中有提到已經(jīng)有編譯好的文件,比如在libsvm-3.11\windows中也會(huì)看到libsvmread.mexw32,但這里不要被誤導(dǎo)!還是需要你自己再編譯一遍的!
如果是最新版MATLAB,mex過程有點(diǎn)不一樣,具體如下:
>>mex –setupMEX 配置為使用 'Microsoft Windows SDK 7.1 (C)' 以進(jìn)行 C 語(yǔ)言編譯。 Warning: The MATLAB C and Fortran API has changed to support MATLABvariables with more than 2^32-1 elements. In the near futureyou will be required to update your code to utilize thenew API. You can find more information about this at:http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html. 要選擇不同的語(yǔ)言,請(qǐng)從以下選項(xiàng)中選擇一種命令:mex -setup C++ mex -setup FORTRAN我們要選擇的是C++編譯器:
>> mex -setup C++ MEX 配置為使用 'Microsoft Windows SDK 7.1 (C++)' 以進(jìn)行 C++ 語(yǔ)言編譯。 Warning: The MATLAB C and Fortran API has changed to support MATLABvariables with more than 2^32-1 elements. In the near futureyou will be required to update your code to utilize thenew API. You can find more information about this at:http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html.這樣就可以執(zhí)行make過程了:
>> make 使用 'Microsoft Windows SDK 7.1 (C)' 編譯。 MEX 已成功完成。 使用 'Microsoft Windows SDK 7.1 (C)' 編譯。 MEX 已成功完成。 使用 'Microsoft Windows SDK 7.1 (C++)' 編譯。 找不到 D:\MATLAB\R2014A\toolbox\libsvm-3.21\matlab\svmtrain.exp 找不到 D:\MATLAB\R2014A\toolbox\libsvm-3.21\matlab\svmtrain.exp MEX 已成功完成。 使用 'Microsoft Windows SDK 7.1 (C++)' 編譯。 找不到 D:\MATLAB\R2014A\toolbox\libsvm-3.21\matlab\svmpredict.exp 找不到 D:\MATLAB\R2014A\toolbox\libsvm-3.21\matlab\svmpredict.exp MEX 已成功完成。 >>
C.加載數(shù)據(jù)集
就是這里搞了我一下午!
加載數(shù)據(jù)集
load heart_scale ;
有兩個(gè)數(shù)據(jù)集,一個(gè)是C++的, 一個(gè)是matlab的。libsvm庫(kù)中下載的是C++數(shù)據(jù),
所以matlab加載我們下載的heart_scale是會(huì)報(bào)錯(cuò)的:
法1、下載matlab數(shù)據(jù)集(http://download.csdn.net/detail/abcjennifer/4215779)
法2、用libsvmread而非load.??
這樣就可以加載數(shù)據(jù)集了,完成該步驟后發(fā)現(xiàn)Workspace中出現(xiàn)了heart_scale_inst 和 heart_scale_label,說明正確。ok,下一步我們來測(cè)試svm的訓(xùn)練和predict
D.train & predict
>> [heart_scale_label, heart_scale_inst] = libsvmread('heart_scale'); >> model = libsvmtrain(heart_scale_label, heart_scale_inst, '-c 1 -g 0.07'); * optimization finished, #iter = 134 nu = 0.433785 obj = -101.855060, rho = 0.426412 nSV = 130, nBSV = 107 Total nSV = 130 >> [predict_label, accuracy, dec_values] = libsvmpredict(heart_scale_label, heart_scale_inst, model); Accuracy = 86.6667% (234/270) (classification)
3.總結(jié)
本文借鑒了浙大天才美女http://blog.csdn.net/abcjennifer/article/details/7370177得很多內(nèi)容,主要在補(bǔ)充了新版MATLAB下的一些編譯細(xì)節(jié)。此外,對(duì)于編譯后報(bào)的“沒找到”不用管,不影響。 如果測(cè)試后沒有Accuracy輸出或輸出為空,應(yīng)該采用三個(gè)參量的返回值。具體如下:>> [predict_label, accuracy, dec_values] = libsvmpredict(heart_scale_label, heart_scale_inst, model); Accuracy = 86.6667% (234/270) (classification)再次表示感謝!
與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖
總結(jié)
以上是生活随笔為你收集整理的Matlab中配置LibSVM 总结的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++类内存分布——深度理解继承与虚函数
- 下一篇: VTK修炼之道10:可视化管道的连接与执