SHOGUN's "modular" interfaces to Python and Octave give intuitive and easy access to the shogun's functionality. Compared to the static interfaces ( Static Interfaces), the modular ones are much more flexible and allow for nearly unlimited extensibility.

If this is your first time using shogun, you've found the right place to start!

In this tutorial, we demonstrate how to use shogun to create a simple Gaussian kernel based Support Vector Machine (SVM) classifier, but first things first. Let's fire up python or octave and load the modular shogun environment.

To load all of shogun's modules under octave start octave and issue

init_shogun

Under python we have to specify what we'd like to import. For this example, we will need features and labels to represent our input data, a classifier, a kernel to compare the similarity of features and some evaluation procedures.

from shogun.Features import * from shogun.Kernel import * from shogun.Classifier import * from shogun.Evaluation import *

(Note here that the module shogun.Features contains the class Labels).

If you would like to get an overview of the classes in shogun or if you are looking for the documentation of a particular class, use the "Classes" tab above and browse through the class list. All classes are rather well documented (if documentation is not good enough in a particular class, please notify us). Note that all classes in the classes list (classes tab above) are all prefixed with a 'C' - that really is the only difference to using them from within the python or octave modular interfaces (where the C prefix is missing).

Alternatively, under python the same documentation is available via python help strings, so you may issue

help(<classname>)

for example

from shogun.Kernel import GaussianKernel help(GaussianKernel)

or

import shogun.Kernel help(shogun.Kernel)

To start with, we will generate a small toy dataset. In python, we need to import numpy to do this. We will generate real valued training (and testing) data drawn from Gaussians distribution. We will generate two Gauss bumps that are "dist" apart. The data is in matrix shape with each column describing an object and as many columns as we have examples. Additionally, we need a vector of labels (that is a vector of ones or minus ones) that indicates the class of each example.

from numpy import * from numpy.random import randn dist=0.5 traindata_real = concatenate((randn(2,100)-dist, randn(2,100)+dist), axis=1) testdata_real = concatenate((randn(2,100)-dist, randn(2,100)+dist), axis=1) train_labels = concatenate((-ones(100), ones(100))) test_labels = concatenate((-ones(100), ones(100)))

In octave, this is is done as follows:

dist=0.5 traindata_real = [randn(2,100)-dist, randn(2,100)+dist]; testdata_real = [randn(2,100)-dist, randn(2,100)+dist]; train_labels = [-ones(1,100), ones(1,100)]; test_labels = [-ones(1,100), ones(1,100)];

The rest of this tutorial below will now work the same (identical syntax) for python and octave (when using a trailing semicolon for each command, which is optional in python).

To process the above toy data in shogun, we need to create a shogun feature object, here RealFeatures (for dense real valued feature matrices, see also shogun::CSimpleFeatures) like this

feats_train = RealFeatures(traindata_real); feats_test = RealFeatures(testdata_real);

Using the above feature object we can now create a kernel object. Here, we create a Gaussian kernel of a certain width (see also shogun::CGaussianKernel) based on our training features

width = 2; kernel = GaussianKernel(feats_train, feats_train, width);

and can now compute the kernel matrix

km = kernel.get_kernel_matrix();

To train an SVM, we need labeled examples, which is a vector of ones and minus ones, such as the one we have previously stored in the variable train_labels. We now create a shogun label object from it (the same goes for our test labels, which we'll use later):

labels = Labels(train_labels); labels_test = Labels(test_labels);

Given the labels object and the kernel all that is left to do is to specify a cost parameter C (used to control generalization performance) and we can construct an SVM object. To start the training, we simply invoke the train method of the SVM object. Quiet easy, isn't it?

C = 1.0; svm = LibSVM(C, kernel, labels); svm.train();

To apply the SVM to unseen test data, we simply need to pass a feature object to the SVM's classify method, which returns a shogun Label object (note that we could alternatively initialize the kernel object with the train and test data manually and then call classify without arguments, which is done in some of the other example scripts). If we would like to analyze the outputs directly in python/octave, we can obtain the vector of outputs in native python/octave representation via get_labels().

output = svm.classify(feats_test); output_vector = output.get_labels();

Given the output and the test labels, we can now assess the prediction performance. For this, we create an instance of the class PerformanceMeasures, which provides a convenient way of obtaining various performance measures, such as accuracy (acc), area under the receiver operator characteristic curve (auROC), F-score and others:

pm = PerformanceMeasures(labels_test, output); acc = pm.get_accuracy(); roc = pm.get_auROC(); fms = pm.get_fmeasure();

That's really it. For any of the advanced topics please have a look one of the **many** self-explanatory examples in

- examples/octave-modular also available online here
- examples/python-modular also available online here

A full, working example similar to the one presented above is shown below:

For octave:

% In this example a two-class support vector machine classifier is trained on a % 2-dimensional randomly generated data set and the trained classifier is used to % predict labels of test examples. As training algorithm the LIBSVM solver is used % with SVM regularization parameter C=1 and a Gaussian kernel of width 2.1. % % For more details on LIBSVM solver see http://www.csie.ntu.edu.tw/~cjlin/libsvm/ init_shogun num=1000; dist=1; width=2.1; C=1; traindata_real=[randn(2,num)-dist, randn(2,num)+dist]; testdata_real=[randn(2,num)-dist, randn(2,num)+dist]; trainlab=[-ones(1,num), ones(1,num)]; testlab=[-ones(1,num), ones(1,num)]; feats_train=RealFeatures(traindata_real); feats_test=RealFeatures(testdata_real); feats_test.copy_feature_matrix(testdata_real); kernel=GaussianKernel(feats_train, feats_train, width); labels=Labels(trainlab); svm=LibSVM(C, kernel, labels); svm.parallel.set_num_threads(8); svm.train(); kernel.init(feats_train, feats_test); out=svm.classify().get_labels(); testerr=mean(sign(out)~=testlab)

For python:

# In this example a two-class support vector machine classifier is trained on a # 2-dimensional randomly generated data set and the trained classifier is used to # predict labels of test examples. As training algorithm the LIBSVM solver is used # with SVM regularization parameter C=1 and a Gaussian kernel of width 2.1. # # For more details on LIBSVM solver see http://www.csie.ntu.edu.tw/~cjlin/libsvm/ from numpy import * from numpy.random import randn from shogun.Features import * from shogun.Classifier import * from shogun.Kernel import * num=1000 dist=1 width=2.1 C=1 traindata_real=concatenate((randn(2,num)-dist, randn(2,num)+dist), axis=1) testdata_real=concatenate((randn(2,num)-dist, randn(2,num)+dist), axis=1); trainlab=concatenate((-ones(num), ones(num))); testlab=concatenate((-ones(num), ones(num))); feats_train=RealFeatures(traindata_real); feats_test=RealFeatures(testdata_real); kernel=GaussianKernel(feats_train, feats_train, width); labels=Labels(trainlab); svm=LibSVM(C, kernel, labels); svm.train(); kernel.init(feats_train, feats_test); out=svm.classify().get_labels(); testerr=mean(sign(out)!=testlab) print testerr

SHOGUN Machine Learning Toolbox - Documentation