Friday, February 4, 2011

Installing libsvm-3.0 for Python on OSX 10.6

This was very frustrating to solve and I eventually had to get my friend Kieran (thanks Kieran!) to help me.  Basically the vanilla installation instructions that come with are insufficient (at least for me) to get a working module.  Here are the steps that were required to get everything to work.

1. Run 'make' in the libsvm-3.0 directory

2. Run 'make' in the libsvm-3.0/python directory

In the libsvm-3.0 directory there now should be a .so2 file

3. Create a new directory in your site-packages directory (your pythonpath) called libsvm

4. Copy the .so2 file from libsvm-3.0 and the svm.py, svm.pyc, svmutil.py files from libsvm-3.0/python to site-packages/libsvm

There are a couple things that are missing so now we need to make them.

5. In site-packages/libsvm create a file called __init__.py.  This is an empty file, but it is necessary to get the directory recognized as a python module.

6. Edit svm.py and add the following two lines after the other import statements at the top of the file:

          import os.path
    _PATH = os.path.join( *os.path.split(__file__)[:-1] )


7.  At around line 7 you will see this statement


          # For unix the prefix 'lib' is not considered.

    if find_library('svm'):
       libsvm = CDLL(find_library('svm'))
    elif find_library('libsvm'):
       libsvm = CDLL(find_library('libsvm'))
    else:
       if sys.platform == 'win32':
          libsvm = CDLL('../windows/libsvm.dll')
       else:
          libsvm = CDLL('../libsvm.so.2')

8.  Change this to look like this:


    # For unix the prefix 'lib' is not considered.
    if find_library('svm'):
  libsvm = CDLL(find_library('svm'))
    elif find_library('libsvm'):
  libsvm = CDLL(find_library('libsvm'))
    else:
  if sys.platform == 'win32':
  libsvm = CDLL(os.path.join(_PATH,'windows','libsvm.dll'))
  else:
  libsvm = CDLL(os.path.join(_PATH,'libsvm.so.2'))

9. Once you save svm.py, you should be able to fire up a python interpreter and do 'from libsvm import svm'.  If that works, and a dir(svm) shows you a ton of functions, then you are good to go.

5 comments:

  1. Why installing libsvm 3.12 on os 10.7.4 does not work ?
    >>>import libsvm ...works
    >>>from libsvm import svm...does not

    ReplyDelete
  2. If you are using the latest version of LIBSVM (3.16, as of March 2013) then the process is a little simpler.

    svm.py file only uses path.join to find the correct dir. The only issue is that it expects to find the shared library in the parent dir. So you just need to remove the '../' kin the following command:

    libsvm = CDLL(path.join(dirname, '../libsvm.so.2'))
    ->
    libsvm = CDLL(path.join(dirname, 'libsvm.so.2'))

    Not sure what is the right command for win32 but I would try
    libsvm = CDLL(path.join(dirname,'windows','libsvm.dll'))
    (have not tested it)

    Notice that dirname uses a different way to get the path name (in the default 3.16 implementation) compared to the code given for _PATH here, but they both work the same.

    ReplyDelete
  3. Help, I followed the instructions, but I haven't been able to use LibSVM. I use LIBSVM-3.20 and Ubuntu 14.04

    ReplyDelete
  4. Hey, thank you so much for the tutorial! I followed the steps but got blocked in the first step. When I run make in the libsvm library, it just says that file not found. Would you please suggest if you ever met of heard of this problem before? If so, how did you fix it? Thank you!

    ReplyDelete
  5. Instead of altering codes, Just make libsvm and python directory a package by adding __init__.py into their respective directory.
    To use svm,
    from libsvm.python import svm

    To use svmutil,
    from libsvm.python import svmutil

    ReplyDelete