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

Answer from Eric O. Lebigot on Stack Overflow
🌐
GitHub
github.com › cjlin1 › libsvm › tree › master › python
libsvm/python at master · cjlin1/libsvm
For Windows, the shared library libsvm.dll is ready in the directory `..\windows' and you can directly run the interface in the current python directory. You can copy libsvm.dll to the system directory (e.g., `C:\WINDOWS\system32\') to make ...
Author   cjlin1
🌐
PyPI
pypi.org › project › libsvm
libsvm
JavaScript is disabled in your browser · Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
Discussions

svm - How to Setup LIBSVM for Python - Stack Overflow
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. ... Side note: instead of compiling programs yourself, you might want to use ... More on stackoverflow.com
🌐 stackoverflow.com
machine learning - An example using python bindings for SVM library, LIBSVM - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I am in dire need of a classification task example using LibSVM in python. More on stackoverflow.com
🌐 stackoverflow.com
machine learning - Documentation for libsvm in python - Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most. Learn more about Collectives ... Connect and share knowledge within a single location that is structured and easy to search. Learn more about Teams ... Is there any good documentation for libsvm in python with ... More on stackoverflow.com
🌐 stackoverflow.com
python - How can I use libsvm on scikit learn? - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... I want to use libsvm as a classifier for predicition. More on stackoverflow.com
🌐 stackoverflow.com
June 5, 2017
🌐
GitHub
github.com › cjlin1 › libsvm › blob › master › python › README
libsvm/python/README at master · cjlin1/libsvm
Programmers should use the interface functions or methods of svm_model class · in Python to get the values. The following description introduces additional · fields and methods. · Before using the data structures, execute the following command to load the · LIBSVM shared library: ·
Author   cjlin1
🌐
Bytefish
bytefish.de › blog › using_libsvm.html
Using libsvm - bytefish.de
September 1, 2011 - from svmutil import * import numpy as np import random import csv def normalize(X, low=0, high=1): X = np.asanyarray(X) minX = np.min(X) maxX = np.max(X) # Normalize to [0...1]. X = X - minX X = X / (maxX - minX) # Scale to [low...high]. X = X * (high-low) X = X + low return X def zscore(X): X = np.asanyarray(X) mean = X.mean() std = X.std() X = (X-mean)/std return X, mean, std reader = csv.reader(open('wine.data', 'rb'), delimiter=',') classes = [] data = [] for row in reader: classes.append(int(row[0])) data.append([float(num) for num in row[1:]]) data = np.asarray(data) classes = np.asarray
🌐
Usherb
dmi.usherb.ca › ~larocheh › mlpython › learners_third_party_libsvm.html
LIBSVM Learners — MLPython 0.1 documentation
tcsh set LIBSVMDIR=~/python set PYTHON_INCLUDEDIR=/usr/include/python2.6 cd $LIBSVMDIR/ wget "http://www.csie.ntu.edu.tw/~cjlin/cgi-bin/libsvm.cgi?+http://www.csie.ntu.edu.tw/~cjlin/libsvm+tar.gz" tar -zxf libsvm-3.1.tar.gz cd libsvm-3.1 make cd python make exit · Finally, you’ll need to add $MLIBSVMDIR/python to your PYTHONPATH.
🌐
pytz
pythonhosted.org › bob.learn.libsvm › guide.html
Support Vector Machines and Trainers — bob.learn.libsvm 2.0.12 documentation
Our bindings to libsvm do not include scaling. If you want to implement that generically, please do it. Then, an SVM [1] can be trained easily using the bob.learn.libsvm.Trainer class. >>> trainer = bob.learn.libsvm.Trainer() >>> machine = trainer.train(data) #ordering only affects labels · This returns a bob.learn.libsvm.Machine which can later be used for classification, as explained before.
Top answer
1 of 8
24

The code examples listed here don't work with LibSVM 3.1, so I've more or less ported the example by mossplix:

from svmutil import *
svm_model.predict = lambda self, x: svm_predict([0], [x], self)[0][0]

prob = svm_problem([1,-1], [[1,0,1], [-1,0,-1]])

param = svm_parameter()
param.kernel_type = LINEAR
param.C = 10

m=svm_train(prob, param)

m.predict([1,1,1])
2 of 8
20

