Skip to content

Commit

Permalink
FIX: PPCA related warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
dengemann committed Nov 29, 2013
1 parent 9b1207b commit a0f0a3d
Showing 1 changed file with 23 additions and 28 deletions.
51 changes: 23 additions & 28 deletions sklearn/decomposition/tests/test_pca.py
Expand Up @@ -361,25 +361,23 @@ def test_probabilistic_pca_1():
rng = np.random.RandomState(0)
X = rng.randn(n, p) * .1 + np.array([3, 4, 5])

with warnings.catch_warnings(record=True) as w:
ppca = ProbabilisticPCA(n_components=2)
ppca.fit(X)
ll1 = ppca.score(X)
h = -0.5 * np.log(2 * np.pi * np.exp(1) * 0.1 ** 2) * p
np.testing.assert_almost_equal(ll1.mean() / h, 1, 0)
ppca = assert_warns(DeprecationWarning, ProbabilisticPCA, n_components=2)
ppca.fit(X)
ll1 = ppca.score(X)
h = -0.5 * np.log(2 * np.pi * np.exp(1) * 0.1 ** 2) * p
np.testing.assert_almost_equal(ll1.mean() / h, 1, 0)


def test_probabilistic_pca_2():
"""Test that probabilistic PCA correctly separated different datasets"""
n, p = 100, 3
rng = np.random.RandomState(0)
X = rng.randn(n, p) * .1 + np.array([3, 4, 5])
with warnings.catch_warnings(record=True) as w:
ppca = ProbabilisticPCA(n_components=2)
ppca.fit(X)
ll1 = ppca.score(X)
ll2 = ppca.score(rng.randn(n, p) * .2 + np.array([3, 4, 5]))
assert_greater(ll1.mean(), ll2.mean())
ppca = assert_warns(DeprecationWarning, ProbabilisticPCA, n_components=2)
ppca.fit(X)
ll1 = ppca.score(X)
ll2 = ppca.score(rng.randn(n, p) * .2 + np.array([3, 4, 5]))
assert_greater(ll1.mean(), ll2.mean())


def test_probabilistic_pca_3():
Expand All @@ -389,14 +387,11 @@ def test_probabilistic_pca_3():
n, p = 100, 3
rng = np.random.RandomState(0)
X = rng.randn(n, p) * .1 + np.array([3, 4, 5])
with warnings.catch_warnings(record=True) as w:
ppca = ProbabilisticPCA(n_components=2)
ppca.fit(X)
ll1 = ppca.score(X)
ppca.fit(X, homoscedastic=False)
ll2 = ppca.score(X)
# XXX : Don't test as homoscedastic=False is buggy
# Comment to be removed with ProbabilisticPCA is removed
ppca = assert_warns(DeprecationWarning, ProbabilisticPCA, n_components=2)
ppca.fit(X).score(X)
ppca.fit(X, homoscedastic=False).score(X)
# XXX : Don't test as homoscedastic=False is buggy
# Comment to be removed with ProbabilisticPCA is removed

def test_probabilistic_pca_4():
"""Check that ppca select the right model"""
Expand All @@ -407,11 +402,11 @@ def test_probabilistic_pca_4():
Xt = (rng.randn(n, p) + rng.randn(n, 1) * np.array([3, 4, 5])
+ np.array([1, 0, 7]))
ll = np.zeros(p)
with warnings.catch_warnings(record=True) as w:
for k in range(p):
ppca = ProbabilisticPCA(n_components=k)
ppca.fit(Xl)
ll[k] = ppca.score(Xt).mean()
for k in range(p):
ppca = assert_warns(DeprecationWarning, ProbabilisticPCA,
n_components=k)
ppca.fit(Xl)
ll[k] = ppca.score(Xt).mean()

assert_true(ll.argmax() == 1)

Expand All @@ -423,9 +418,9 @@ def test_probabilistic_pca_vs_pca():
rng = np.random.RandomState(0)
X = rng.randn(n, p) * .1 + np.array([3, 4, 5])
pca = PCA(n_components=2).fit(X)
with warnings.catch_warnings(record=True) as w:
ppca = ProbabilisticPCA(n_components=2).fit(X)
assert_array_almost_equal(pca.score_samples(X), ppca.score(X))
ppca = assert_warns(DeprecationWarning, ProbabilisticPCA,
n_components=2).fit(X)
assert_array_almost_equal(pca.score_samples(X), ppca.score(X))


if __name__ == '__main__':
Expand Down

0 comments on commit a0f0a3d

Please sign in to comment.