问题
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?
回答1:
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).
回答2:
Seems like a old thread. Hope it helps someone else in the future.
I had the same problem. The solution is
- Run
make
in libsvm-3.0 directory - 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.
回答3:
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.
回答4:
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.
回答5:
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.
回答6:
You should go into /python and make to generate .so.1 file
回答7:
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.
来源:https://stackoverflow.com/questions/15755130/how-to-setup-libsvm-for-python