Skip to content

Commit

Permalink
Merge pull request #463 from Victor-Sadkov/master
Browse files Browse the repository at this point in the history
New statistics sample and various whitespace changes.
  • Loading branch information
Soeren Sonnenburg committed Apr 17, 2012
2 parents 4287096 + 5f73ad6 commit 024fc0a
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 30 deletions.
1 change: 1 addition & 0 deletions examples/undocumented/libshogun/Makefile
Expand Up @@ -75,6 +75,7 @@ TARGETS = basic_minimal \
serialization_basic_tests \
library_cover_tree \
kernel_machine_train_locked \
statistics \

all: $(TARGETS)

Expand Down
53 changes: 53 additions & 0 deletions examples/undocumented/libshogun/statistics.cpp
@@ -0,0 +1,53 @@
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Written (W) 2011 Heiko Strathmann
* Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society
* Written (W) 2012 Victor Sadkov
* Copyright (C) 2011 Moscow State University
*/

#include <shogun/base/init.h>
#include <shogun/mathematics/Statistics.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

void print_message(FILE* target, const char* str)
{
fprintf(target, "%s", str);
}

const int DATA_SIZE=100;

int main(int argc, char **argv)
{
init_shogun(&print_message, &print_message, &print_message);

SGVector<float64_t> data(DATA_SIZE, true);
CMath::random_vector(data.vector, data.vlen, 0.0, 1.0);

// for (int32_t i=0; i<DATA_SIZE; i++)
// SG_SPRINT("data[%02d] = %.5lf%s", i, data[i], (i+1)%4?"\t":"\n");

float64_t low, up, mean;
float64_t error_prob=0.1;
mean=CStatistics::confidence_intervals_mean(data, error_prob, low, up);

SG_SPRINT("sample mean: %f. True mean lies in [%f,%f] with %f%%\n",
mean, low, up, 100*(1-error_prob));

SG_SPRINT("variance: %f\n", CStatistics::variance(data));
SG_SPRINT("deviation: %f\n", CStatistics::std_deviation(data));

data.free_vector();

SG_SPRINT("\nEND\n");
exit_shogun();

return 0;
}

62 changes: 32 additions & 30 deletions src/README.developer
Expand Up @@ -50,57 +50,59 @@ init_shogun(&print_message, &print_warning,
To finally see some action one has to include the appropriate header files,
e.g. we create some features and a gaussian kernel


#include <shogun/features/Labels.h>
#include <shogun/features/RealFeatures.h>
#include <shogun/features/SimpleFeatures.h>
#include <shogun/kernel/GaussianKernel.h>
#include <shogun/classifier/svm/LibSVM.h>
#include <shogun/base/init.h>
#include <shogun/lib/common.h>
#include <shogun/io/SGIO.h>
#include <stdio.h>

using namespace shogun;

void print_message(FILE* target, const char* str)
{
fprintf(target, "%s", str);
fprintf(target, "%s", str);
}

int main(int argc, char** argv)
{
init_shogun(&print_message);
init_shogun(&print_message);

// create some data
float64_t* matrix = new float64_t[6];
for (int32_t i=0; i<6; i++)
matrix[i]=i;
// create some data
float64_t* matrix = SG_MALLOC(float64_t, 6);
for (int32_t i=0; i<6; i++)
matrix[i]=i;

// create three 2-dimensional vectors
// shogun will now own the matrix created
CRealFeatures* features= new CRealFeatures();
features->set_feature_matrix(matrix, 2, 3);
// create three 2-dimensional vectors
// shogun will now own the matrix created
CSimpleFeatures<float64_t>* features= new CSimpleFeatures<float64_t>();
features->set_feature_matrix(matrix, 2, 3);

// create three labels
CLabels* labels=new CLabels(3);
labels->set_label(0, -1);
labels->set_label(1, +1);
labels->set_label(2, -1);
// create three labels
CLabels* labels=new CLabels(3);
labels->set_label(0, -1);
labels->set_label(1, +1);
labels->set_label(2, -1);

// create gaussian kernel with cache 10MB, width 0.5
CGaussianKernel* kernel = new CGaussianKernel(10, 0.5);
kernel->init(features, features);
// create gaussian kernel with cache 10MB, width 0.5
CGaussianKernel* kernel = new CGaussianKernel(10, 0.5);
kernel->init(features, features);

// create libsvm with C=10 and train
CLibSVM* svm = new CLibSVM(10, kernel, labels);
svm->train();
// create libsvm with C=10 and train
CLibSVM* svm = new CLibSVM(10, kernel, labels);
svm->train();

// apply to training examples
for (int32_t i=0; i<3; i++)
SG_SPRINT("output[%d]=%f\n", i, svm->apply(i));
// classify on training examples
for (int32_t i=0; i<3; i++)
SG_SPRINT("output[%d]=%f\n", i, svm->apply(i));

// free up memory
SG_UNREF(svm);
// free up memory
SG_UNREF(svm);

exit_shogun();
return 0;
exit_shogun();
return 0;
}

Now you probably wonder why this example does not leak memory. First of all,
Expand Down

0 comments on commit 024fc0a

Please sign in to comment.