天天看點

Libsvm使用說明(三)

  • Function: double svm_predict(const struct svm_model *model,

    const struct svm_node *x);

    這個函數為一個測試向量x在提供的一個模式下做分類或回歸。

    對于分類模式,傳回的是x被預測的類。

    對于回歸模式,傳回的是用該模式計算x的函數值。

    對于 one-class 模式,傳回 +1 或 -1 。

  • Function: void svm_cross_validation(const struct svm_problem *prob,

    const struct svm_parameter *param, int nr_fold, double *target);

    這個函數執行交叉驗證。資料被分成 nr_fold 折。在已設的參數下,按順序用剩下的訓練集的訓練出的模 式來驗證每一折。驗證過程中預測的 labels (所有可能的實體)被儲存在名為 target 數組中。

    svm_prob 的格式與 svm_train() 的格式一樣。

  • Function: int svm_get_svm_type(const struct svm_model *model);

    這個函數提供模式的 svm_type 。可能的 svm_type 值在 svm.h 已定義。

  • Function: int svm_get_nr_class(const svm_model *model);

    對于分類模式,這個函數提供類的數目。對于回歸或一個 one-class 模式,傳回值為 2 。

  • Function: void svm_get_labels(const svm_model model, int label)

    對于一個分類模式,這個函數輸出 labels 的名字,存進一個名為 label 的數組。

    對于回歸和 one-class 模式,label 是未改變的。

  • Function: double svm_get_svr_probability(const struct svm_model *model);

    對于一個有機率資訊的回歸模式,這個函數輸出一個值 sigma > 0。

    對于測試資料,我們認為這樣一個機率模式:

    目标值 = 預測值 + z, z: Laplace

    分布 e^(-|z|/sigma)/(2sigma)

    如果模式不是用于 svr 或不包含所需的資訊,傳回 0 。

  • Function: double svm_predict_values(const svm_model *model,

    const svm_node x, double dec_values)

    這個函數給出一個在給定模式下測試向量x的決定值,并傳回預測的 label(類)或者函數值(回歸)。

    對于有 nr_class 個類的分類模式,這個函數在 dec_values 數組中提供 nr_class*(nr_class-1)/2 個決 定值,nr_class 可從 svm_get_nr_class 函數獲得。

    順序是 label[0] vs. label[1], …,label[0] vs. label[nr_class-1],

    label[1] vs. label[2], …,label[nr_class-2] vs. label[nr_class-1],

    label可由 svm_get_labels 函數得到。

    傳回值是 x 被預測的類。注意當 nr_class = 1 時這個函數不給出任何決定值。

    對于回歸模式,dec_values[0] 和傳回值都是在該模式下計算的 x 的函數值。

    對于 one-class 模式,dec_values[0] 是 x 的決定值,傳回值是 +1/-1 。

  • Function: double svm_predict_probability(const struct svm_model *model,

    const struct svm_node x, double prob_estimates);

    這個函數在有機率資訊的模式下為測試向量 x 做分類或回歸。

    對于一個有機率資訊的分類模式,這個函數在 prob_estimates 數組中給出 nr_class 個機率估計。

    nr_class 可從 svm_get_nr_class 函數獲得。傳回有最大機率的類。

    對于回歸或 one-class SVM,prob_estimates 數組未改變并且傳回值與 svm_predict 一樣。

  • Function: const char *svm_check_parameter(const struct svm_problem *prob,

    const struct svm_parameter *param);

    這個函數檢查參數是否在可行的範圍之内。這個函數應該被 svm_train() 和 svm_cross_validation()使用之前調用。如果參數可行,它傳回 NULL ,否則傳回一個錯誤資訊。

  • Function: int svm_check_probability_model(const struct svm_model *model);

    這個函數檢查模式是否包含做機率估計時所需的資訊。如果有,傳回 +1 。否則,傳回 0 。

    這個函數應該在 svm_get_svr_probability 和 svm_predict_probability隻用之前調用。

  • Function: int svm_save_model(const char *model_file_name,

    const struct svm_model *model);

    這個函數儲存一個模式為一個檔案;傳回 0 表示成功,或者 -1 表示有錯誤發生。

  • Function: struct svm_model *svm_load_model(const char *model_file_name);

    這個函數傳回一個指向從檔案讀入的模式的指針,或者如果模式不能被載入則傳回一個空指針。

  • Function: void svm_free_model_content(struct svm_model *model_ptr);

    這個函數釋放被模式結構中的條目使用的記憶體。

  • Function: void svm_free_and_destroy_model(struct svm_model **model_ptr_ptr);

    這個函數釋放模式使用的記憶體并摧毀模式結構。

    等價于 svm_destroy_model ,version 3.0 以後的版本中被免去。

  • Function: void svm_destroy_param(struct svm_parameter *param);

    這個函數釋放設定參數時候使用的記憶體。

  • Function: void svm_set_print_string_function(void (print_func)(const char ));

    使用者可以用一個函數确定他們的輸出格式。用 svm_set_print_string_function(NULL);

    向預設的标準輸出中輸出。

