Skip to content

Compute sampled KPCA training scores on every input row - #776

Open
antonsoo wants to merge 1 commit into
yzhao062:developmentfrom
antonsoo:fix/kpca-full-training-scores
Open

antonsoo wants to merge 1 commit into
yzhao062:developmentfrom
antonsoo:fix/kpca-full-training-scores

Conversation

@antonsoo

Copy link
Copy Markdown

With sampling=True, fit replaces the training array with the sampled basis before computing decision_scores_, labels_ and the threshold. These arrays therefore contain only the subset, and the threshold is calibrated on the basis rather than the training population. Even sampling all rows leaves the stored scores in random draw order.

For example:

from sklearn.datasets import load_digits
from sklearn.preprocessing import StandardScaler
from pyod.models.kpca import KPCA

X = StandardScaler().fit_transform(load_digits().data[:300])
model = KPCA(n_components=10, sampling=True, subset_size=30,
             random_state=0).fit(X)
print(model.decision_scores_.shape, model.predict(X).sum())

Before: (30,) 238. After: (300,) 30, consistent with the default contamination of 0.1.

Keep the original training array, fit the kernel PCA basis on the subset, and compute the centered reconstruction error for every original row. The sampled basis and query decision_function remain unchanged. The non-sampling path keeps its existing calculations.

The new regression compares linear and RBF scores against an independent centered-kernel eigendecomposition, covering integer, fractional and full-size subsets. All six cases fail before the fix and pass after it. All 21 KPCA tests pass, with 100% statement coverage of pyod/models/kpca.py.

A separate check on sklearn's Digits, Wine and Iris datasets covers 36 configurations, including non-sampling controls, serialization and refits. All training arrays now align with the input rows; maximum training-score error against the independent reference is below 1e-12. Every fitted basis and raw query-score array is identical before/after, as are the non-sampling training scores. This validates calibration and row alignment, not detection quality.

Submission checks

  • Followed the contribution guidance and targeted development.
  • Checked existing issues and open PRs; no matching fix found.
  • Added an explanation and a failing-then-passing regression.
  • Ran the complete affected detector suite locally with coverage.
  • Upstream CI pending. Local validation used Linux/Python 3.12; the full cross-platform suite was not run locally.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-10T08:11:19.042115Z 3702cf0 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant