Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #406 from puffin444/master
Gaussian Process Regression
- Loading branch information
Showing
8 changed files
with
607 additions
and
2 deletions.
There are no files selected for viewing
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
78 changes: 78 additions & 0 deletions
78
examples/undocumented/libshogun/regression_gaussian_process.cpp
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
|
||
|
||
|
30 changes: 30 additions & 0 deletions
30
examples/undocumented/python_modular/regression_gaussian_process_modular.py
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
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
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
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
Oops, something went wrong.