SHOGUN  4.1.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
StaticTutorial.mainpage
Go to the documentation of this file.
1 /*!
2 \page static_tutorial Tutorial for Static Interfaces
3 
4 SHOGUN provides "static" interfaces to Matlab(tm), R, Python, Octave and
5 provides a command line stand-a-lone executable. The idea behind the static
6 interfaces is to provide a simple environment just enough to do simple
7 experiments. For example, it will allow you to train and evaluate a classifier
8 but not go beyond that. In case you are looking for basically unlimited
9 extensibility (multiple methods like classifiers potentially sharing data and
10 interacting) you might want to look at the \subpage modular_tutorial "Modular
11 Interfaces" instead.
12 
13 In this tutorial we demonstrate how to use shogun to create a simple
14 gaussian kernel based support vector machine classifier but first
15 things first. Lets start up R, python, octave or matlab load the shogun
16 environment.
17 
18 \section start_shogun_static Starting SHOGUN
19 To start SHOGUN in python, start python and type
20 
21 \verbatim
22  from sg import sg
23 \endverbatim
24 
25 For R issue (from within R)
26 
27 \verbatim
28  library(sg)
29 \endverbatim
30 
31 For octave and matlab just make sure sg is in the path (use addpath). For the
32 cmdline interface just start the shogun executable
33 
34 Now in all languages
35 
36 \verbatim
37  sg('help')
38 \endverbatim
39 
40 and
41 
42 \verbatim
43  help
44 \endverbatim
45 
46 in the cmdline interface will show the help screen. If not consult
47 \subpage installation on how to install shogun.
48 
49 \section svm_tutorial_static Creating an SVM classifier
50 
51 The rest of this tutorial assumes that the cmdline shogun executable
52 is used (but hints on how things work using other interfaces). The
53 basic syntax is
54 \verbatim
55  <command> <option1> <option2> ...
56 \endverbatim
57 here options are separated by spaces. For example
58 
59 \verbatim
60 set_kernel GAUSSIAN REAL 10 1.2
61 \endverbatim
62 
63 will create a gaussian kernel that operates on real-valued features uses a
64 kernel cache of size 10 MB and kernel width of 1.2. In analogy the other the
65 cmdline for the other interfaces (python,r,...) would look like
66 
67 \verbatim
68 sg('set_kernel', 'GAUSSIAN', 'REAL', 10, 1.2)
69 \endverbatim
70 
71 Note that there is little difference to the other interfaces, basically only
72 strings are marked as such and arguments comma separated.
73 
74 We now use two random gaussians as inputs as train data:
75 \verbatim
76 set_features TRAIN ../data/fm_train_real.dat
77 \endverbatim
78 
79 (For other interfaces sth. like
80 \verbatim
81 sg('set_features', 'TRAIN', [ randn(2, 100)-1, randn(2,100)+1 ])
82 \endverbatim
83 would work).
84 
85 For training a supervised method like an SVM we need a labeling of the training
86 data, which we set via
87 \verbatim
88 set_labels TRAIN ../data/label_train_twoclass.dat
89 \endverbatim
90 
91 (For other interfaces, e.g. matlab/octave sth. like
92 \verbatim
93 sg('set_labels', 'TRAIN', sign(randn(1, 100)))
94 \endverbatim
95 would work)
96 
97 Now we create an SVM and set the SVM-C parameter to some hopefully sane value
98 (which in real applications needs tuning; like the kernel parameters (here
99 kernel width)).
100 \verbatim
101 new_classifier LIBSVM
102 c 1
103 \endverbatim
104 
105 We then train our SVM:
106 \verbatim
107 train_classifier
108 \endverbatim
109 
110 We can now apply our classifier to unseen test data by loading some
111 test data and classifying the examples:
112 
113 \verbatim
114 set_features TEST ../data/fm_test_real.dat
115 out.txt = classify
116 \endverbatim
117 
118 In case we want to save the classifiers, such that we don't have to perform
119 potentially time consuming training again we can save and load like this
120 
121 \verbatim
122 save_classifier libsvm.model
123 
124 load_classifier libsvm.model LIBSVM
125 \endverbatim
126 
127 Other interfaces (python,r...) could use the load/save functions but typically
128 one manually obtains and restores the model parameters, like
129 
130 \verbatim
131 [b,alphas]=sg('get_classifier')
132 
133 sg('set_classifier', b, alphas)
134 \endverbatim
135 
136 in this case.
137 
138 Finally, the complete example looks like this
139 \verbinclude classifier_libsvm.sg
140 
141 and can be found together with many other \subpage examples "examples"
142 in examples/cmdline/classifier_libsvm.sg .
143 
144 For users of other interfaces we show the translated example for completeness:
145 
146 \li Static R Interface \verbinclude classifier_libsvm.R
147 \li Static Matlab/Octave Interface \verbinclude classifier_libsvm.m
148 \li Static Python Interface \verbinclude classifier_libsvm.py
149 */

SHOGUN Machine Learning Toolbox - Documentation