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
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
🌐
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 › tree › master › python
libsvm/python at master · cjlin1/libsvm
>>> from libsvm.svm import * >>> prob = svm_problem(np.asarray([1,-1]), scipy.sparse.csr_matrix(([1, 1, -1, -1], ([0, 0, 1, 1], [0, 2, 0, 2])))) >>> param = svm_parameter('-c 4') >>> m = libsvm.svm_train(prob, param) # m is a ctype pointer to an svm_model # Convert a tuple of ndarray (index, data) to feature_nodearray, a ctypes structure # Note that index starts from 0, though the following example will be changed to 1:1, 3:1 internally >>> x0, max_idx = gen_svm_nodearray((np.asarray([0,2]), np.asarray([1,1]))) >>> label = libsvm.svm_predict(m, x0) Design Description ================== There are two files svm.py and svmutil.py, which respectively correspond to low-level and high-level use of the interface. In svm.py, we adopt the Python built-in library "ctypes," so that Python can directly access C structures and interface functions defined in svm.h.
Author   cjlin1
🌐
pytz
pythonhosted.org › bob.learn.libsvm › guide.html
Support Vector Machines and Trainers — bob.learn.libsvm 2.0.12 documentation
Visit the documentation for ... files directly into python and producing numpy.ndarray objects. Below is a quick example: Suppose the variable f contains an object of type bob.learn.libsvm.File....
🌐
GitHub
github.com › cjlin1 › libsvm › blob › master › python › README
libsvm/python/README at master · cjlin1/libsvm
# Note that index starts from 0, though the following example will be changed to 1:1, 3:1 internally · >>> x0, max_idx = gen_svm_nodearray((np.asarray([0,2]), np.asarray([1,1]))) >>> label = libsvm.svm_predict(m, x0) · Design Description · ================== · There are two files svm.py and svmutil.py, which respectively correspond to · low-level and high-level use of the interface. · In svm.py, we adopt the Python built-in library "ctypes," so that ·
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): ...
Find elsewhere
🌐
Usherb
dmi.usherb.ca › ~larocheh › mlpython › learners_third_party_libsvm.html
LIBSVM Learners — MLPython 0.1 documentation
Classifier using LIBSVM’s Support Vector Machine implementation · Examples should be input and target pairs. The input can use either a sparse or coarse representation, as returned by the mlpython.misc.io.libsvm_load function.
🌐
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%
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
gist.github.com › Salinger › 4579980
Test code for libsvm-python. · GitHub
January 20, 2013 - Test code for libsvm-python. GitHub Gist: instantly share code, notes, and snippets.
🌐
GeeksforGeeks
geeksforgeeks.org › machine learning › introduction-to-libsvm-and-python-bindings
Introduction to LIBSVM and Python Bindings - GeeksforGeeks
July 23, 2025 - Let's consider a practical example of using LIBSVM for multi-class classification. We will use the UCI Wine dataset, which is a popular dataset for classification tasks. First, download the dataset and convert it into the LIBSVM format.
🌐
PyPI
pypi.org › project › libsvm-official
libsvm-official · PyPI
>>> from libsvm.svm import * >>> prob = svm_problem(np.asarray([1,-1]), scipy.sparse.csr_matrix(([1, 1, -1, -1], ([0, 0, 1, 1], [0, 2, 0, 2])))) >>> param = svm_parameter('-c 4') >>> m = libsvm.svm_train(prob, param) # m is a ctype pointer to an svm_model # Convert a tuple of ndarray (index, data) to feature_nodearray, a ctypes structure # Note that index starts from 0, though the following example will be changed to 1:1, 3:1 internally >>> x0, max_idx = gen_svm_nodearray((np.asarray([0,2]), np.asarray([1,1]))) >>> label = libsvm.svm_predict(m, x0) Design Description ================== There are two files svm.py and svmutil.py, which respectively correspond to low-level and high-level use of the interface. In svm.py, we adopt the Python built-in library "ctypes," so that Python can directly access C structures and interface functions defined in svm.h.
      » pip install libsvm-official
    
