Skip to content

TST use global_random_seed in sklearn/decomposition/tests/test_incremental_pca.py #31250

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions sklearn/decomposition/tests/test_incremental_pca.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def test_incremental_pca_sparse(sparse_container):
ipca.partial_fit(X_sparse)


def test_incremental_pca_check_projection():
def test_incremental_pca_check_projection(global_random_seed):
# Test that the projection of data is correct.
rng = np.random.RandomState(1999)
rng = np.random.RandomState(global_random_seed)
n, p = 100, 3
X = rng.randn(n, p) * 0.1
X[:10] += np.array([3, 4, 5])
Expand All @@ -108,9 +108,9 @@ def test_incremental_pca_check_projection():
assert_almost_equal(np.abs(Yt[0][0]), 1.0, 1)


def test_incremental_pca_inverse():
def test_incremental_pca_inverse(global_random_seed):
# Test that the projection of data can be inverted.
rng = np.random.RandomState(1999)
rng = np.random.RandomState(global_random_seed)
n, p = 50, 3
X = rng.randn(n, p) # spherical data
X[:, 1] *= 0.00001 # make middle component relatively small
Expand Down Expand Up @@ -217,9 +217,9 @@ def test_incremental_pca_num_features_change():
ipca.partial_fit(X2)


def test_incremental_pca_batch_signs():
def test_incremental_pca_batch_signs(global_random_seed):
# Test that components_ sign is stable over batch sizes.
rng = np.random.RandomState(1999)
rng = np.random.RandomState(global_random_seed)
n_samples = 100
n_features = 3
X = rng.randn(n_samples, n_features)
Expand Down Expand Up @@ -254,9 +254,9 @@ def test_incremental_pca_partial_fit_small_batch():
assert_allclose(pca.components_, pipca.components_, atol=1e-3)


def test_incremental_pca_batch_values():
def test_incremental_pca_batch_values(global_random_seed):
# Test that components_ values are stable over batch sizes.
rng = np.random.RandomState(1999)
rng = np.random.RandomState(global_random_seed)
n_samples = 100
n_features = 3
X = rng.randn(n_samples, n_features)
Expand Down Expand Up @@ -286,9 +286,9 @@ def test_incremental_pca_batch_rank():
assert_allclose_dense_sparse(components_i, components_j)


def test_incremental_pca_partial_fit():
def test_incremental_pca_partial_fit(global_random_seed):
# Test that fit and partial_fit get equivalent results.
rng = np.random.RandomState(1999)
rng = np.random.RandomState(global_random_seed)
n, p = 50, 3
X = rng.randn(n, p) # spherical data
X[:, 1] *= 0.00001 # make middle component relatively small
Expand Down Expand Up @@ -316,9 +316,9 @@ def test_incremental_pca_against_pca_iris():
assert_almost_equal(np.abs(Y_pca), np.abs(Y_ipca), 1)


def test_incremental_pca_against_pca_random_data():
def test_incremental_pca_against_pca_random_data(global_random_seed):
# Test that IncrementalPCA and PCA are approximate (to a sign flip).
rng = np.random.RandomState(1999)
rng = np.random.RandomState(global_random_seed)
n_samples = 100
n_features = 3
X = rng.randn(n_samples, n_features) + 5 * rng.rand(1, n_features)
Expand Down Expand Up @@ -348,10 +348,10 @@ def test_explained_variances():
assert_almost_equal(pca.noise_variance_, ipca.noise_variance_, decimal=prec)


def test_singular_values():
def test_singular_values(global_random_seed):
# Check that the IncrementalPCA output has the correct singular values

rng = np.random.RandomState(0)
rng = np.random.RandomState(global_random_seed)
n_samples = 1000
n_features = 100

Expand All @@ -360,7 +360,7 @@ def test_singular_values():
)

pca = PCA(n_components=10, svd_solver="full", random_state=rng).fit(X)
ipca = IncrementalPCA(n_components=10, batch_size=100).fit(X)
ipca = IncrementalPCA(n_components=10, batch_size=150).fit(X)
assert_array_almost_equal(pca.singular_values_, ipca.singular_values_, 2)

# Compare to the Frobenius norm
Expand All @@ -382,7 +382,7 @@ def test_singular_values():
)

# Set the singular values and see what we get back
rng = np.random.RandomState(0)
rng = np.random.RandomState(global_random_seed)
n_samples = 100
n_features = 110

Expand Down