This example demonstrates a one-class SVM classifier; it's about as simple as possible while still showing the complete LIBSVM workflow.

Step 1: Import NumPy & LIBSVM

  import numpy as NP
    from svm import *

Step 2: Generate synthetic data: for this example, 500 points within a given boundary (note: quite a few real data sets are are provided on the LIBSVM website)

Data = NP.random.randint(-5, 5, 1000).reshape(500, 2)

Step 3: Now, choose some non-linear decision boundary for a one-class classifier:

rx = [ (x**2 + y**2) < 9 and 1 or 0 for (x, y) in Data ]

Step 4: Next, arbitrarily partition the data w/r/t this decision boundary:

  • Class I: those that lie on or within an arbitrary circle

  • Class II: all points outside the decision boundary (circle)


The SVM Model Building begins here; all steps before this one were just to prepare some synthetic data.

Step 5: Construct the problem description by calling svm_problem, passing in the decision boundary function and the data, then bind this result to a variable.

px = svm_problem(rx, Data)

Step 6: Select a kernel function for the non-linear mapping

For this exmaple, i chose RBF (radial basis function) as my kernel function

pm = svm_parameter(kernel_type=RBF)

Step 7: Train the classifier, by calling svm_model, passing in the problem description (px) & kernel (pm)

v = svm_model(px, pm)

Step 8: Finally, test the trained classifier by calling predict on the trained model object ('v')

v.predict([3, 1])
# returns the class label (either '1' or '0')

For the example above, I used version 3.0 of LIBSVM (the current stable release at the time this answer was posted).

Finally, w/r/t the part of your question regarding the choice of kernel function, Support Vector Machines are not specific to a particular kernel function--e.g., i could have chosen a different kernel (gaussian, polynomial, etc.).

LIBSVM includes all of the most commonly used kernel functions--which is a big help because you can see all plausible alternatives and to select one for use in your model, is just a matter of calling svm_parameter and passing in a value for kernel_type (a three-letter abbreviation for the chosen kernel).

Finally, the kernel function you choose for training must match the kernel function used against the testing data.

Find elsewhere
🌐
Vivian Website
csie.ntu.edu.tw › ~cjlin › libsvm
LIBSVM -- A Library for Support Vector Machines
> pip install -U libsvm-official The python directory is re-organized so ... >>> from svmutil import * should be used. LIBSVM tools provides many extensions of LIBSVM. Please check it if you need some functions not supported in LIBSVM. We now have a nice page LIBSVM data sets providing problems ...
🌐
PyPI
pypi.org › project › libsvm-official
libsvm-official · PyPI
In addition, DON'T use the following FAILED commands > python setup.py install (failed to run at the python directory) > pip install . Quick Start =========== "Quick Start with Scipy" is in the next section. There are two levels of usage. The high-level one uses utility functions in svmutil.py and commonutil.py (shared with LIBLINEAR and imported by svmutil.py). The usage is the same as the LIBSVM MATLAB interface.
      » pip install libsvm-official
    
Published   Dec 29, 2025
Version   3.37.0
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › introduction-to-libsvm-and-python-bindings
Introduction to LIBSVM and Python Bindings - GeeksforGeeks
July 23, 2025 - First, download the dataset and convert it into the LIBSVM format. You can use Python's pandas library to load and preprocess the data.
Top answer
1 of 2
8

If you have already downloaded libSVM you will find some "usefull" documentation inside two files:

  • ./libsvm-3.xx/README file in the top directory which covers the C/C++ API and also documentation about the binary executables svm-predict, svm-scale and svm-train

  • ./libsvm-3.xx/python/README which deals with the Python interfaces (svm and svmutil), which I think is what you are looking for. However the example is quite naive although is a good beginning.

Let me suggest you that if you want to work with libSVM in Python, the scikit-learn package implements SVM using libSVM underneath, it much more easy, better documented and let's you control the same parameters of libSVM.

2 of 2
0

I think you might be approaching this the wrong way. You seem to be expecting to use LIBSVM as if it was ls: just do man ls to get the parameters and view the results. SVMs are more complicated than that.

The authors of LIBSVM publish a document (not a scientific paper!) called: A Practical Guide to Support Vector Classification. You need to read and understand all that the authors explain there. The appendix to that guide gives multiple examples on many datasets and how to train and how to search for parameters (all things that are very important).

