LibSvm 使用说明 学习心得.docx
《LibSvm 使用说明 学习心得.docx》由会员分享,可在线阅读,更多相关《LibSvm 使用说明 学习心得.docx(15页珍藏版)》请在冰豆网上搜索。
LibSvm使用说明学习心得
LibSvm使用说明学习心得
(本页内容来自互联网,如果对您的利益造成侵害,请通知我,我会立即删除!
)
Viewthistutorialin:
EnglishOnlyTraditionalChineseOnlyBoth(Default)(req.JavaScriptifyouwanttoswitchlanguages)
CoreStyleSheets:
ChocolateMidnightModernistOldstyleSteelySwissTraditionalUltramarine
*Thisdocumentiswritteninmultilingualformat.Westronglysuggestthatyouchooseyourlanguagefirsttogetabetterdisplay.
piaip'sUsing(lib)SVMTutorialpiaip的(lib)SVM简易入门
piaipatcsiedotntudotedudottw,
Hung-TeLin
FriApr1815:
04:
53CST2003
$Id:
svm_tutorial.html,v1.122005/10/2606:
12:
40piaipExppiaip$原作:
林弘德,转载请保留原出处
Whythistutorialishere
我一直觉得SVM是个很有趣的东西,不过也一直没办法(mostly冲堂)去听林智仁老师的Datamining跟SVM的课;后来看了一些网络上的文件跟听kcwu讲了一下libsvm的用法后,就想整理一下,算是对于并不需要知道完整SVM理论的人提供使用libsvm的入门。
原始libsvm的README跟FAQ也是很好的文件,不过你可能要先对svm跟流程有点了解才看得懂(我在看时有这样的感觉);这篇入门就是为了从零开始的人而写的。
I'vebeenconsideringSVMasaninterestingandusefultoolbutcouldn'tattendthe"DataminingandSVM"coursebyprof.cjlineaboutit(mostlyduetoschedulingconflicts).Afterreadingsomematerialsontheinternetanddiscussinglibsvmwithsomeofmyclassmatesandfriends,IwantedtoprovidesomenoteshereasatutorialforthosewhodonotneedtoknowthecompletetheorybehindSVMtheorytouselibsvm.TheoriginalREADMEandFAQfilesthatcomeswithlibsvmaregooddocumentstoo.ButyoumayneedtohavesomebasicknowledgeofSVManditsworkflow(that'showIfeltwhenIwasreadingthem).Thistutorialisspecificlyforthosestartingfromzero.
后来还有一些人提供意见,所以在此要感谢:
Imustthanktheseguyswhoprovidedfeedbackandhelpedmemakethistutorial:
kcwu,biboshen,puffer,somi
不过请记得底下可能有些说法不一定对,但是对于只是想用SVM的人来说我觉得这样说明会比较易懂。
Rememberthatsomeaspectbelowmaynotbecorrect.Butforthosewhojustwishto"USE"SVM,Ithinktheexplanationbelowiseasiertounderstand.
这篇入门原则上是给会写基本程序的人看的,也是给我自己一个备忘,不用太多数学底子,也不用对SVM有任何先备知识。
Thistutorialisbasicallyforpeoplewhoalreadyknowhowtoprogram.It'salsoamemotomyself.NeithertoomuchmathmaticsnorpriorSVMknowledgeisrequired.
还看不懂的话有三个情形,一是我讲的不够清楚,二是你的常识不足,三是你是小白^^;Ifyoustillcan'tunderstandthistutorial,therearethreepossibilities:
1.Ididn'texplainclearlyenough,2.Youlacksufficientcommonknowledge,3.Youdon'tuseyourbrainproperly^^;
我自己是以完全不懂的角度开始的,这篇入门也有不少一样不懂SVM的人看过、而且看完多半都有一定程度的理解,所以假设情况一不会发生,那如果不懂一定是后两个情况:
P也所以,有问题别问我。
SinceIbeginwritingthismyselfwithnounderstandingofthesubject,ansthisdocumenthasbeenreadbymanypeoplewhoalsodidn'tunderstandSVMbutgainedacertainlevelofunderstandingafterreadingit,possibility1canberuledout.Thusifyoucan'tunderstandityoumustbelongtothelattertwocategories,:
Pthusevenifyouhaveanyquestionsafterreadingthis,don'taskme.
SVM:
Whatisitandwhatcanitdoforme?
SVM,SupportVectorMachine,简而言之它是个起源跟类神经网络有点像的东西,不过现今最常拿来就是做分类(classification)。
也就是说,如果我有一堆已经分好类的东西(可是分类的依据是未知的!
),那当收到新的东西时,SVM可以预测(predict)新的数据要分到哪一堆去。
SVM,SupportVectorMachine,issomethingthathassimilarrootswithneuralnetworks.ButrecentlyithasbeenwidelyusedinClassification.Thatmeans,ifIhavesomesetsofthingsclassified(ButyouknownothingaboutHOWICLASSIFIEDTHEM,orsayyoudon'tknowtherulesusedforclassification),whenanewdatacomes,SVMcanPREDICTwhichsetitshouldbelongto.
听起来是很神奇的事(如果你觉得不神奇,请重想一想这句话代表什么:
分类的依据是未知的!
,还是不神奇的话就请你写个程序解解看这个问题),也很像要AI之类的高等技巧...不过SVM基于统计学习理论可以在合理的时间内漂亮的解决这个问题。
ItsoundsmarvelousandwouldseemtorequireadvancedtechniqueslikeAIsearchingorsometime-consumingcomplexcomputation.ButSVMusedsomeStatisticalLearningTheorytosolvethisprobleminreasonabletime.
以图形化的例子来说明(bySVMToy),像假定我在空间中标了一堆用颜色分类的点,点的颜色就是他的类别,位置就是他的数据,那SVM就可以找出区隔这些点的方程式,依此就可以分出一区区的区域;拿到新的点(数据)时,只要对照该位置在哪一区就可以(predict)找出他应该是哪一颜色(类别)了:
Nowweexplainwithagraphicalexample(bySVMToy),Imarkedlotsofpointswithdifferentcolorsonaplane,thecolorofeachpointisits"class"andthelocationisitsdata.SVMcanthenfindequationstosplitthesepointsandwiththeseequationswecangetcoloredregions.Whenanewpoint(data)comes,wecanfind(predict)whatcolor(class)apointshouldbejustbyusingthepoint'slocation(data)
原始资料分布OriginalData
SVM找出来的区域SVMRegions
当然SVM不是真的只有画图分区那么简单,不过看上面的例子应该可以了解SVM大概在作什么.OfcourseSVMisnotreallyjustaboutpaintingandmarkingregions,butwiththeexampleaboveyoushouldshouldbeabletogetsomeideaaboutwhatSVMisdoing.
要对SVM再多懂一点点,可以参考cjlin在datamining课的slides:
pdforps。
底下我试着在不用看那个slide的情况解释及使用libsvm。
TogetyourselfmorefamiliarwithSVM,youmayrefertotheslidescjlinusedinhisDataMiningcourse:
pdforps.
I'mgoingtotrytoexplainanduselibSVMwithoutthoseslides.
所以,我们可以把SVM当个黑盒子,数据丢进去让他处理然后我们再来用就好了.ThuswecanconsiderSVMasablackbox.JustpushdataintoSVMandusetheoutput.
HowdoIgetSVM?
林智仁(cjlin)老师的libsvm当然是最完美的工具.Chih-JenLin'slibsvmisofcoursethebesttoolyoucaneverfind.
Downloadlibsvm
下载处:
DownloadLocation:
libsvm.ziporlibsvm.tar.gz
.zip跟.tar.gz基本上是一样的,只是看你的OS;习惯上Windows用.zip比较方便(因为有WinZIP,不过我都用WinRAR),UNIX则是用.tar.gzContentsinthe.zipand.tar.gzarethesame.PeopleusingWindowsusuallyliketouse.zipfilesbecausetheyhaveWinZIP,whichIalwaysreplacewithWinRAR.UNIXusersmostlyprefer.tar.gz
Buildlibsvm
解开来后,假定是UNIX系统,直接打make就可以了;编不出来的话请详读说明和运用常识.因为这是tutorial,所以我不花时间细谈,而且会编不出来的情形真是少之又少,通常一定是你的系统有问题或你太笨了.其它的子目录可以不管,只要svm-train,svm-scale,svm-predict三个执行档有编出来就可以了.Afteryouextractedthearchives,justtypemakeifyouareusingUNIX.Youmayignoresomeofthesubdirectories.Weonlyneedtheseexecutablefiles:
svm-train,svm-scale,andsvm-predict
Windows的用户要自己重编当然也是可以,不过已经有编好的binary在里面了:
请检查windows子目录,应该会有svmtrain.exe,svmscale.exe,svmpredict.exe,svmtoy.exe.Windowsusersmayrebuildfromsourceifyouwant,butthere'realreadysomeprebuiltbinariesinthearchive:
justcheckyour"windows"subdirectoryandyoushouldfindsvmtrain.exe,svmscale.exe,svmpredict.exe,andsvmtoy.exe.
UsingSVM
libsvm有很多种用法,这篇tutorial只打算讲简单的部分.libsvmhaslotsoffunctions.Thistutorialwillonlyexplaintheeasierparts(mostlyclassificationwithdefaultmodel).
Theprograms
解释一下几个主要执行档的作用:
(UNIX/Windows下檔名稍有不同,请用常识理解我在讲哪个)I'mgoingtodescribehowtousethemostimportantexecutableshere.ThefilenamesarealittlebitdifferentunderUnixandWindows,applycommonsensetoseewhichI'mreferringto.
svmtrain
Train(训练)data.跑SVM被戏称为"开火车"也是由于这个程序名而来.train会接受特定格式的输入,产生一个"Model"檔.这个model你可以想象成SVM的内部数据,因为predict要model才能predict,不能直接吃原始数据.想想也很合理,假定train本身是很耗时的动作,而train好可以以某种形式存起内部数据,那下次要predict时直接把那些内部数据load进来就快多了.Useyourdatafortraining.RunningSVMisoftenreferredtoas'drivingtrains'byitsnon-nativeEnglishspeakingauthorsbecauseofthisprogram.svmtrainacceptssomespecificallyformatwhichwillbeexplainedbelowandthengeneratea'Model'file.Youmaythinkofa'Model'asastorageformatfortheinternaldataofSVM.Thisshouldappearveryreasonableaftersomethought,sincetrainingwithdataisatime-consumingprocess,sowe'train'firstandstoretheresultenablingthe'predict'operationtogomuchfaster.
svmpredict
依照已经train好的model,再加上给定的输入(新值),输出predict(预测)新值所对应的类别(class).Outputthepredictedclassofthenewinputdataaccordingtoapre-trainedmodel.
svmscale
Rescaledata.因为原始数据可能范围过大或过小,svmscale可以先将数据重新scale(缩放)到适当范围.Rescaledata.Theoriginaldatamaybetoohugeorsmallinrange,thuswecanrescalethemtotheproperrangesothattrainingandpredictingwillbefaster.
FileFormat
档案格式要先交代一下.你可以参考libsvm里面附的"heart_scale":
ThisistheinputfileformatofSVM.Youmayalsorefertothefile"heart_scale"whichisbundledinofficiallibsvmsourcearchive.
[label][index1]:
[value1][index2]:
[value2]...
[label][index1]:
[value1][index2]:
[value2]...
.
.
一行一笔资料,如Onerecordperline,as:
+11:
0.7082:
13:
14:
-0.3205:
-0.1056:
-1
label
或说是class,就是你要分类的种类,通常是一些整数。
Sometimesreferredtoas'class',theclass(orset)ofyourclassification.Usuallyweputintegershere.
index
是有顺序的索引,通常是放连续的整数。
Orderedindexes.usuallycontinuousintegers.
value
就是用来train的数据,通常是一堆实数。
Thedatafortraining.Usuallylotsofreal(floatingpoint)numbers.
每一行都是如上的结构,意思就是:
我有一排资料,分别是value1,value2,....valueN,(而且它们的顺序已由indexN分别指定),这排数据的分类结果就是label。
Eachlinehasthestructuredescribedabove.Itmeans,Ihaveanarray(vector)ofdata(numbers):
value1,value2,....valueN(andtheorderofthevaluesarespecifiedbytherespectiveindex),andtheclass(ortheresult)ofthisarrayislabel.
或许你会不太懂,为什么会是value1,value2,....这样一排呢?
这牵涉到SVM的原理。
你可以这样想(我没说这是正确的),它的名字就叫Support"Vector"Machine,所以输入的trainingdata是"Vector"(向量),也就是一排的x1,x2,x3,...这些值就是valueN,而x[n]的n就是由indexN指定。
这些东西又称为"attribute"。
真实的情况是,大部份时候我们给定的数据可能有很多"特征(feature)"或说"属性(attribute)",所以输入会是一组的。
举例来说,以前面画点分区的例子来说,我们不是每个点都有X跟Y的坐标吗?
所以它就有两种attribute。
假定我有两个点:
(0,3)跟(5,8)分别在label(class)1跟2,那就会写成11:
02:
3
21:
52:
8
同理,空间中的三维坐标就等于有三组attribute。
Maybeit'sconfusingtoyou:
whyvalue,value2,...?
Thereasonisusuallytheinputdatatotheproblemyouweretryingtosolveinvolveslotsof'features',orsay'attributes',sotheinputwillbeaset(orsayvector/array).TaketheMarkingpointsandfindregionexampledescribedabove,weassumedeachpointhascoordinatesXandYsoithastwoattributes(XandY).Todescribetwopoints(0,3)and(5