Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 6509213

Browse filesBrowse files
authored
fixed badges and added tests for gp reg (#7)
* added tests for gp reg * fixed badges
1 parent e704002 commit 6509213
Copy full SHA for 6509213

File tree

Expand file treeCollapse file tree

3 files changed

+116
-126
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+116
-126
lines changed

‎.travis.yml

Copy file name to clipboardExpand all lines: .travis.yml
+6-6Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ install:
2222
- pip install coveralls travis-sphinx==2.0.0
2323

2424
env:
25-
- PYTHON_VERSION=2.7 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append pmlearn/tests/test_base.py pmlearn/linear_model/tests/test_base.py pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
26-
- PYTHON_VERSION=2.7 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append --ignore=pmlearn/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
27-
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append pmlearn/tests/test_base.py pmlearn/linear_model/tests/test_base.py pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
28-
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append --ignore=pmlearn/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
29-
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append pmlearn/tests/test_base.py pmlearn/linear_model/tests/test_base.py pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
30-
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append --ignore=pmlearn/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
25+
- PYTHON_VERSION=2.7 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=50 --cov-append pmlearn/tests/test_base.py pmlearn/linear_model/tests/test_base.py pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
26+
- PYTHON_VERSION=2.7 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=50 --cov-append pmlearn/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_logistic.py pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
27+
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=50 --cov-append pmlearn/tests/test_base.py pmlearn/linear_model/tests/test_base.py pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
28+
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=50 --cov-append pmlearn/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_logistic.py pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
29+
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=50 --cov-append pmlearn/tests/test_base.py pmlearn/linear_model/tests/test_base.py pmlearn/linear_model/tests/test_logistic.py --ignore=pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
30+
- PYTHON_VERSION=3.6 FLOATX='float64' RUN_PYLINT="true" TESTCMD="--durations=50 --cov-append pmlearn/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_base.py --ignore=pmlearn/linear_model/tests/test_logistic.py pmlearn/gaussian_process/tests/test_gpr.py --ignore=pmlearn/mixture/tests/test_gaussian_mixture.py --ignore=pmlearn/mixture/tests/test_dirichlet_process.py --ignore=pmlearn/naive_bayes/tests/test_naive_bayes.py --ignore=pmlearn/neural_network/test_multilayer_perceptron.py"
3131

3232
script:
3333
- . ./scripts/test.sh $TESTCMD

‎README.rst

Copy file name to clipboardExpand all lines: README.rst
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -259,11 +259,11 @@ project: https://github.com/parsing-science/pymc3_models.
259259
.. |Binder| image:: https://mybinder.org/badge.svg
260260
:target: https://mybinder.org/v2/gh/pymc-learn/pymc-learn/master?filepath=%2Fdocs%2Fnotebooks?urlpath=lab
261261

262-
.. |Travis| image:: https://api.travis-ci.org/pymc-learn/pymc-learn.svg?branch=master
263-
:target: https://travis-ci.org/pymc-learn/pymc-learn
262+
.. |Travis| image:: https://travis-ci.com/pymc-learn/pymc-learn.svg?branch=master
263+
:target: https://travis-ci.com/pymc-learn/pymc-learn
264264

265-
.. |Coverage| image:: https://coveralls.io/repos/github/pymc-learn/pymc-learn/badge.svg?branch=master
266-
:target: https://coveralls.io/github/pymc-learn/pymc-learn?branch=master
265+
.. |Coverage| image:: https://coveralls.io/repos/github/pymc-learn/pymc-learn/badge.svg
266+
:target: https://coveralls.io/github/pymc-learn/pymc-learn
267267

268268
.. |Python27| image:: https://img.shields.io/badge/python-2.7-blue.svg
269269
:target: https://badge.fury.io/py/pymc-learn

‎pmlearn/gaussian_process/tests/test_gpr.py

Copy file name to clipboardExpand all lines: pmlearn/gaussian_process/tests/test_gpr.py
+106-116Lines changed: 106 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -4,56 +4,56 @@
44
#
55
# License: BSD 3 clause
66

7-
# import pytest
7+
import pytest
88
import numpy.testing as npt
9-
# import pandas.testing as pdt
9+
import pandas.testing as pdt
1010
import shutil
1111
import tempfile
1212

1313
import numpy as np
1414
import pymc3 as pm
15-
# from pymc3 import summary
16-
# from sklearn.gaussian_process import \
17-
# GaussianProcessRegressor as skGaussianProcessRegressor
18-
# from sklearn.model_selection import train_test_split
19-
#
20-
#
21-
# from pmlearn.exceptions import NotFittedError
15+
from pymc3 import summary
16+
from sklearn.gaussian_process import \
17+
GaussianProcessRegressor as skGaussianProcessRegressor
18+
from sklearn.model_selection import train_test_split
19+
20+
21+
from pmlearn.exceptions import NotFittedError
2222
from pmlearn.gaussian_process import (GaussianProcessRegressor)
2323
# ,
2424
# SparseGaussianProcessRegressor,
2525
# StudentsTProcessRegressor)
2626

2727

2828
class TestGaussianProcessRegressor(object):
29-
"""
30-
Compare the logp of GPR models in pmlearn to sklearn
31-
"""
3229

3330
def setup_method(self):
34-
"""Setup the data for testing
35-
"""
3631
self.num_pred = 1
37-
self.num_training_samples = 20
38-
self.length_scale = 0.1
39-
self.signal_variance = 0.01
40-
self.noise_variance = 0.01
41-
self.X = np.random.randn(self.num_training_samples, self.num_pred)
42-
self.y = np.random.randn(self.num_training_samples) * \
43-
self.noise_variance
44-
self.Xnew = np.random.randn(50, self.num_pred)
45-
self.pnew = np.random.randn(50) * self.noise_variance
46-
with pm.Model() as model:
47-
cov_func = self.signal_variance**2 * \
48-
pm.gp.cov.ExpQuad(self.num_pred, self.length_scale)
49-
gp = pm.gp.Latent(cov_func=cov_func)
50-
f = gp.prior("f", self.X, reparameterize=False)
51-
p = gp.conditional("p", self.Xnew)
52-
53-
self.latent_logp = model.logp({"f": self.y, "p": self.pnew})
54-
self.plogp = p.logp({"f": self.y, "p": self.pnew})
55-
56-
self.test_gpr = GaussianProcessRegressor(kernel=cov_func)
32+
self.num_training_samples = 300
33+
34+
self.length_scale = 1.0
35+
self.signal_variance = 0.1
36+
self.noise_variance = 0.1
37+
38+
X = np.linspace(start=0, stop=10,
39+
num=self.num_training_samples)[:, None]
40+
41+
cov_func = self.signal_variance ** 2 * pm.gp.cov.ExpQuad(
42+
1, self.length_scale)
43+
mean_func = pm.gp.mean.Zero()
44+
45+
f_true = np.random.multivariate_normal(
46+
mean_func(X).eval(),
47+
cov_func(X).eval() + 1e-8 * np.eye(self.num_training_samples),
48+
1).flatten()
49+
y = f_true + \
50+
self.noise_variance * np.random.randn(self.num_training_samples)
51+
52+
self.X_train, self.X_test, self.y_train, self.y_test = \
53+
train_test_split(X, y, test_size=0.3)
54+
55+
self.advi_gpr = GaussianProcessRegressor()
56+
5757
self.test_dir = tempfile.mkdtemp()
5858

5959
def teardown_method(self):
@@ -67,93 +67,83 @@ def test_advi_fit_returns_correct_model(self):
6767
# This print statement ensures PyMC3 output won't overwrite
6868
# the test name
6969
print('')
70-
self.test_gpr.fit(self.X, self.y)
71-
72-
npt.assert_equal(self.num_pred, self.test_gpr.num_pred)
73-
npt.assert_almost_equal(self.signal_variance,
74-
int(self.test_GPR.summary['mean']['signal_variance__0']),
75-
0)
76-
self.assertAlmostEqual(self.length_scale,
77-
int(self.test_GPR.summary['mean']['length_scale__0_0']),
78-
0)
79-
self.assertAlmostEqual(self.noise_variance,
80-
int(self.test_GPR.summary['mean']['noise_variance__0']),
81-
0)
70+
self.advi_gpr.fit(self.X_train, self.y_train,
71+
inference_args={"n": 25000})
8272

83-
# def test_nuts_fit_returns_correct_model(self):
84-
# # This print statement ensures PyMC3 output won't overwrite the test name
85-
# print('')
86-
# self.test_nuts_GPR.fit(self.X_train, self.y_train, inference_type='nuts')
87-
#
88-
# self.assertEqual(self.num_pred, self.test_nuts_GPR.num_pred)
89-
# self.assertAlmostEqual(self.signal_variance,
90-
# int(self.test_nuts_GPR.summary['mean']['signal_variance__0']),
91-
# 0)
92-
# self.assertAlmostEqual(self.length_scale,
93-
# int(self.test_nuts_GPR.summary['mean']['length_scale__0_0']),
94-
# 0)
95-
# self.assertAlmostEqual(self.noise_variance,
96-
# int(self.test_nuts_GPR.summary['mean']['noise_variance__0']),
97-
# 0)
73+
npt.assert_equal(self.num_pred, self.advi_gpr.num_pred)
74+
npt.assert_almost_equal(
75+
self.signal_variance,
76+
self.advi_gpr.summary['mean']['signal_variance__0'],
77+
0)
78+
npt.assert_almost_equal(
79+
self.length_scale,
80+
self.advi_gpr.summary['mean']['length_scale__0_0'],
81+
0)
82+
npt.assert_almost_equal(
83+
self.noise_variance,
84+
self.advi_gpr.summary['mean']['noise_variance__0'],
85+
0)
9886

9987

100-
# class GaussianProcessRegressorPredictTestCase(GaussianProcessRegressorTestCase):
101-
# def test_predict_returns_predictions(self):
102-
# print('')
103-
# self.test_GPR.fit(self.X_train, self.y_train)
104-
# preds = self.test_GPR.predict(self.X_test)
105-
# self.assertEqual(self.y_test.shape, preds.shape)
106-
#
107-
# def test_predict_returns_mean_predictions_and_std(self):
108-
# print('')
109-
# self.test_GPR.fit(self.X_train, self.y_train)
110-
# preds, stds = self.test_GPR.predict(self.X_test, return_std=True)
111-
# self.assertEqual(self.y_test.shape, preds.shape)
112-
# self.assertEqual(self.y_test.shape, stds.shape)
113-
#
114-
# def test_predict_raises_error_if_not_fit(self):
115-
# print('')
116-
# with self.assertRaises(NotFittedError) as no_fit_error:
117-
# test_GPR = GaussianProcessRegressor()
118-
# test_GPR.predict(self.X_train)
119-
#
120-
# expected = 'Run fit on the model before predict.'
121-
# self.assertEqual(str(no_fit_error.exception), expected)
88+
class TestGaussianProcessRegressorPredict(TestGaussianProcessRegressor):
89+
def test_predict_returns_predictions(self):
90+
print('')
91+
self.advi_gpr.fit(self.X_train, self.y_train,
92+
inference_args={"n": 25000})
93+
preds = self.advi_gpr.predict(self.X_test)
94+
npt.assert_equal(self.y_test.shape, preds.shape)
95+
96+
def test_predict_returns_mean_predictions_and_std(self):
97+
print('')
98+
self.advi_gpr.fit(self.X_train, self.y_train,
99+
inference_args={"n": 25000})
100+
preds, stds = self.advi_gpr.predict(self.X_test, return_std=True)
101+
npt.assert_equal(self.y_test.shape, preds.shape)
102+
npt.assert_equal(self.y_test.shape, stds.shape)
103+
104+
def test_predict_raises_error_if_not_fit(self):
105+
print('')
106+
with pytest.raises(NotFittedError):
107+
advi_gpr = GaussianProcessRegressor()
108+
advi_gpr.predict(self.X_train)
109+
110+
111+
class TestGaussianProcessRegressorScore(TestGaussianProcessRegressor):
112+
def test_score_matches_sklearn_performance(self):
113+
print('')
114+
sk_gpr = skGaussianProcessRegressor()
115+
sk_gpr.fit(self.X_train, self.y_train)
116+
sk_gpr_score = sk_gpr.score(self.X_test, self.y_test)
117+
118+
self.advi_gpr.fit(self.X_train, self.y_train,
119+
inference_args={"n": 25000})
120+
advi_gpr_score = self.advi_gpr.score(self.X_test, self.y_test)
121+
122+
npt.assert_almost_equal(sk_gpr_score, advi_gpr_score, 1)
123+
124+
125+
class TestGaussianProcessRegressorSaveAndLoad(TestGaussianProcessRegressor):
126+
def test_save_and_load_work_correctly(self):
127+
print('')
128+
self.advi_gpr.fit(self.X_train, self.y_train,
129+
inference_args={"n": 25000})
130+
score1 = self.advi_gpr.score(self.X_test, self.y_test)
131+
self.advi_gpr.save(self.test_dir)
132+
133+
gpr2 = GaussianProcessRegressor()
134+
gpr2.load(self.test_dir)
135+
136+
npt.assert_equal(self.advi_gpr.inference_type, gpr2.inference_type)
137+
npt.assert_equal(self.advi_gpr.num_pred, gpr2.num_pred)
138+
npt.assert_equal(self.advi_gpr.num_training_samples,
139+
gpr2.num_training_samples)
140+
pdt.assert_frame_equal(summary(self.advi_gpr.trace),
141+
summary(gpr2.trace))
142+
143+
score2 = gpr2.score(self.X_test, self.y_test)
144+
npt.assert_almost_equal(score1, score2, 0)
122145

123146

124-
# class GaussianProcessRegressorScoreTestCase(GaussianProcessRegressorTestCase):
125-
# def test_score_matches_sklearn_performance(self):
126-
# print('')
127-
# skGPR = skGaussianProcessRegressor()
128-
# skGPR.fit(self.X_train, self.y_train)
129-
# skGPR_score = skGPR.score(self.X_test, self.y_test)
130-
#
131-
# self.test_GPR.fit(self.X_train, self.y_train)
132-
# test_GPR_score = self.test_GPR.score(self.X_test, self.y_test)
133-
#
134-
# self.assertAlmostEqual(skGPR_score, test_GPR_score, 1)
135-
#
136-
#
137-
# class GaussianProcessRegressorSaveAndLoadTestCase(GaussianProcessRegressorTestCase):
138-
# def test_save_and_load_work_correctly(self):
139-
# print('')
140-
# self.test_GPR.fit(self.X_train, self.y_train)
141-
# score1 = self.test_GPR.score(self.X_test, self.y_test)
142-
# self.test_GPR.save(self.test_dir)
143-
#
144-
# GPR2 = GaussianProcessRegressor()
145-
# GPR2.load(self.test_dir)
146-
#
147-
# self.assertEqual(self.test_GPR.inference_type, GPR2.inference_type)
148-
# self.assertEqual(self.test_GPR.num_pred, GPR2.num_pred)
149-
# self.assertEqual(self.test_GPR.num_training_samples, GPR2.num_training_samples)
150-
# pd.testing.assert_frame_equal(summary(self.test_GPR.trace),
151-
# summary(GPR2.trace))
152-
#
153-
# score2 = GPR2.score(self.X_test, self.y_test)
154-
# self.assertAlmostEqual(score1, score2, 1)
155-
#
156-
#
157147
# class StudentsTProcessRegressorTestCase(unittest.TestCase):
158148
#
159149
# def setUp(self):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.