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])
Answer from ShinNoNoir on Stack Overflow
🌐
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
🌐
GitHub
github.com › cjlin1 › libsvm › tree › master › python
libsvm/python at master · cjlin1/libsvm
The interface is developed with the built-in Python library "ctypes." Installation via PyPI ===================== To install the interface from PyPI, execute the following command: > pip install -U libsvm-official Installation via Sources ======================== Alternatively, you may install the interface from sources by generating the LIBSVM shared library.
Author   cjlin1
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.

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])
Answer from ShinNoNoir on Stack Overflow
🌐
PyPI
pypi.org › project › libsvm-official
libsvm-official · PyPI
The interface is developed with the built-in Python library "ctypes." Installation via PyPI ===================== To install the interface from PyPI, execute the following command: > pip install -U libsvm-official Installation via Sources ======================== Alternatively, you may install the interface from sources by generating the LIBSVM shared library.
      » pip install libsvm-official
    
Published   Dec 29, 2025
Version   3.37.0
🌐
Vivian Website
csie.ntu.edu.tw › ~cjlin › libsvm
LIBSVM -- A Library for Support Vector Machines
Version 3.31 released on February 28, 2023. Probabilistic outputs for one-class SVM are now supported. Version 3.25 released on April 14, 2021. Installing the Python interface through PyPI is supported · > pip install -U libsvm-official The python directory is re-organized so
🌐
GitHub
github.com › cjlin1 › libsvm › blob › master › python › README
libsvm/python/README at master · cjlin1/libsvm
Python can directly access C structures and interface functions defined · in svm.h. · While advanced users can use structures/functions in svm.py, to · avoid handling ctypes structures, in svmutil.py we provide some easy-to-use · functions. The usage is similar to LIBSVM MATLAB interface.
Author   cjlin1
🌐
Usherb
dmi.usherb.ca › ~larocheh › mlpython › learners_third_party_libsvm.html
LIBSVM Learners — MLPython 0.1 documentation
The package learners.third_party.libsvm contains modules for learning algorithms using the LIBSVM library. These modules all require that the LIBSVM library be installed. ... That should do it. Try ‘python test.py’ to see if your installation is working.
Find elsewhere
🌐
pytz
pythonhosted.org › bob.learn.libsvm › guide.html
Support Vector Machines and Trainers — bob.learn.libsvm 2.0.12 documentation
Visit the documentation for bob.learn.libsvm.File for information on loading LIBSVM data files directly into python and producing numpy.ndarray objects.
🌐
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
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.

🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › introduction-to-libsvm-and-python-bindings
Introduction to LIBSVM and Python Bindings - GeeksforGeeks
July 23, 2025 - LIBSVM offers simple linear, polynomial, and RBF kernels as well as the most efficient methods to resolve large scale data issues. In Python there is a module named svm which is a direct wrapper to libsvm, and there is another very powerful Library named as scikit-learn which wraps LIBSVM for the ease of execution of SVM operation.
🌐
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%
🌐
GitHub
github.com › cjlin1 › libsvm › blob › master › python › libsvm › svm.py
libsvm/python/libsvm/svm.py at master · cjlin1/libsvm
LIBSVM -- A Library for Support Vector Machines. Contribute to cjlin1/libsvm development by creating an account on GitHub.
Author   cjlin1
🌐
Idiap
idiap.ch › software › bob › docs › bob › bob.learn.libsvm › v2.1.1 › py_api.html
Python API - bob.learn.libsvm - Idiap Research Institute — EN
If the number of classes is greater than 2, labels are picked starting from 1 (i.e., 1, 2, 3, 4, etc.). This convention follows what is done at the command-line for LIBSVM. The input object data must be an iterable object (such as a Python list or tuple) containing 2D 64-bit float arrays each ...
🌐
GitHub
github.com › arnaudsj › libsvm › blob › master › python › svmutil.py
libsvm/python/svmutil.py at master · arnaudsj/libsvm
LIBSVM -- A Library for Support Vector Machines (unofficial snapshot dist mirror) - libsvm/python/svmutil.py at master · arnaudsj/libsvm
Author   arnaudsj
🌐
Anaconda.org
anaconda.org › conda-forge › libsvm
libsvm - conda-forge | Anaconda.org
Sign In Sign Up · conda-forge/libsvm · Community · A simple, easy-to-use, and efficient software for SVM classification and regression. Copied fromcf-post-staging / libsvm · Overview · Files 94 · Labels 3 · Badges · Versions · 336 · To install this package, run one of the following: ...
🌐
GitHub
github.com › ocampor › libsvm
GitHub - ocampor/libsvm: Pre-built LibSVM packages for Python.
Pre-built LibSVM packages for Python. Crated by Chih-Chung Chang and Chih-Jen Lin, LIBSVM is an integrated software for support vector classification, (C-SVC, nu-SVC), regression (epsilon-SVR, nu-SVR) and distribution estimation (one-class SVM).
Starred by 4 users
Forked by 3 users
Languages   C++ 63.7% | Python 32.5% | C 3.3% | C++ 63.7% | Python 32.5% | C 3.3%
🌐
Vivian Website
csie.ntu.edu.tw › ~cjlin › libsvmtools
LIBSVM Tools
This is a graphical environment for doing experiments with libsvm. You can create and connect components (like scaler, trainer, predictor, etc) in this environment. The program can be extended easily by writing more "plugins". It was written in python and uses wxPython library.
🌐
GitHub
github.com › surban › libsvm › blob › master › python › svmutil.py
libsvm/python/svmutil.py at master · surban/libsvm
#!/usr/bin/env python · · from svm import * · def svm_read_problem(data_file_name): """ svm_read_problem(data_file_name) -> [y, x] · Read LIBSVM-format data from data_file_name and return labels y · and data instances x. """ prob_y = [] prob_x = [] for line in open(data_file_name): line = line.split(None, 1) # In case an instance with all zero features ·
Author   surban