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 70fd9ae

Browse filesBrowse files
committed
added tests for logistic reg
1 parent 9ac9a1d commit 70fd9ae
Copy full SHA for 70fd9ae

File tree

Expand file treeCollapse file tree

5 files changed

+119
-83
lines changed
Filter options
Expand file treeCollapse file tree

5 files changed

+119
-83
lines changed

‎.travis.yml

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

2424
env:
25-
- PYTHON_VERSION=2.7 FLOATX='float32' RUN_PYLINT="true" TESTCMD="--durations=10 --cov-append pmlearn/tests/test_base.py 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"
26-
- 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 --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 --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"
28-
- 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 --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='float32' 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 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"
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 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"
2929

3030
script:
3131
- . ./scripts/test.sh $TESTCMD

‎pmlearn/gaussian_process/tests/test_gpr.py

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

7-
import pytest
7+
# import pytest
88
import numpy.testing as npt
99
import shutil
1010
import tempfile
1111

1212
import numpy as np
13-
import pandas as pd
13+
# import pandas as pd
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
15+
# from pymc3 import summary
16+
# from sklearn.gaussian_process import \
17+
# GaussianProcessRegressor as skGaussianProcessRegressor
18+
# from sklearn.model_selection import train_test_split
1919

2020

21-
from pmlearn.exceptions import NotFittedError
22-
from pmlearn.gaussian_process import (GaussianProcessRegressor,
23-
SparseGaussianProcessRegressor,
24-
StudentsTProcessRegressor)
21+
# from pmlearn.exceptions import NotFittedError
22+
from pmlearn.gaussian_process import (GaussianProcessRegressor)
23+
# ,
24+
# SparseGaussianProcessRegressor,
25+
# StudentsTProcessRegressor)
2526

2627

2728
class TestGaussianProcessRegressor(object):
+96-61Lines changed: 96 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,135 +1,170 @@
1+
"""Testing for Logistic regression """
2+
3+
# Authors: Daniel Emaasit <daniel.emaasit@gmail.com>
4+
#
5+
# License: BSD 3 clause
6+
7+
import pytest
8+
import numpy.testing as npt
9+
import pandas.testing as pdt
110
import shutil
211
import tempfile
3-
import unittest
412

513
import numpy as np
6-
import pandas as pd
714
from pymc3 import summary
815
from sklearn.model_selection import train_test_split
916

1017
from pmlearn.exceptions import NotFittedError
1118
from pmlearn.linear_model import HierarchicalLogisticRegression
1219

1320

14-
class HierarchicalLogisticRegressionTestCase(unittest.TestCase):
15-
def setUp(self):
21+
class TestHierarchicalLogisticRegression(object):
22+
def setup_method(self):
1623
def numpy_invlogit(x):
1724
return 1 / (1 + np.exp(-x))
1825

1926
self.num_cats = 3
2027
self.num_pred = 1
21-
self.num_samples_per_cat = 100000
28+
self.num_samples_per_cat = 1000
2229

2330
self.alphas = np.random.randn(self.num_cats)
2431
self.betas = np.random.randn(self.num_cats, self.num_pred)
25-
#TODO: make this more efficient; right now, it's very explicit so I understand it.
32+
# TODO: make this more efficient; right now, it's very explicit
33+
# so I understand it.
2634
x_a = np.random.randn(self.num_samples_per_cat, self.num_pred)
27-
y_a = np.random.binomial(1, numpy_invlogit(self.alphas[0] + np.sum(self.betas[0] * x_a, 1)))
35+
y_a = np.random.binomial(1,
36+
numpy_invlogit(self.alphas[0] +
37+
np.sum(self.betas[0] * x_a, 1)
38+
))
2839
x_b = np.random.randn(self.num_samples_per_cat, self.num_pred)
29-
y_b = np.random.binomial(1, numpy_invlogit(self.alphas[1] + np.sum(self.betas[1] * x_b, 1)))
40+
y_b = np.random.binomial(1,
41+
numpy_invlogit(self.alphas[1] +
42+
np.sum(self.betas[1] * x_b, 1)
43+
))
3044
x_c = np.random.randn(self.num_samples_per_cat, self.num_pred)
31-
y_c = np.random.binomial(1, numpy_invlogit(self.alphas[2] + np.sum(self.betas[2] * x_c, 1)))
45+
y_c = np.random.binomial(1,
46+
numpy_invlogit(self.alphas[2] +
47+
np.sum(self.betas[2] * x_c, 1)
48+
))
3249

