Tuesday, June 23, 2015

libsvm - How to use?

libsvm is a popular library for performing Support Vector Machine algorithm. This library is developed by Chih-Chung Chang and Chih-Jen Lin, professor at National Taiwan University. Next, I will show you how to install the libsvm in MATLAB.

1. Installation

  • Step 1. Download the libsvm from http://www.csie.ntu.edu.tw/~cjlin/libsvm/ and unzip it. There are some important files as in the following figure:
  • Step 2. In order to distinguish with possible internal svm function, we should change the names of files svmtrain.c and svmpredict.c in \libsvm-3.16\matlab\ to be libsvmtrain.c and libsvmpredict.c. And then locate make.m in the same folder and change line 16 and line 17 to be:
    mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims libsvmtrain.c ../svm.cpp svm_model_matlab.c
    mex CFLAGS="\$CFLAGS -std=c99" -largeArrayDims libsvmpredict.c ../svm.cpp svm_model_matlab.c
  • Step 3. Run make.m you just changed to mex *.c files. The successful result should look like the following figure:
  • Step 3 often fails due to the lack of a compatible C compiler for MATLAB. You can check for current accessible C compilers by issuing the below command in MATLAB:
    mex -setup C
    The expected result is the one with listed C compilers, which is already installed in your system:

    If none of compatible C compilers are found, you can obtain one from http://www.mathworks.com/support/compilers/current_release/. For me, "Microsoft Visual C++ 2013 Professional" works perfect in the case of compiling libsvm.

2. Usage


matlab> model = libsvmtrain(training_label_vector, training_instance_matrix [, 'libsvm_options']);

        -training_label_vector:
            An m by 1 vector of training labels (type must be double).
        -training_instance_matrix:
            An m by n matrix of m training instances with n features.
            It can be dense or sparse (type must be double).
        -libsvm_options:
            A string of training options in the same format as that of LIBSVM.

matlab> [predicted_label, accuracy, decision_values/prob_estimates] = libsvmpredict(testing_label_vector, testing_instance_matrix, model [, 'libsvm_options']);

matlab> [predicted_label] = libsvmpredict(testing_label_vector, testing_instance_matrix, model [, 'libsvm_options']);

        -testing_label_vector:
            An m by 1 vector of prediction labels. If labels of test
            data are unknown, simply use any random values. (type must be double)
        -testing_instance_matrix:
            An m by n matrix of m testing instances with n features.
            It can be dense or sparse. (type must be double)
        -model:
            The output of svmtrain.
        -libsvm_options:
            A string of testing options in the same format as that of LIBSVM.
For more information, please refer to README files inside libsvm packet.
(Nguyen My - 2015/06/23)

0 comments:

Post a Comment