Skip to content

Commit

Permalink
Merge pull request #406 from puffin444/master
Browse files Browse the repository at this point in the history
Gaussian Process Regression
  • Loading branch information
Soeren Sonnenburg committed Apr 11, 2012
2 parents 73db76e + 6f128a5 commit a27e1ae
Show file tree
Hide file tree
Showing 8 changed files with 607 additions and 2 deletions.
1 change: 1 addition & 0 deletions examples/undocumented/libshogun/Makefile
Expand Up @@ -33,6 +33,7 @@ TARGETS = basic_minimal \
evaluation_cross_validation_regression \
evaluation_cross_validation_locked_comparison \
modelselection_parameter_combination_test \
regression_gaussian_process \
modelselection_model_selection_parameters_test \
modelselection_parameter_tree \
modelselection_apply_parameter_tree \
Expand Down
78 changes: 78 additions & 0 deletions examples/undocumented/libshogun/regression_gaussian_process.cpp
@@ -0,0 +1,78 @@
#include <shogun/features/SimpleFeatures.h>
#include <shogun/io/SGIO.h>
#include <shogun/kernel/GaussianKernel.h>
#include <shogun/regression/GaussianProcessRegression.h>
#include <shogun/features/Labels.h>

/* Example mean prediction from a Gaussian Kernel adapted from
* classifier_minimal_svm.cpp
* Jacob Walker
*/

using namespace shogun;

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

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

#ifdef HAVE_LAPACK

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

//Labels
CLabels* labels = new CLabels(3);
SG_REF(labels);

labels->set_label(0, -1);
labels->set_label(1, +1);
labels->set_label(2, -1);

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

// create gaussian kernel with cache 10MB, width 0.5
CGaussianKernel* kernel = new CGaussianKernel(10, 0.5);
SG_REF(kernel);

//Gaussian Process Regression with sigma = 1.
CGaussianProcessRegression regressor(1.0, kernel, features, labels);

regressor.train(features);
//Get mean predictions
CLabels* result = regressor.apply();
SG_REF(result);

SGMatrix<float64_t> cov = regressor.getCovarianceMatrix(features);

CMath::display_matrix(cov.matrix, cov.num_rows, cov.num_cols, "Covariance Matrix");

// output predictions
for (int32_t i=0; i<3; i++)
SG_SPRINT("output[%d]=%f\n", i, result->get_label(i));

// free up memory
SG_UNREF(result);
SG_UNREF(features);
SG_UNREF(labels);
SG_UNREF(kernel);
cov.destroy_matrix();

#endif

exit_shogun();
return 0;
}



@@ -0,0 +1,30 @@
###########################################################################
# Mean prediction from Gaussian Processes based on classifier_libsvm_minimal_modular.py
###########################################################################
from numpy import *
from numpy.random import randn
from shogun.Features import *
from shogun.Classifier import *
from shogun.Kernel import *

num=100
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);
gp=GaussianProcessRegression(1.0, kernel, feats_train, labels);
gp.train(feats_train);
out=gp.apply(feats_test).get_labels();
testerr=mean(sign(out)!=testlab)
print testerr
2 changes: 2 additions & 0 deletions src/interfaces/modular/Regression.i
Expand Up @@ -13,6 +13,7 @@
%rename(KernelRidgeRegression) CKernelRidgeRegression;
%rename(LinearRidgeRegression) CLinearRidgeRegression;
%rename(LeastSquaresRegression) CLeastSquaresRegression;
%rename(GaussianProcessRegression) CGaussianProcessRegression;
%rename(LibSVR) CLibSVR;
%rename(MKL) CMKL;
%rename(MKLRegression) CMKLRegression;
Expand All @@ -25,6 +26,7 @@
%include <shogun/regression/KernelRidgeRegression.h>
%include <shogun/regression/LinearRidgeRegression.h>
%include <shogun/regression/LeastSquaresRegression.h>
%include <shogun/regression/GaussianProcessRegression.h>
%include <shogun/regression/svr/LibSVR.h>
%include <shogun/classifier/mkl/MKL.h>
%include <shogun/regression/svr/MKLRegression.h>
Expand Down
1 change: 1 addition & 0 deletions src/interfaces/modular/Regression_includes.i
Expand Up @@ -5,6 +5,7 @@
#include <shogun/regression/KernelRidgeRegression.h>
#include <shogun/regression/LinearRidgeRegression.h>
#include <shogun/regression/LeastSquaresRegression.h>
#include <shogun/regression/GaussianProcessRegression.h>
#include <shogun/classifier/svm/SVM.h>
#include <shogun/classifier/svm/LibSVM.h>
#include <shogun/regression/svr/LibSVR.h>
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/machine/Machine.h
Expand Up @@ -72,8 +72,8 @@ enum EClassifierType
CT_LINEARRIDGEREGRESSION = 410,
CT_LEASTSQUARESREGRESSION = 420,
CT_QDA = 430,
CT_NEWTONSVM = 440

CT_NEWTONSVM = 440,
CT_GAUSSIANPROCESSREGRESSION = 450
};

/** solver type */
Expand Down

0 comments on commit a27e1ae

Please sign in to comment.