3350
X = np.concatenate([x_a, x_b, x_c])
34-
Y = np.concatenate([y_a, y_b, y_c])
51+
y = np.concatenate([y_a, y_b, y_c])
3552
cats = np.concatenate([
3653
np.zeros(self.num_samples_per_cat, dtype=np.int),
3754
np.ones(self.num_samples_per_cat, dtype=np.int),
3855
2*np.ones(self.num_samples_per_cat, dtype=np.int)
3956
])
4057

41-
self.X_train, self.X_test, self.cat_train, self.cat_test, self.Y_train, self.Y_test = train_test_split(
42-
X, cats, Y, test_size=0.4
58+
self.X_train, self.X_test, self.cat_train, self.cat_test, \
59+
self.y_train, self.y_test = train_test_split(
60+
X, cats, y, test_size=0.4
4361
)
4462

45-
self.test_HLR = HierarchicalLogisticRegression()
63+
self.advi_hlr = HierarchicalLogisticRegression()
4664

4765
self.test_dir = tempfile.mkdtemp()
4866

49-
def tearDown(self):
67+
def teardown_method(self):
5068
shutil.rmtree(self.test_dir)
5169

5270

53-
class HierarchicalLogisticRegressionFitTestCase(HierarchicalLogisticRegressionTestCase):
54-
def test_fit_returns_correct_model(self):
71+
class TestHierarchicalLogisticRegressionFit(TestHierarchicalLogisticRegression):
72+
def test_advi_fit_returns_correct_model(self):
5573
# Note: print is here so PyMC3 output won't overwrite the test name
5674
print('')
57-
self.test_HLR.fit(self.X_train, self.Y_train, self.cat_train, minibatch_size=2000)
75+
self.advi_hlr.fit(self.X_train, self.y_train, self.cat_train,
76+
minibatch_size=500, inference_args={"n": 50000})
5877

59-
self.assertEqual(self.num_cats, self.test_HLR.num_cats)
60-
self.assertEqual(self.num_pred, self.test_HLR.num_pred)
78+
npt.assert_equal(self.num_cats, self.advi_hlr.num_cats)
79+
npt.assert_equal(self.num_pred, self.advi_hlr.num_pred)
6180

6281
#TODO: Figure out best way to test
63-
#np.testing.assert_almost_equal(self.alphas, self.test_HLR.trace['alphas'].mean(), decimal=1)
64-
#np.testing.assert_almost_equal(self.betas, self.test_HLR.trace['betas'].mean(), decimal=1)
65-
66-
# For now, just check that the estimated parameters have the correct signs
67-
np.testing.assert_equal(
82+
#np.testing.assert_almost_equal(self.alphas,
83+
# self.advi_hlr.trace['alphas'].mean(), decimal=1)
84+
#np.testing.assert_almost_equal(self.betas,
85+
# self.advi_hlr.trace['betas'].mean(), decimal=1)
86+
87+
# For now, just check that the estimated parameters
88+
# have the correct signs
89+
npt.assert_equal(
6890
np.sign(self.alphas),
69-
np.sign(self.test_HLR.trace['alpha'].mean(axis=0))
91+
np.sign(self.advi_hlr.trace['alpha'].mean(axis=0))
7092
)
71-
np.testing.assert_equal(
93+
npt.assert_equal(
7294
np.sign(self.betas),
73-
np.sign(self.test_HLR.trace['beta'].mean(axis=0))
95+
np.sign(self.advi_hlr.trace['beta'].mean(axis=0))
7496
)
7597

7698

77-
class HierarchicalLogisticRegressionPredictProbaTestCase(HierarchicalLogisticRegressionTestCase):
99+
class TestHierarchicalLogisticRegressionPredictProba(
100+
TestHierarchicalLogisticRegression):
101+
78102
def test_predict_proba_returns_probabilities(self):
79103
print('')
80-
self.test_HLR.fit(self.X_train, self.Y_train, self.cat_train, minibatch_size=2000)
81-
probs = self.test_HLR.predict_proba(self.X_test, self.cat_test)
82-
self.assertEqual(probs.shape, self.Y_test.shape)
104+
self.advi_hlr.fit(self.X_train, self.y_train, self.cat_train,
105+
minibatch_size=500, inference_args={"n": 50000})
106+
probs = self.advi_hlr.predict_proba(self.X_test, self.cat_test)
107+
npt.assert_equal(probs.shape, self.y_test.shape)
83108

84109
def test_predict_proba_returns_probabilities_and_std(self):
85110
print('')
86-
self.test_HLR.fit(self.X_train, self.Y_train, self.cat_train, minibatch_size=2000)
87-
probs, stds = self.test_HLR.predict_proba(self.X_test, self.cat_test, return_std=True)
88-
self.assertEqual(probs.shape, self.Y_test.shape)
89-
self.assertEqual(stds.shape, self.Y_test.shape)
111+
self.advi_hlr.fit(self.X_train, self.y_train, self.cat_train,
112+
minibatch_size=500, inference_args={"n": 50000})
113+
probs, stds = self.advi_hlr.predict_proba(self.X_test, self.cat_test,
114+
return_std=True)
115+
npt.assert_equal(probs.shape, self.y_test.shape)
116+
npt.assert_equal(stds.shape, self.y_test.shape)
90117

91118
def test_predict_proba_raises_error_if_not_fit(self):
92-
with self.assertRaises(NotFittedError) as no_fit_error:
93-
test_HLR = HierarchicalLogisticRegression()
94-
test_HLR.predict_proba(self.X_train, self.cat_train)
119+
with pytest.raises(NotFittedError):
120+
advi_hlr = HierarchicalLogisticRegression()
121+
advi_hlr.predict_proba(self.X_train, self.cat_train)
95122

96-
expected = 'Run fit on the model before predict.'
97-
self.assertEqual(str(no_fit_error.exception), expected)
98123

124+
class TestHierarchicalLogisticRegressionPredict(
125+
TestHierarchicalLogisticRegression):
99126

100-
class HierarchicalLogisticRegressionPredictTestCase(HierarchicalLogisticRegressionTestCase):
101127
def test_predict_returns_predictions(self):
102128
print('')
103-
self.test_HLR.fit(self.X_train, self.Y_train, self.cat_train, minibatch_size=2000)
104-
preds = self.test_HLR.predict(self.X_test, self.cat_test)
105-
self.assertEqual(preds.shape, self.Y_test.shape)
129+
self.advi_hlr.fit(self.X_train, self.y_train, self.cat_train,
130+
minibatch_size=500, inference_args={"n": 50000})
131+
preds = self.advi_hlr.predict(self.X_test, self.cat_test)
132+
npt.assert_equal(preds.shape, self.y_test.shape)
133+
106134

135+
class TestHierarchicalLogisticRegressionScore(
136+
TestHierarchicalLogisticRegression):
107137

108-
class HierarchicalLogisticRegressionScoreTestCase(HierarchicalLogisticRegressionTestCase):
109138
def test_score_scores(self):
110139
print('')
111-
self.test_HLR.fit(self.X_train, self.Y_train, self.cat_train, minibatch_size=2000)
112-
score = self.test_HLR.score(self.X_test, self.Y_test, self.cat_test)
113-
naive_score = np.mean(self.Y_test)
114-
self.assertGreaterEqual(score, naive_score)
140+
self.advi_hlr.fit(self.X_train, self.y_train, self.cat_train,
141+
minibatch_size=500, inference_args={"n": 50000})
142+
score = self.advi_hlr.score(self.X_test, self.y_test, self.cat_test)
143+
naive_score = np.mean(self.y_test)
144+
npt.assert_array_less(naive_score, score)
145+
115146

147+
class TestHierarchicalLogisticRegressionSaveandLoad(
148+
TestHierarchicalLogisticRegression):
116149

117-
class HierarchicalLogisticRegressionSaveandLoadTestCase(HierarchicalLogisticRegressionTestCase):
118150
def test_save_and_load_work_correctly(self):
119151
print('')
120-
self.test_HLR.fit(self.X_train, self.Y_train, self.cat_train, minibatch_size=2000)
121-
probs1 = self.test_HLR.predict_proba(self.X_test, self.cat_test)
122-
self.test_HLR.save(self.test_dir)
152+
self.advi_hlr.fit(self.X_train, self.y_train, self.cat_train,
153+
minibatch_size=500, inference_args={"n": 50000})
154+
probs1 = self.advi_hlr.predict_proba(self.X_test, self.cat_test)
155+
self.advi_hlr.save(self.test_dir)
123156

124-
HLR2 = HierarchicalLogisticRegression()
157+
hlr2 = HierarchicalLogisticRegression()
125158

126-
HLR2.load(self.test_dir)
159+
hlr2.load(self.test_dir)
127160

128-
self.assertEqual(self.test_HLR.num_cats, HLR2.num_cats)
129-
self.assertEqual(self.test_HLR.num_pred, HLR2.num_pred)
130-
self.assertEqual(self.test_HLR.num_training_samples, HLR2.num_training_samples)
131-
pd.testing.assert_frame_equal(summary(self.test_HLR.trace), summary(HLR2.trace))
161+
npt.assert_equal(self.advi_hlr.num_cats, hlr2.num_cats)
162+
npt.assert_equal(self.advi_hlr.num_pred, hlr2.num_pred)
163+
npt.assert_equal(self.advi_hlr.num_training_samples,
164+
hlr2.num_training_samples)
165+
pdt.assert_frame_equal(summary(self.advi_hlr.trace),
166+
summary(hlr2.trace))
132167

133-
probs2 = HLR2.predict_proba(self.X_test, self.cat_test)
168+
probs2 = hlr2.predict_proba(self.X_test, self.cat_test)
134169

135-
np.testing.assert_almost_equal(probs2, probs1, decimal=1)
170+
npt.assert_almost_equal(probs2, probs1, decimal=1)

‎pmlearn/mixture/tests/test_dirichlet_process.py

Copy file name to clipboardExpand all lines: pmlearn/mixture/tests/test_dirichlet_process.py
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import tempfile
44

55
import numpy as np
6-
import pandas as pd
7-
import pymc3 as pm
8-
from pymc3 import summary
9-
from sklearn.mixture import BayesianGaussianMixture as skBayesianGaussianMixture
6+
# import pandas as pd
7+
# import pymc3 as pm
8+
# from pymc3 import summary
9+
# from sklearn.mixture import BayesianGaussianMixture as skBayesianGaussianMixture
1010
from sklearn.model_selection import train_test_split
1111

1212
from pmlearn.exceptions import NotFittedError

‎pmlearn/mixture/tests/test_gaussian_mixture.py

Copy file name to clipboardExpand all lines: pmlearn/mixture/tests/test_gaussian_mixture.py
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import tempfile
44

55
import numpy as np
6-
import pandas as pd
7-
import pymc3 as pm
8-
from pymc3 import summary
9-
from sklearn.mixture import GaussianMixture as skGaussianMixture
6+
# import pandas as pd
7+
# import pymc3 as pm
8+
# from pymc3 import summary
9+
# from sklearn.mixture import GaussianMixture as skGaussianMixture
1010
from sklearn.model_selection import train_test_split
1111

1212
from pmlearn.exceptions import NotFittedError

0 commit comments

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