How to Setup LIBSVM for Python

David Williams picture David Williams · Apr 2, 2013 · Viewed 36.4k times · Source

I built libsvm on Mac OS X with Make.

$ tar xzfv libsvm-3.17.tar.gz
$ cd libsvm-3.17
$ make

This built the various libsvm binaries:

$ ls
COPYRIGHT           heart_scale svm-predict.c   svm-train.c tools
FAQ.html            java        svm-scale   svm.cpp     windows
Makefile            matlab      svm-scale.c svm.def
Makefile.win    python      svm-toy     svm.h
README      svm-predict svm-train   svm.o

I also linked to this in /usr/local:

$ ls -la /usr/local/
...
svm -> /usr/local/libsvm-3.17/

And appended the Python bindings to my path:

import sys
sys.path.append('/usr/local/svm/python')

But the Python bindings cannot find the "LIBSVM" library:

$ python test.py 
Traceback (most recent call last):
   File "test.py", line 8, in <module>
      import svmutil
   File "/usr/local/svm/python/svmutil.py", line 5, in <module>
      from svm import *
   File "/usr/local/svm/python/svm.py", line 22, in <module>
      raise Exception('LIBSVM library not found.')
Exception: LIBSVM library not found.

Can anyone tell me how to set this up? In the python readme for libsvm the only description is

Installation 
============

On Unix systems, type

> make

The interface needs only LIBSVM shared library, which is generated by
the above command. We assume that the shared library is on the LIBSVM
main directory or in the system path.

What am I missing?

Answer

Eric O Lebigot picture Eric O Lebigot · Apr 2, 2013

Instead of going through libsvm in order to access it with Python (I installed libsvm through MacPorts, and import svmutil fails), you might want to install the popular scikit-learn package, which contains an optimized version of libsvm with Python bindings.

The install is very simple with MacPorts: sudo port install py27-scikit-learn (adapt py27 to whatever version of Python you use).