如何为 Python 设置 LIBSVM

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15755130/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:55:35  来源:igfitidea点击:

How to Setup LIBSVM for Python

pythonsvmlibsvm

提问by David Williams

I built libsvmon Mac OS X with Make.

我使用 Make 在 Mac OS X 上构建了libsvm

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

This built the various libsvm binaries:

这构建了各种 libsvm 二进制文件:

$ 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:

我也在/usr/local

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

And appended the Python bindings to my path:

并将 Python 绑定附加到我的路径中:

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

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

但是 Python 绑定找不到“LIBSVM”库:

$ 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

谁能告诉我如何设置?在 libsvm 的 python 自述文件中,唯一的描述是

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?

我错过了什么?

采纳答案by Eric O Lebigot

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

与其通过 libsvm 以使用 Python 访问它(我通过 MacPorts 安装了 libsvm,但import svmutil失败了),您可能想要安装流行的scikit-learn包,其中包含带有 Python 绑定libsvm的优化版本。

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

MacPorts的安装非常简单:(sudo port install py27-scikit-learn适应py27您使用的任何版本的 Python)。

回答by Owen

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

如果您需要非 MacPorts 解决方案,请参阅此页面(尤其是来自 Thanassis 的评论):

Installing libsvm-3.0 for Python on OSX 10.6

在 OSX 10.6 上为 Python 安装 libsvm-3.0

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

尽管帖子标题如此,但该解决方案在装有 python 2.7 的 CentOS 机器上对我有用。

回答by Coderzelf

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

你应该进入 /python 并生成 .so.1 文件

回答by MagicalMachines

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:

python 中的 find_library 只查看扩展名为 .so 的文件。为了使其正常工作,您需要创建一个 libsvm.so:

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

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

Then try this again, it will work correctly.

然后再试一次,它会正常工作。

回答by Raja

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

看起来像一个旧线程。希望它可以帮助其他人在未来。

I had the same problem. The solution is

我有同样的问题。解决办法是

  1. Run makein libsvm-3.0 directory
  2. Run makein libsvm-3.0/python directory
  1. make在 libsvm-3.0 目录下运行
  2. 运行make在LIBSVM-3.0 /蟒蛇目录

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.

如果您仅在 libsvm-3.0 文件夹中执行此操作,您将面临此问题。在两个文件夹中执行此操作。然后它会正常工作。

回答by zhen wang

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.

您也可以尝试使用 homebrew 安装 libsvm,例如 'brew install libsvm',然后您可以打开您的项目文件并将 'svmutil.py' 和 'svm.py' 文件复制到项目 floder,然后您可以使用命令'from svmutil import *',也许就可以了。

回答by syssel

You do not need to use scikit learn in order to use libSVM. I had the same issue when loading the libsvmmodules 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.

您无需使用 scikit learn 即可使用 libSVM。libsvm通过python加载模块时我遇到了同样的问题。我从 github 克隆了该项目并使用 运行从命令行构建它make,在设置环境后,我得到了同样的错误。

I fixed the issue by installing libSVM through homebrew:

我通过以下方式安装 libSVM 解决了这个问题homebrew

brew install libsvm

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.

这不包括特定于 python 的二进制文件,因此您仍然需要从 github 克隆和制作并设置环境。