Table Of Contents

Previous topic

Third-party Learners

Next topic

Milk Learners

This Page

LIBSVM Learners

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.

To use LIBSVM through MLPython, do the following:

  1. download LIBSVM from here:
  2. install LIBSVM (see LIBSVM instructions)
  3. install the included python interface (see LIBSVM intrusctions)
  4. put path to the python interface in PYTHONPATH

That should do it. Try ‘python’ to see if your installation is working.

Here is an example of what steps 1 to 3 can look like, where LIBSVMDIR is the path where you wish to install LIBSVM and PYTHON_INCLUDEDIR is the path of your python include directory (use at your own risk!):

set LIBSVMDIR=~/python
set PYTHON_INCLUDEDIR=/usr/include/python2.6
wget ""
tar -zxf libsvm-3.1.tar.gz
cd libsvm-3.1
cd python

Finally, you’ll need to add $MLIBSVMDIR/python to your PYTHONPATH.

Currently, learner.third_party.libsvm contains the following modules:

  • learning.third_party.libsvm.classification: SVM classifier based on the LIBSVM library.

LIBSVM Classifiers

The learners.third_party.libsvm.classification module contains an SVN classifier based on the LIBSVM library:

  • SVMClassifier: Classifier using LIBSVM’s Support Vector Machine implementation.
class learners.third_party.libsvm.classification.SVMClassifier(kernel='linear', degree=3, gamma=1, coef0=0, C=1, tolerance=0.001, cache_size=100, shrinking=True, output_probabilities=False, label_weights=None)[source]

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 function. Option kernel (which can be either 'linear', 'polynomial', 'rbf' or 'sigmoid') determines the type of kernel.

Weights to examples of different classes can be given using option label_weights, which must be a dictionary mapping from the label (string) to the weight (float).

The SVM will also output probabilities if option 'output_probabilities' is True.

Other options are the same as those in the LIBSVM implementation (see for more details).

Required metadata:

  • 'targets'
  • 'class_to_id'

Trains the SVM.


Outputs the class_id chosen by the algorithm. If output_probabilities is True, also outputs the vector of probabilities.


Outputs the result of use(dataset) and the classification error cost for each example in the dataset