Skip to content

TST use global_random_seed in sklearn/decomposition/tests/test_factor_analysis.py #29272

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jun 20, 2024
6 changes: 3 additions & 3 deletions sklearn/decomposition/tests/test_factor_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@

# Ignore warnings from switching to more power iterations in randomized_svd
@ignore_warnings
def test_factor_analysis():
def test_factor_analysis(global_random_seed):
# Test FactorAnalysis ability to recover the data covariance structure
rng = np.random.RandomState(0)
rng = np.random.RandomState(global_random_seed)
n_samples, n_features, n_components = 20, 5, 3

# Some random settings for the generative model
Expand Down Expand Up @@ -57,7 +57,7 @@ def test_factor_analysis():
# Model Covariance
mcov = fa.get_covariance()
diff = np.sum(np.abs(scov - mcov)) / W.size
assert diff < 0.1, "Mean absolute difference is %f" % diff
assert diff < 0.2, "Mean absolute difference is %f" % diff
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I added the global_random_seed fixture I got the following test failures:

FAILED sklearn/decomposition/tests/test_factor_analysis.py::test_factor_analysis[16] - AssertionError: Mean absolute difference is 0.170675
FAILED sklearn/decomposition/tests/test_factor_analysis.py::test_factor_analysis[38] - AssertionError: Mean absolute difference is 0.130926
FAILED sklearn/decomposition/tests/test_factor_analysis.py::test_factor_analysis[96] - AssertionError: Mean absolute difference is 0.134258

By increasing the threshold the failures are fixed. 0.2 looks acceptable to me but let me know if you disagree.

fa = FactorAnalysis(
n_components=n_components, noise_variance_init=np.ones(n_features)
)
Expand Down