原文
http://pwwang.com/technology/svm/libsvm-288中文文档tools/
=================================================
這個目錄中包括一些有用的程式:
1. 子集選擇工具
2. 參數選擇工具
3. LIBSVM格式檢查工具
第一部分: 子集選擇工具
介紹
==============
訓練大量的資料需要消耗大量的時間. 有時你需要先對一個小的子集進行訓練. python腳本subset.py可以隨機地選擇一個特定數目的樣本. 對於分類資料, 我們提供一個分層選擇來保證相同的類在一個子集中.
用法: subset.py [options] dataset number [output1] [output2]
這個腳本從給定的資料集中選擇一個子集.
選項:
-s method: 選擇的方式(預設 0)
0 — 分層選擇(只用於分類)
1 — 隨機選擇
output1: 子集(可選)
output2: 餘下的數據(可選)
如果output1未選, 那麼子集將在螢幕上列印出來.
例子
===============
>python subset.py heart_scale 100 file1 file2
從heart_scale中隨機選擇100個樣本保存到file1中,而剩餘的實例保存到file2中.
第二部分: 參數選擇工具
介紹
===============
grid.py是一個參數選擇工具, 這個工具是用於採用RBF(徑向基函數)核的C-SVM分類的. 它採用CV(交叉驗證)技術來估計特定範圍內每一個聯合參數的準確性並且幫助你選擇一個適合你問題的最佳參數.
grid.py為交叉驗證直接執行libsvm二進位檔案(不需要python的結合)並且用gnuplot來畫CV準確性的輪廓. 在使用之前, 你必須事先安裝libsvm和gnuplot. gnuplot可以在這裡下載: http://www.gnuplot.info/
在蘋果系統中, 預編譯的gnuplot檔需要Aquarterm庫的支援, 這個庫也需要安裝. 另外, 這個版本的gnuplot不支持png, 所以你需要修改"set term png transparent small"並使用其他圖片格式. 例如你可以設定為"set term pbm small color".
用法: grid.py [-log2c begin,end,step] [-log2g begin,end,step] [-v fold]
[-svmtrain pathname] [-gnuplot pathname] [-out pathname] [-png pathname]
[additional parameters for svm-train] dataset
這個程式處理v-fold交叉驗證的時候採用的參數C(和gamma) = 2^begin, 2^(begin+step), …, 2^end.
你可以採用-svmtrain和-gnuplot參數來指定libsvm和gnuplot的位置.
對於windows用戶, 請使用gnuplot.exe. 如果你正在使用gnuplot 3.7.1版本, 請升級到3.7.3或更高版本. 3.7.1有一個bug. 如果你使用的是windows下的
cygwin, 請使用gunplot-x11.
例子
===============
> python grid.py -log2c -5,5,1 -log2g -4,0,1 -v 5 -m 300 heart_scale
使用者(特別是MS Windows使用者)可能需要指定可執行檔的路徑. 你既可以在grid.py的開頭修改路徑,也可以在命令列中指定它們. 例如,
> grid.py -log2c -5,5,1 -svmtrain c:\libsvm\windows\svm-train.exe -gnuplot c:\tmp\gnuplot\bin\pgnuplot.exe -v 10 heart_scale
Output: 兩個文件
dataset.png: gnuplot生成的CV準確性輪廓圖
dataset.out: 每一個(log2(C),log2(gamma))的CV準確性
並行網格搜索
===============
把工作分離到一個共用相同檔案系統的電腦集群, 你可以處理一個並行的網格搜索. 首先, 在grid.py中添加機器名字:
ssh_workers = ["linux1", "linux5", "linux5"]
然後設定你的ssh, 使得授權工作不需要詢問密碼.
如果一台機器有多個CPU或者更多的RAM, 相同的機器(例如這裡的linux5)可以列出不只一次. 如果本地機器是最好的, 你也可以擴大nr_local_worker, 例如,
nr_local_worker = 2
例子:
> python grid.py heart_scale
[local] -1 -1 78.8889 (best c=0.5, g=0.5, rate=78.8889)
[linux5] -1 -7 83.3333 (best c=0.5, g=0.0078125, rate=83.3333)
[linux5] 5 -1 77.037 (best c=0.5, g=0.0078125, rate=83.3333)
[linux1] 5 -7 83.3333 (best c=0.5, g=0.0078125, rate=83.3333)
.
.
.
如果 -log2c, -log2g, 或 -v 沒有指定, 則使用預設值.
如果你的系統使用telnet代替ssh, 你應該把你的機器名字在telnet_workers列出.
第三部分: LIBSVM格式檢查工具
介紹
=================
`svm-train’只是對輸入資料進行簡單的檢查. 要做更詳細的檢查, 我們提供一個python腳本 `checkdata.py’.
用法: checkdata.py dataset
這個工具的作者是臺灣大學的Rong-En Fan.
例子
==================
> cat bad_data
1 3:1 2:4
> python checkdata.py bad_data
line 1: feature indices must be in an ascending order, previous/current features 3:1 2:4
Found 1 lines with error.
請先 登入 以發表留言。