There is a README file in the python directory of the LIBSVM distribution. If you understand python and you read the practical guide you should be able to use it. If not you should probably start from the command line examples to learn SVM or start with somthing easier(not SVMs!) to learn python. After reading and understanding that you should be able to read use all the examples from the appendix and invoke them from python.

Once you've tried this you should be up and running in no time. If not, this is a great place to ask specific questions about problems you run into.

🌐
GitHub
github.com › cjlin1 › libsvm
GitHub - cjlin1/libsvm: LIBSVM -- A Library for Support Vector Machines · GitHub
Libsvm is available at http://www.csie.ntu.edu.tw/~cjlin/libsvm Please read the COPYRIGHT file before using libsvm. Table of Contents ================= - Quick Start - Installation and Data Format - `svm-train' Usage - `svm-predict' Usage - `svm-scale' Usage - Tips on Practical Use - Examples - Precomputed Kernels - Library Usage - Java Version - Building Windows Binaries - Additional Tools: Sub-sampling, Parameter Selection, Format checking, etc. - MATLAB/OCTAVE Interface - Python Interface - Additional Information Quick Start =========== If you are new to SVM and if the data is not large, please go to `tools' directory and use easy.py after installation.
Starred by 4.7K users
Forked by 1.6K users
Languages   Java 21.8% | C++ 18.7% | HTML 17.9% | M4 13.9% | Python 13.5% | C 13.3%
🌐
Anaconda.org
anaconda.org › conda-forge › libsvm
libsvm - conda-forge | Anaconda.org
Sign In Sign Up · conda-forge/libsvm ... 94 · Labels 3 · Badges · Versions · 336 · To install this package, run one of the following: $conda install conda-forge::libsvm ·...
🌐
GitHub
github.com › cjlin1 › libsvm › blob › master › python › libsvm › svm.py
libsvm/python/libsvm/svm.py at master · cjlin1/libsvm
csr_to_problem_nojit(x.shape[0], x.data, x.indices, x.indptr, prob_val, prob_ind, prob.rowptr, indx_start) ... fillprototype(libsvm.svm_cross_validation, None, [POINTER(svm_problem), POINTER(svm_parameter), c_int, POINTER(c_double)])
Author   cjlin1
🌐
Python Programming
pythonprogramming.net › svm-in-python-machine-learning-tutorial
Beginning SVM from Scratch in Python
Just know that OOP creates objects with attributes, the functions within the class are actually methods, and we use "self" on variables that can be referenced anywhere within the class (or object). This is by no means a great explanation, but it should be enough to get you going. If you are confused about the code, just ask! class Support_Vector_Machine: def __init__(self, visualization=True): self.visualization = visualization self.colors = {1:'r',-1:'b'} if self.visualization: self.fig = plt.figure() self.ax = self.fig.add_subplot(1,1,1)
🌐
Stack Overflow
stackoverflow.com › questions › 44368107 › how-can-i-use-libsvm-on-scikit-learn
python - How can I use libsvm on scikit learn? - Stack Overflow
June 5, 2017 - I got following error: File "sklearn/svm/libsvm.pyx", line 270, in sklearn.svm.libsvm.predict (sklearn/svm/libsvm.c:3917) TypeError: predict() takes at least 6 positional arguments (1 given) Is this the correct way? How can I resolve the error? ... Check out the excellent docs. You are doing a wrong import (don't use the low-level stuff). It's called sklearn.svm.SVC. Apart from that, your python-usage in regards to calling your imported function is wrong too, so one more documentation (python) you should consider.
🌐
Vivian Website
csie.ntu.edu.tw › ~cjlin › libsvmtools
LIBSVM Tools
Download LIBSVM (version 2.91 or after) and make the LIBSVM python interface. Download plotroc.py to the python directory. Edit the path of gnuplot in plotroc.py in necessary. ... > plotroc.py -v 5 -c 10 ../heart_scale If there is no test data, "validated decision values" from cross-validation ...
🌐
Paperspace
blog.paperspace.com › multi-class-classification-using-libsvm
Multi-class classification using LIBSVM
October 22, 2022 - In this article, we explain the SVM algorithm generally, and then show how to use the LIBSVM package in a code demo. After the code section, we will share some additional tips to help improve the performance of our model, as well as some assumptions and limitations of the algorithm.