Skip to content

FIX max int64-safe scale for skewed tiny sample weights - #25

Merged
ogrisel merged 2 commits into
fix-34813-cmat-weighted-int-scalefrom
cursor/max-int64-cmat-scale-4d84
Aug 31, 2026
Merged

ogrisel merged 2 commits into
fix-34813-cmat-weighted-int-scalefrom
cursor/max-int64-cmat-scale-4d84

Conversation

@ogrisel

@ogrisel ogrisel commented Aug 30, 2026 •

Copy link
Copy Markdown
Owner

Follow-up to the mean-based adaptive scale (merged from #24 into this branch / upstream scikit-learn#34827), addressing Fazel94's latest comment.

Why the previous adaptive scale was still wrong

Mean-based scale ≈ 1e6 * n / weight.sum() works when all weights are small (pre-normalized / uniformly tiny), but fails when weights are skewed:

  • top 10% by score have w = 1e-8, rest have w = 1.0
  • mean stays ≈0.9 → scale ≈ 1.11e6
  • round(1e-8 * scale) = 0 → early-curve tps are exactly 0 while the float64 reference is ≈4.5e-3

Final tps[-1] can still look fine because most mass uses weight 1.0 — so end-point-only tests miss this.

Fix

Use the largest int64-safe scale from max weight (as suggested in the review):

scale = int(0.9 * (2**63 - 1) / (n_samples * weight_max))
scale = max(min(scale, 2**63 - 1), 1)  # clip when n * max(w) < 1

Also removes the int(round(...)) that triggered ruff RUF046 on the linter CI for scikit-learn#34827.

Tests

Extended test_confusion_matrix_at_thresholds_float32_only_weighted_large_n with skewed_tiny_top (same n_samples=20_000_000 as the other kinds):

  • asserts mean-based scale would zero 1e-8 weights
  • checks the early-curve region (tps_ref < 1) tightly against float64 and requires tps > 0

All five parametrizations pass locally (uniform, variable, normalized, tiny, skewed_tiny_top).

This pull request includes code written with the assistance of AI.
The code has not yet been reviewed by a human.

Open in Web Open in Cursor 

cursoragent and others added 2 commits August 30, 2026 10:28
Mean-based adaptive scale (~1e6 / mean(weight)) still rounds minority
tiny weights to zero when most weights are O(1). Choose the largest
scale such that n * max(weight) * scale fits in int64 (clipped to
int64 max when n * max(weight) < 1), and extend the float32-only test
with a skewed_tiny_top case that fails under the mean-based scheme.
Also fixes ruff RUF046 from the previous adaptive-scale change.

Co-authored-by: Olivier Grisel <[email protected]>
Drop the skewed_tiny_top special-case size so the parametrized test
always covers n_pos > 2**24 with a single sample count.

Co-authored-by: Olivier Grisel <[email protected]>
@ogrisel
ogrisel marked this pull request as ready for review August 31, 2026 07:56
@ogrisel
ogrisel merged commit f57222f into fix-34813-cmat-weighted-int-scale Aug 31, 2026
18 of 19 checks passed
@ogrisel
ogrisel deleted the cursor/max-int64-cmat-scale-4d84 branch August 31, 2026 07:56
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.

2 participants