Created
November 6, 2014 13:09
-
-
Save karlnapf/97cee3b334bf78d69466 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# A single Shogun program | |
This documents classififier_libsvm_probabilities.cpp. | |
Cool thing is we can use standard markdown to write the documentation while the source code can be executed. Another advantage is that not all source code is pasted at once. Rather, we can write little paragraphs in between, even including math. | |
## Include and initialisation | |
First, let's include the needed header files and initialise Shogun. | |
However, if this is not documented in this doxypage (one might not want to do that as it is standard), the code still is in the source file and thus can be executed and tested. | |
### Header includes | |
\snippet classifier_libsvm_probabilities.cpp headers | |
### Initialising Shogun | |
\snippet classifier_libsvm_probabilities.cpp init_shogun | |
## SVM | |
First, we define some constants | |
\snippet classifier_libsvm_probabilities.cpp define_constants | |
Then, training an SVM, even using math \f$ f(x)=\sum_{i=1}^N \alpha_i k(x,x_i) \f$ blablabla | |
\snippet classifier_libsvm_probabilities.cpp train_svm | |
## Fitting probabilities to SVM output | |
Convert scores to calibrated probabilities by fitting a sigmoid function | |
using the method described in Lin, H., Lin, C., and Weng, R. (2007). A note | |
on Platt's probabilistic outputs for support vector machines. | |
See CBinaryLabels documentation for details | |
\snippet classifier_libsvm_probabilities.cpp scores_to_probabilities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//! [headers] | |
#include <shogun/base/init.h> | |
#include <shogun/features/DenseFeatures.h> | |
#include <shogun/kernel/LinearKernel.h> | |
#include <shogun/labels/BinaryLabels.h> | |
#include <shogun/classifier/svm/LibSVM.h> | |
#include <iostream> | |
//! [headers] | |
using namespace shogun; | |
void gen_rand_data(SGMatrix<float64_t> features, SGVector<float64_t> labels, float64_t distance) | |
{ | |
index_t num_samples=labels.vlen; | |
index_t dimensions=features.num_rows; | |
for (int32_t i=0; i<num_samples; i++) | |
{ | |
if (i<num_samples/2) | |
{ | |
labels[i]=-1.0; | |
for(int32_t j=0; j<dimensions; j++) | |
features(j,i)=CMath::random(0.0,1.0)+distance; | |
} | |
else | |
{ | |
labels[i]=1.0; | |
for(int32_t j=0; j<dimensions; j++) | |
features(j,i)=CMath::random(0.0,1.0)-distance; | |
} | |
} | |
labels.display_vector("labels"); | |
std::cout<<std::endl; | |
features.display_matrix("features"); | |
std::cout<<std::endl; | |
} | |
int main(int argc, char** argv) | |
{ | |
//! [init_shogun] | |
init_shogun_with_defaults(); | |
//! [init_shogun] | |
//! [define_constants] | |
const float64_t svm_C=10; | |
index_t num_samples=20; | |
index_t dimensions=2; | |
float64_t dist=0.5; | |
//! [define_constants] | |
SGMatrix<float64_t> featureMatrix(dimensions,num_samples); | |
SGVector<float64_t> labelVector(num_samples); | |
gen_rand_data(featureMatrix,labelVector,dist); | |
CLabels* labels=new CBinaryLabels(labelVector); | |
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(); | |
SG_REF(features); | |
features->set_feature_matrix(featureMatrix); | |
CLinearKernel* kernel=new CLinearKernel(); | |
SG_REF(kernel); | |
kernel->init(features, features); | |
//! [train_svm] | |
CLibSVM* svm=new CLibSVM(svm_C,kernel, labels); | |
SG_REF(svm); | |
svm->train(); | |
//! [train_svm] | |
//! [scores_to_probabilities] | |
CBinaryLabels* out_labels=CLabelsFactory::to_binary(svm->apply()); | |
out_labels->scores_to_probabilities(); | |
//! [scores_to_probabilities] | |
for (int32_t i=0; i<num_samples; i++) | |
{ | |
SG_SPRINT("out[%d]=%f (%f)\n", i, out_labels->get_label(i), | |
out_labels->get_value(i)); | |
} | |
SG_UNREF(out_labels); | |
SG_UNREF(kernel); | |
SG_UNREF(features); | |
SG_UNREF(svm); | |
exit_shogun(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment