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

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

Merged
merged 2 commits into from
May 21, 2025
Merged
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 32 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)
Copy link
Member

@lesteve lesteve May 21, 2025

Choose a reason for hiding this comment

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

For the record I was also getting one failure locally without increasing batch_size, here it is for completeness/posterity:

___________________________________________________________________________________________________________________________________________________ test_singular_values[47] ___________________________________________________________________________________________________________________________________________________

global_random_seed = 47

    def test_singular_values(global_random_seed):
        # Check that the IncrementalPCA output has the correct singular values
    
        rng = np.random.RandomState(global_random_seed)
        n_samples = 1000
        n_features = 100
    
        X = datasets.make_low_rank_matrix(
            n_samples, n_features, tail_strength=0.0, effective_rank=10, random_state=rng
        )
    
        pca = PCA(n_components=10, svd_solver="full", random_state=rng).fit(X)
        ipca = IncrementalPCA(n_components=10, batch_size=100).fit(X)
>       assert_array_almost_equal(pca.singular_values_, ipca.singular_values_, 2)

sklearn/decomposition/tests/test_incremental_pca.py:364: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ 

args = (array([0.99955105, 0.99004802, 0.95818804, 0.91391011, 0.85194042,
       0.7787519 , 0.69739244, 0.61203967, 0.52688...0.99001237, 0.95811893, 0.91387836, 0.85180776,
       0.77866619, 0.69725655, 0.61192212, 0.52547507, 0.37600852]), 2), kwargs = {}, old_name = 'y', new_name = 'desired'

    @functools.wraps(fun)
    def wrapper(*args, **kwargs):
        for old_name, new_name in zip(old_names, new_names):
            if old_name in kwargs:
                if dep_version:
                    end_version = dep_version.split('.')
                    end_version[1] = str(int(end_version[1]) + 2)
                    end_version = '.'.join(end_version)
                    msg = (f"Use of keyword argument `{old_name}` is "
                           f"deprecated and replaced by `{new_name}`. "
                           f"Support for `{old_name}` will be removed "
                           f"in NumPy {end_version}.")
                    warnings.warn(msg, DeprecationWarning, stacklevel=2)
                if new_name in kwargs:
                    msg = (f"{fun.__name__}() got multiple values for "
                           f"argument now known as `{new_name}`")
                    raise TypeError(msg)
                kwargs[new_name] = kwargs.pop(old_name)
>       return fun(*args, **kwargs)
E       AssertionError: 
E       Arrays are not almost equal to 2 decimals
E       
E       Mismatched elements: 1 / 10 (10%)
E       Max absolute difference among violations: 0.06882675
E       Max relative difference among violations: 0.18304573
E        ACTUAL: array([1.  , 0.99, 0.96, 0.91, 0.85, 0.78, 0.7 , 0.61, 0.53, 0.44])
E        DESIRED: array([1.  , 0.99, 0.96, 0.91, 0.85, 0.78, 0.7 , 0.61, 0.53, 0.38])

../../micromamba/envs/scikit-learn-dev/lib/python3.13/site-packages/numpy/_utils/__init__.py:85: AssertionError
=================================================================================================================================================== short test summary info ====================================================================================================================================================
FAILED sklearn/decomposition/tests/test_incremental_pca.py::test_singular_values[47] - AssertionError: 
================================================================================================================================================ 1 failed, 818 passed in 6.08s =================================================================================================================================================

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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.