Skip to content

Make PCA projection reproducible given seed - #46

Open
NetZissou wants to merge 2 commits into
mainfrom
fix/pca-seed-reproducibility
Open

Make PCA projection reproducible given seed#46
NetZissou wants to merge 2 commits into
mainfrom
fix/pca-seed-reproducibility

Conversation

@NetZissou

Copy link
Copy Markdown
Collaborator

sklearn auto-selects the randomized SVD solver for large inputs, calling PCA(n_components=2) without random_state produced a different projection on every run. Now fixed by passing the seed to the API call.

cuML PCA is left unchanged: it has no random_state parameter (passing one raises TypeError and silently falls back to sklearn), and its full-SVD solver is already deterministic. Verified on a V100: cuML PCA stays on GPU path and is reproducible run-to-run. Changing the solver to "jacobi" doesn't make sense for the interactive scale data with 768 dim.

{546A243E-7139-4D64-B8BB-C2688E81E020}

sklearn auto-selects the randomized SVD solver for large inputs, calling
PCA(n_components=2) without `random_state` produced a different
projection on every run. Now fixed by passing the seed to the API call.

cuML PCA is left unchanged: it has no random_state parameter (passing
one raises TypeError and silently falls back to sklearn), and its
full-SVD solver is already deterministic. Verified on a V100: cuML PCA
stays on GPU path and is reproducible run-to-run.
@NetZissou
NetZissou requested a review from egrace479 June 18, 2026 13:29
@NetZissou NetZissou self-assigned this Jun 18, 2026
@NetZissou NetZissou added the bug Something isn't working label Jun 18, 2026
@NetZissou
NetZissou marked this pull request as draft June 22, 2026 19:49
Verified empirically on both backends (sklearnex-patched and vanilla
sklearn on CPU; cuML 26.4 on an H100): with a fixed seed and a pinned
backend, PCA/UMAP/KMeans/t-SNE reproduce bit-identically across process
restarts, except cuML t-SNE, which RAPIDS documents as not completely
deterministic even with random_state (NVIDIA/cuml#2980).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@NetZissou
NetZissou requested a review from thompsonmj July 16, 2026 21:51
@NetZissou
NetZissou marked this pull request as ready for review July 16, 2026 21:52
@NetZissou
NetZissou removed the request for review from thompsonmj August 27, 2026 14:39
@NetZissou

Copy link
Copy Markdown
Collaborator Author

@egrace479 this should be ready for review.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

Makes sklearn PCA projections reproducible when a fixed seed is enabled.

Changes:

  • Passes the seed to sklearn PCA.
  • Documents projection and clustering reproducibility across backends.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
shared/utils/clustering.py Seeds sklearn PCA and clarifies cuML behavior.
README.md Adds reproducibility guidance.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread README.md

- **PCA** is a deterministic decomposition, with no stochastic optimization involved. cuML PCA uses a full eigendecomposition and always returns the same result, seed or no seed. sklearn can auto-select a randomized SVD solver, so the app passes the seed to make it reproducible.
- **UMAP** and **KMeans** reproduce exactly on both backends when a seed is set. (Seeded UMAP trades some speed for determinism.)
- **t-SNE** reproduces on `sklearn` when a seed is set. cuML's implementation is highly parallelized and documented as [not completely deterministic between runs, even with the same `random_state`](https://docs.rapids.ai/api/cuml/stable/api/generated/cuml.manifold.tsne/) (see [rapidsai/cuml#2980](https://github.com/rapidsai/cuml/issues/2980)). Select `sklearn` when t-SNE results need to be reproducible.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

@egrace479 very interesting comment it made here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems to have corrected the 26.4 statement to the actual release of 26.04 but then fallen back to semver to say well this clearly should be fine since 26.4 would come after 26.08 😵‍💫

reducer = PCA(n_components=2)
# Pass random_state so the randomized SVD solver (auto-selected for
# large inputs) is reproducible when a seed is set; None keeps it random.
reducer = PCA(n_components=2, random_state=seed)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants