TST Fix test gaussian mixture warm start
import sys
import copy
import warnings
+import pytest
import numpy as np
@@ -764,8 +765,10 @@ def test_gaussian_mixture_verbose():
sys.stdout = old_stdout
-def test_warm_start():
- random_state = 0
+@pytest.mark.filterwarnings('ignore:.*did not converge.*')
+@pytest.mark.parametrize("seed", (0, 1, 2))
+def test_warm_start(seed):
+ random_state = seed
rng = np.random.RandomState(random_state)
n_samples, n_features, n_components = 500, 2, 2
X = rng.rand(n_samples, n_features)
@@ -778,8 +781,6 @@ def test_warm_start():
reg_covar=0, random_state=random_state,
warm_start=True)
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ConvergenceWarning)
g.fit(X)
score1 = h.fit(X).score(X)
score2 = h.fit(X).score(X)
@@ -787,7 +788,7 @@ def test_warm_start():
assert_almost_equal(g.weights_, h.weights_)
assert_almost_equal(g.means_, h.means_)
assert_almost_equal(g.precisions_, h.precisions_)
- assert_greater(score2, score1)
+ assert score2 > score1
# Assert that by using warm_start we can converge to a good solution
g = GaussianMixture(n_components=n_components, n_init=1,
@@ -797,13 +798,18 @@ def test_warm_start():
max_iter=5, reg_covar=0, random_state=random_state,
warm_start=True, tol=1e-6)
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ConvergenceWarning)
g.fit(X)
- h.fit(X).fit(X)
+ assert not g.converged_
- assert_true(not g.converged_)
- assert_true(h.converged_)
+ h.fit(X)
+ # depending on the data there is large variability in the number of
+ # refit necessary to converge due to the complete randomness of the
+ # data
+ for _ in range(1000):
+ h.fit(X)
+ if h.converged_:
+ break
+ assert h.converged_
@ignore_warnings(category=ConvergenceWarning)