Published   Dec 29, 2025
Version   3.37.0
🌐
VitalFlux
vitalflux.com › home › data science › sklearn svm classifier using libsvm – code example
Sklearn SVM Classifier using LibSVM - Code Example - Analytics Yogi
July 10, 2020 - In this section, you will see the code example for training an SVM classifier based on C-SVC implementation within LibSVM. Note that C is a regularization parameter that is used to train a soft-margin classifier allowing for bias-variance tradeoff based on the value of C.
🌐
Vivian Website
csie.ntu.edu.tw › ~cjlin › libsvmtools
LIBSVM Tools
Most of these programs are extended from/for LIBSVM. Some of the most useful programs include confidence margin/decision value output, infinite ensemble learning with SVM, dense format, and MATLAB implementation for estimating posterior probability. This is a simple python script (download here) to use F-score for selecting features.
🌐
GitHub
github.com › cjlin1 › libsvm › blob › master › python › libsvm › svm.py
libsvm/python/libsvm/svm.py at master · cjlin1/libsvm
Convert a ctypes POINTER(svm_model) to a Python svm_model · """ if bool(model_ptr) == False: raise ValueError("Null pointer") m = model_ptr.contents · m.__createfrom__ = 'C' return m · · fillprototype(libsvm.svm_train, POINTER(svm_model), [POINTER(svm_problem), POINTER(svm_parameter)]) fillprototype(libsvm.svm_cross_validation, None, [POINTER(svm_problem), POINTER(svm_parameter), c_int, POINTER(c_double)]) ·
Author   cjlin1
Top answer
1 of 2
11

Here's a step-by-step guide for how to train an SVM using your data and then evaluate using the same dataset. It's also available at http://nbviewer.ipython.org/gist/anonymous/2cf3b993aab10bf26d5f. At the url you can also see the output of the intermediate data and the resulting accuracy (it's an iPython notebook)

Step 0: Install dependencies

You need to install the following libraries:

  • pandas
  • scikit-learn

From command line:

pip install pandas
pip install scikit-learn

Step 1: Load the data

We will use pandas to load our data. pandas is a library for easily loading data. For illustration, we first save sample data to a csv and then load it.

We will train the SVM with train.csv and get test labels with test.csv

import pandas as pd

train_data_contents = """
class_label,distance_from_beginning,distance_from_end,contains_digit,capitalized
B,1,10,1,0
M,10,1,0,1
C,2,3,0,1
S,23,2,0,0
N,12,0,0,1"""


with open('train.csv', 'w') as output:
    output.write(train_data_contents)

train_dataframe = pd.read_csv('train.csv')

Step 2: Process the data

We will convert our dataframe into numpy arrays which is a format that scikit- learn understands.

We need to convert the labels "B", "M", "C",... to numbers also because svm does not understand strings.

Then we will train a linear svm with the data

import numpy as np

train_labels = train_dataframe.class_label
labels = list(set(train_labels))
train_labels = np.array([labels.index(x) for x in train_labels])
train_features = train_dataframe.iloc[:,1:]
train_features = np.array(train_features)

print "train labels: "
print train_labels
print 
print "train features:"
print train_features

We see here that the length of train_labels (5) exactly matches how many rows we have in trainfeatures. Each item in train_labels corresponds to a row.

Step 3: Train the SVM

from sklearn import svm
classifier = svm.SVC()
classifier.fit(train_features, train_labels)

Step 4: Evaluate the SVM on some testing data

test_data_contents = """
class_label,distance_from_beginning,distance_from_end,contains_digit,capitalized
B,1,10,1,0
M,10,1,0,1
C,2,3,0,1
S,23,2,0,0
N,12,0,0,1
"""

with open('test.csv', 'w') as output:
    output.write(test_data_contents)

test_dataframe = pd.read_csv('test.csv')

test_labels = test_dataframe.class_label
labels = list(set(test_labels))
test_labels = np.array([labels.index(x) for x in test_labels])

test_features = test_dataframe.iloc[:,1:]
test_features = np.array(test_features)

results = classifier.predict(test_features)
num_correct = (results == test_labels).sum()
recall = num_correct / len(test_labels)
print "model accuracy (%): ", recall * 100, "%"

Links & Tips

  • Example code for how to load LinearSVC: http://scikitlearn.org/stable/modules/svm.html#svm
  • Long list of scikit-learn examples: http://scikitlearn.org/stable/auto_examples/index.html. I've found these mildly helpful but often confusing myself.
  • If you find that the SVM is taking a long time to train, try LinearSVC instead: http://scikitlearn.org/stable/modules/generated/sklearn.svm.LinearSVC.html
  • Here's another tutorial on getting familiar with machine learning models: http://scikit-learn.org/stable/tutorial/basic/tutorial.html

You should be able to take this code and replace train.csv with your training data, test.csv with your testing data, and get predictions for your test data, along with accuracy results.

Note that since you're evaluating using the data you trained on the accuracy will be unusually high.

2 of 2
2

I echo the comment of @MarcoPashkov but will try to elaborate on the LibSVM file format. I find the documentation comprehensive yet hard to find, for the Python lib I recommend the README on GitHub.

An important piece to recognize is that there is a Sparse format where all features which are 0 get removed and a Dense format where features which are 0 are not removed. These two are equivalent examples of each taken from the README.

# Dense data
>>> y, x = [1,-1], [[1,0,1], [-1,0,-1]]
# Sparse data
>>> y, x = [1,-1], [{1:1, 3:1}, {1:-1,3:-1}]

The y variable stores a list of all the categories for the data.

The x variable stores the feature vector.

assert len(y) == len(x), "Both lists should be the same length"

The format found in the Heart Scale Example is a Sparse format where the dictionary key is the feature index and the dictionary value is the feature value while the first value is the category.

The Sparse format is incredibly useful while using a Bag of Words Representation for your feature vector.

As most documents will typically use a very small subset of the words used in the corpus, the resulting matrix will have many feature values that are zeros (typically more than 99% of them).

For instance a collection of 10,000 short text documents (such as emails) will use a vocabulary with a size in the order of 100,000 unique words in total while each document will use 100 to 1000 unique words individually.

For an example using the feature vector you started with, I trained a basic LibSVM 3.20 model. This code isn't meant to be used but may help in showing how to create and test a model.

from collections import namedtuple
# Using namedtuples for descriptive purposes, in actual code a normal tuple would work fine.
Category = namedtuple("Category", ["index", "name"])
Feature = namedtuple("Feature", ["category_index", "distance_from_beginning", "distance_from_end", "contains_digit", "capitalized"])

# Separate up the set of categories, libsvm requires a numerical index so we associate each with an index.
categories = dict()
for index, name in enumerate("B M C S NA".split(' ')):
    # LibSVM expects index to start at 1, not 0.
    categories[name] = Category(index + 1, name)
categories

Out[0]: {'B': Category(index=1, name='B'),
   'C': Category(index=3, name='C'),
   'M': Category(index=2, name='M'),
   'NA': Category(index=5, name='NA'),
   'S': Category(index=4, name='S')}

# Faked set of CSV input for example purposes.
csv_input_lines = """category_index,distance_from_beginning,distance_from_end,contains_digit,capitalized
B,1,10,1,0
M,10,1,0,1
C,2,3,0,1
S,23,2,0,0
NA,12,0,0,1""".split("\n")
# We just ignore the header.
header = csv_input_lines[0]

# A list of Feature namedtuples, this will be trained as lists.
features = list()
for line in csv_input_lines[1:]:
    split_values = line.split(',')
    # Create a Feature with the values converted to integers.
    features.append(Feature(categories[split_values[0]].index, *map(int, split_values[1:])))

features

Out[1]: [Feature(category_index=1, distance_from_beginning=1, distance_from_end=10, contains_digit=1, capitalized=0),
 Feature(category_index=2, distance_from_beginning=10, distance_from_end=1, contains_digit=0, capitalized=1),
 Feature(category_index=3, distance_from_beginning=2, distance_from_end=3, contains_digit=0, capitalized=1),
 Feature(category_index=4, distance_from_beginning=23, distance_from_end=2, contains_digit=0, capitalized=0),
 Feature(category_index=5, distance_from_beginning=12, distance_from_end=0, contains_digit=0, capitalized=1)]

# Y is the category index used in training for each Feature. Now it is an array (order important) of all the trained indexes.
y = map(lambda f: f.category_index, features)
# X is the feature vector, for this we convert all the named tuple's values except the category which is at index 0.
x = map(lambda f: list(f)[1:], features)

from svmutil import svm_parameter, svm_problem, svm_train, svm_predict
# Barebones defaults for SVM
param = svm_parameter()
# The (Y,X) parameters should be the train dataset.
prob = svm_problem(y, x)
model=svm_train(prob, param)

# For actual accuracy checking, the (Y,X) parameters should be the test dataset.
p_labels, p_acc, p_vals = svm_predict(y, x, model)

Out[3]: Accuracy = 100% (5/5) (classification)

I hope this example helps, it shouldn't be used for your training. It is meant as an example only because it is inefficient.

🌐
Paperspace
blog.paperspace.com › multi-class-classification-using-libsvm
Multi-class classification using LIBSVM
October 22, 2022 - I will use the Python interface in a Jupyter notebook; importing it via cloning it from GitHub and building the package. ... Type of SVM: LIBSVM supports Support Vector Classification (SVC) problems, binary and multi-class, e.g C-SVC and nu-SVC, Support Vector Regression (SVR) problems, e.g epsilon-SVR and nu-SVR, and One-class SVM.
🌐
Quora
quora.com › What-is-an-example-of-proper-usage-of-the-LIBSVM-library-functions
What is an example of proper usage of the LIBSVM library functions? - Quora
Answer (1 of 3): Josh Freckleton, answer is quite informative. Still you can go through this Page I assume you already have an algorithmic understanding of how SVM works. I don't use libsvm directly but there are APIs in R and Python which call libsvm. I feel the best way to understand functio...