Skip to content

Commit

Permalink
cookbook page
Browse files Browse the repository at this point in the history
  • Loading branch information
OXPHOS committed Jun 8, 2016
1 parent 4781b40 commit edc55d7
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 38 deletions.
@@ -0,0 +1,43 @@
==========================
Multi-class Linear Machine
==========================

We extend the application of linear machines to multi-class datasets by constructing generic multiclass classifiers with ensembles of binary classifiers.

In this example, we show how to apply :sgclass:`CLibLinear` to multi-class cases with :sgclass:`CLinearMulticlassMachine`.

`See the linear SVM cookbook <http://shogun.ml/cookbook/latest/examples/classifier/linear_svm.html>`_ for the infomration about :sgclass:`CLibLinear` binary classifier.

-------
Example
-------

Imagine we have files with training and test data. We create CDenseFeatures (here 64 bit floats aka RealFeatures) and :sgclass:`CMulticlassLabels` as

.. sgexample:: multiclass_linearmachine.sg:create_features

We use :sgclass:`CLibLinear` as base classifier and create an instance of :sgclass:`CLibLinear`.

.. sgexample:: multiclass_linearmachine.sg:create_classifier

In order to run :sgclass:`CLinearMulticlassMachine`, we need to specify an multi-class strategy from :sgclass:`CMulticlassOneVsRestStrategy` and :sgclass:`CMulticlassOneVsOneStrategy`.

.. sgexample:: multiclass_linearmachine.sg:choose_strategy

We create an instance of the :sgclass:`CLinearMulticlassMachine` classifier by passing it the strategy, dataset, binary classifer and the labels.

.. sgexample:: multiclass_linearmachine.sg:create_instance

Then we train and apply it to test data, which here gives :sgclass:`CMulticlassLabels`.

.. sgexample:: multiclass_linearmachine.sg:train_and_apply

We can evaluate test performance via e.g. :sgclass:`CMulticlassAccuracy`.

.. sgexample:: multiclass_linearmachine.sg:evaluate_accuracy

----------
References
----------

:wiki:`Multiclass_classification`
36 changes: 36 additions & 0 deletions examples/meta/src/classifier/multiclass_linearmachine.sg
@@ -0,0 +1,36 @@
CSVFile f_feats_train("../../data/classifier_4class_2d_linear_features_train.dat")
CSVFile f_feats_test("../../data/classifier_4class_2d_linear_features_test.dat")
CSVFile f_labels_train("../../data/classifier_4class_2d_linear_labels_train.dat")
CSVFile f_labels_test("../../data/classifier_4class_2d_linear_labels_test.dat")

#![create_features]
RealFeatures features_train(f_feats_train)
RealFeatures features_test(f_feats_test)
MulticlassLabels labels_train(f_labels_train)
MulticlassLabels labels_test(f_labels_test)
#![create_features]

#![create_classifier]
LibLinear classifier()
#![create_classifier]

#![choose_strategy]
MulticlassOneVsOneStrategy strategy()
#![choose_strategy]

#![create_instance]
LinearMulticlassMachine mc_classifier(strategy, features_train, classifier, labels_train)
#![create_instance]

#![train_and_apply]
mc_classifier.train()
MulticlassLabels labels_predict = mc_classifier.apply_multiclass(features_test)
#![train_and_apply]

#![evaluate_accuracy]
MulticlassAccuracy eval()
real accuracy = eval.evaluate(labels_predict, labels_test)
#![evaluate_accuracy]

# integration testing variables
RealVector output = labels_predict.get_labels()

This file was deleted.

0 comments on commit edc55d7

Please sign in to comment.