Happy new year!! Feel blue but still have to work.

I am gonna introduce the application of a famous package namely libsvm, which realizes the support vector machin (SVM) based algorithms and supports multiple programming languages. I have designed SVM based approaches for machine-learning tasks on MATLAB by the libsvm, which is very awsome. Now, it’s time to try it on python, though it has been encapsulated in the SciPy.

To use libsvm on python, you may follow the coming steps.

  1. Download the package

    1
    $ wget -O libsvm.tar.gz http://www.csie.ntu.edu.tw/~cjlin/cgi-bin/libsvm.cgi?+http://www.csie.ntu.edu.tw/~cjlin/libsvm+tar.gz
  2. unzip the package

    1
    $ tar -xvf libsvm.tar.gz
  3. Installization

    1
    2
    $ cd libsvm-3.22
    $ make

If it generates four svm- prefixed files as well as the library libsvm.so.2, then the installization is completed.

Now you can take a trial with provided python based utilities in the folder ./python. For instance,

1
2
3
4
5
6
7
8
9
10
11
12
>>> from svmutil import *
>>> y, x = svm_read_problem('../heart_scale') # load data
>>> m = svm_train(y[:200], x[:200], '-c 4') # train a SVM model
# This will output like,
*.*
optimization finished, #iter = 257
nu = 0.351161
obj = -225.628984, rho = 0.636110
nSV = 91, nBSV = 49
Total nSV = 91
>>> p_label, p_acc, p_val = svm_predict(y[200:], x[200:], m) # make a test
Accuracy = 84.2857% (59/70) (classification)

Note

As introduced by the README in the ./python folder, two .py scripts are provided, of which svm.py and svmutil.py are corresponding to low-level and high-level use of the interface. In my opinion, I suggest the users to directly use svmutil.py.

In addition, the python scripts rely on the libsvm.so.2, which should be added into the LD_LIBRARY_PATH or a package not find exception will be raised when import svm.

Now, time to do your machine learning work.