Java版本

=======================

預編譯的 java 類檔案‘libsvm.jar’和其源碼檔案在 java 檔案夾下。使用下列指令來運作程式,

java -classpath libsvm.jar svm_train <arguments>
java -classpath libsvm.jar svm_predict <arguments>
java -classpath libsvm.jar svm_toy
java -classpath libsvm.jar svm_scale <arguments>
           

注意你需要 Java 1.5 (5.0) 或更高來運作它。

你也許需要添加 Java runtime library (像是 classes.zip) 到classpath中。

你也可能要增加 maximum Java heap size.

庫的使用與C版本類似。有以下函數可用:

public class svm {
    public static final int LIBSVM_VERSION=; 
    public static svm_model svm_train(svm_problem prob, svm_parameter param);
    public static void svm_cross_validation(svm_problem prob, svm_parameter param, int nr_fold, double[] target);
    public static int svm_get_svm_type(svm_model model);
    public static int svm_get_nr_class(svm_model model);
    public static void svm_get_labels(svm_model model, int[] label);
    public static void svm_get_sv_indices(svm_model model, int[] indices);
    public static int svm_get_nr_sv(svm_model model);
    public static double svm_get_svr_probability(svm_model model);
    public static double svm_predict_values(svm_model model, svm_node[] x, double[] dec_values);
    public static double svm_predict(svm_model model, svm_node[] x);
    public static double svm_predict_probability(svm_model model, svm_node[] x, double[] prob_estimates);
    public static void svm_save_model(String model_file_name, svm_model model) throws IOException
    public static svm_model svm_load_model(String model_file_name) throws IOException
    public static String svm_check_parameter(svm_problem prob, svm_parameter param);
    public static int svm_check_probability_model(svm_model model);
    public static void svm_set_print_string_function(svm_print_interface print_func);
}
           

庫在”libsvm” 包中.

注意,在 Java 版本中,svm_node[] 并不以一個 index = -1 的節點結束。

使用者可以指定輸出格式用:

your_print_func = new svm_print_interface()
    { 
        public void print(String s)
        {
            // your own format
        }
    };
    svm.svm_set_print_string_function(your_print_func);
           

編譯Windows二進制檔案

===================================

Windows 二進制檔案在‘windows’目錄下。按以下步驟用 Visual C++ 編譯它們:

1)打開DOS指令行 (或Visual Studio指令行) 切換到 libsvm目錄.

若 VC++ 的環境變量還未設定, 鍵入

"C:\Program Files\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat"
           

你可能不得不更改上面指令根據你自己VC++的版本或安裝的位置。

2)鍵入

3) (可選) 建構共享庫 libsvm.dll, 鍵入

nmake -f Makefile.win lib
           

4) (可選) To build 32-bit windows binaries, you must

(1) Setup “C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\vcvars32.bat” instead of vcvars64.bat

(2) Change CFLAGS in Makefile.win: /D _WIN64 to /D _WIN32

Another way is to build them from Visual C++ environment. See details in libsvm FAQ.

附加工具: Sub-sampling, Parameter Selection, Format checking,等

==============================================

詳見tools目錄中的README.

MATLAB/OCTAVE接口

===============================

Please check the file README in the directory ‘matlab’.

Python接口

=======================

See the README file in python directory.

補充

=======================

If you find LIBSVM helpful, please cite it as

Chih-Chung Chang and Chih-Jen Lin, LIBSVM : a library for support

vector machines. ACM Transactions on Intelligent Systems and

Technology, 2:27:1–27:27, 2011. Software available at

http://www.csie.ntu.edu.tw/~cjlin/libsvm

LIBSVM implementation document is available at

http://www.csie.ntu.edu.tw/~cjlin/papers/libsvm.pdf

For any questions and comments, please email [email protected]

Acknowledgment:

This work was supported in part by the National Science

Council of Taiwan via the grant NSC 89-2213-E-002-013.

The authors thank their group members and users

for many helpful discussions and comments. They are listed in

http://www.csie.ntu.edu.tw/~cjlin/libsvm/acknowledgements

繼續閱讀