How to Setup LIBSVM for Python
Asked Answered
B

7

9

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?

Bumgardner answered 2/4, 2013 at 2:34 Comment(2)
Side note: instead of compiling programs yourself, you might want to use a package manager like the very good MacPorts manager: package maintainers have solved compilation problems for you. :)Irrelevant
I had the same problem in a 64bit Unix environment (64 bit Python installation). In my case, trying the 64bit of Liblinear solved the problem.Rustproof
I
9

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).

Irrelevant answered 2/4, 2013 at 2:49 Comment(6)
I had a hard time installing with Macports. The Scipy Superpack worked immediately. fonnesbeck.github.com/ScipySuperpackBumgardner
What problems did you have with MacPorts? I am asking the question because MacPorts is a good investment: it contains 15k+ packages that (almost always) compile automatically. If I remember correctly, the install procedure requires a long download (XCode) that requires registration with Apple, running the .pkg installer, and possibly configuring the shell path (unless you use bash, in which case this is done for you). Once this is done, things run usually pretty smoothly.Irrelevant
If I remember correctly I dont think I could get scipy to install and scikit-learn depends on it. I tried downloading scipy from sourceforge but I got a strange message about "Scipy can't be installed on this disk. scipy requires System Python 2.7 to install.'" but didnt know what to make of it.Bumgardner
So maybe not a macports error per se, but I gave up on installing the whole stack after about half an hour, then found the superpack.Bumgardner
@DavidWilliams: A package manager actually makes the install much easier than what you attempted: sudo port install py27-scikit-learn should download and install everything for you, including Scipy, automatically. You don't have to do anything else (once MacPorts is setup like I indicated).Irrelevant
Downvoters: can you be constructive and explain why you do this? That could make this answer more useful.Irrelevant
I
5

Seems like a old thread. Hope it helps someone else in the future.

I had the same problem. The solution is

  1. Run make in libsvm-3.0 directory
  2. Run make in libsvm-3.0/python directory

If you did only at libsvm-3.0 folder you will face this issue. Do it at both the folders. Then it will work fine.

Inexpert answered 14/12, 2015 at 22:57 Comment(0)
A
2

In case you need a non-MacPorts solution, see this page (especially the comment from Thanassis):

Installing libsvm-3.0 for Python on OSX 10.6

Despite the title of the post, the solution worked for me on a CentOS machine with python 2.7.

Aria answered 16/9, 2013 at 18:23 Comment(0)
O
2

find_library in python is only looking at files with an extension of .so. In order for this to work correctly you need to create a libsvm.so:

% ln -s libsvm.so.2 libsvm.so

Then try this again, it will work correctly.

Osy answered 5/11, 2014 at 19:57 Comment(0)
C
1

You do not need to use scikit learn in order to use libSVM. I had the same issue when loading the libsvm modules through python. I cloned the project from github and run build it from the command-line with make, and after setting the enviroment, I got the same error.

I fixed the issue by installing libSVM through homebrew:

brew install libsvm

This do not include the python specific binaries, so you would still have to clone and make from github and set up the environment.

Corlisscorly answered 21/1, 2017 at 13:16 Comment(0)
P
0

You should go into /python and make to generate .so.1 file

Paapanen answered 22/6, 2014 at 2:18 Comment(0)
J
0

you can also try to use homebrew to install the libsvm like 'brew install libsvm',then you can open your project file and copy the 'svmutil.py' and 'svm.py' files to the project floder,then you can use the command 'from svmutil import *',and maybe it will be ok.

Joellajoelle answered 27/3, 2016 at 13:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.