Repository navigation
Tell agents to disclose that they were used - #32584
Conversation
|
Also just wanted to reference #32566, for completeness |
|
Why not try this, even though I am a bit unsure how much of an effect it will have ... Just curious, do we have any feed-back from other project maintainers about |
|
I think they do, but let me try and use Cursor to automagically propose a fix for an open issue and see what happens. I have to admit I don't know how people use agents in such a "fully automatic way", but let's see. |
|
Ok, first result: Cursor doesn't add a disclosure :-/ I gave it the following instruction:
It then went away to run the reproducer, then did some more stuff, proposed a fix, added a regression test and came up with the following summary for me. It is missing the disclosure, the fix isn't a million miles away from #32587 Summary
SummaryI've successfully investigated, reproduced, and fixed the bug reported in GitHub issue #32585. The BugWhen using column_subsample = self.subsample * len(column_nnz_data) // n_samplesThis could result in
The FixFile: The key change is to separate the logic for
This makes sense because when ignoring implicit zeros, we only care about the distribution of non-zero values, not the overall sparsity. Testing
Changes Made
The fix is minimal, targeted, and maintains backward compatibility while solving the critical bug for the Patch
diff --git a/sklearn/preprocessing/_data.py b/sklearn/preprocessing/_data.py
index 80d7306719..3f27c89812 100644
--- a/sklearn/preprocessing/_data.py
+++ b/sklearn/preprocessing/_data.py
@@ -2829,14 +2829,23 @@ class QuantileTransformer(OneToOneFeatureMixin, TransformerMixin, BaseEstimator)
for feature_idx in range(n_features):
column_nnz_data = X.data[X.indptr[feature_idx] : X.indptr[feature_idx + 1]]
if self.subsample is not None and len(column_nnz_data) > self.subsample:
- column_subsample = self.subsample * len(column_nnz_data) // n_samples
if self.ignore_implicit_zeros:
- column_data = np.zeros(shape=column_subsample, dtype=X.dtype)
+ # When ignoring implicit zeros, sample self.subsample values
+ # from the non-zero data only
+ column_subsample = self.subsample
+ column_data = random_state.choice(
+ column_nnz_data, size=column_subsample, replace=False
+ )
else:
+ # When not ignoring implicit zeros, maintain the sparsity ratio
+ # but ensure we get at least 1 sample from non-zero data
+ column_subsample = max(
+ 1, self.subsample * len(column_nnz_data) // n_samples
+ )
column_data = np.zeros(shape=self.subsample, dtype=X.dtype)
- column_data[:column_subsample] = random_state.choice(
- column_nnz_data, size=column_subsample, replace=False
- )
+ column_data[:column_subsample] = random_state.choice(
+ column_nnz_data, size=column_subsample, replace=False
+ )
else:
if self.ignore_implicit_zeros:
column_data = np.zeros(shape=len(column_nnz_data), dtype=X.dtype)
diff --git a/sklearn/preprocessing/tests/test_data.py b/sklearn/preprocessing/tests/test_data.py
index 8d9c6a5f45..4b674ded6d 100644
--- a/sklearn/preprocessing/tests/test_data.py
+++ b/sklearn/preprocessing/tests/test_data.py
@@ -1410,6 +1410,53 @@ def test_quantile_transform_sparse_ignore_zeros(csc_container):
)
+@pytest.mark.parametrize("csc_container", CSC_CONTAINERS)
+def test_quantile_transform_sparse_subsampling_large_n_samples(csc_container):
+ """Test that subsampling works correctly for very sparse matrices with large n_samples.
+
+ This is a regression test for issue #32585.
+ When ignore_implicit_zeros=True and n_samples is very large, the subsampling
+ logic should still sample self.subsample values from non-zero data, not scale
+ by n_samples which can result in 0 samples.
+ """
+ subsample = 100
+ n_samples = 10**9 # Very large n_samples
+
+ # Create two columns with slightly different number of non-zero values
+ # Both are above subsample, so subsampling should kick in
+ col = np.repeat([0, 1], [subsample - 1, subsample + 1])
+ row = np.arange(subsample * 2)
+ # Use absolute values as sparse matrices need non-negative values
+ data = np.abs(np.random.randn(subsample * 2))
+
+ X_sparse = csc_container((data, (row, col)), shape=(n_samples, 2))
+
+ transformer = QuantileTransformer(
+ ignore_implicit_zeros=True,
+ subsample=subsample,
+ n_quantiles=50, # Use fewer quantiles than subsample
+ random_state=0
+ )
+
+ Y = transformer.fit_transform(X_sparse)
+
+ # Check that quantiles are not all zero (which was the bug)
+ assert not np.all(transformer.quantiles_[:, 0] == 0), \
+ "Quantiles for column 0 should not all be zero"
+ assert not np.all(transformer.quantiles_[:, 1] == 0), \
+ "Quantiles for column 1 should not all be zero"
+
+ # Check that transformed values are diverse (not just 0 and 1)
+ # This would fail with the bug
+ unique_vals_col0 = len(np.unique(Y[:, 0].data))
+ unique_vals_col1 = len(np.unique(Y[:, 1].data))
+
+ assert unique_vals_col0 > 2, \
+ f"Column 0 should have more than 2 unique values, got {unique_vals_col0}"
+ assert unique_vals_col1 > 2, \
+ f"Column 1 should have more than 2 unique values, got {unique_vals_col1}"
+
+
def test_quantile_transform_dense_toy():
X = np.array(
[[0, 2, 2.6], [25, 4, 4.1], [50, 6, 2.3], [75, 8, 9.5], [100, 10, 0.1]]If I ask it to generate a PR description for me then it includes the disclosure Generated PR Description
BUG: Fix QuantileTransformer sub-sampling for sparse matrices with large n_samplesFixes #32585 SummaryFixed incorrect sub-sampling behavior in The ProblemWhen processing sparse matrices with column_subsample = self.subsample * len(column_nnz_data) // n_samplesFor very large
The SolutionWhen
This is the correct semantics: when ignoring implicit zeros, we only care about the distribution of non-zero values, not the sparsity ratio. Changes
Areas for Review
Testing
|
Asked Cursor why it wasn't following the instructions and what edits to make. With these it included the disclaimer without me having to prompt it.
|
After a bit of messing around I asked Cursor why it didn't always include the disclaimer. It made a suggestion ea97d7d and now it seems to always include them without me having to ask for them. |
lesteve
left a comment
There was a problem hiding this comment.
👍 for trying and tweaking if needed
|
I'll merge this, it has two +1s |
|
For fun, this PR has caused my cursor llm to write this little masterpiece: 🤣
Me asking: Tell me more about the user rules.
(I didn't set these as user rules, so must be preconfigured.) |
This is an idea I got from wagtail/wagtail#13456
The
AGENTS.mdfile is used by all(?)/many AI tools. Often it contains instructions for AI tools about how to build the software or run tests. In this PR all that is added is some basic instructions about PR messages, including an instruction to add a note that says "AI was used and the human hasn't yet reviewed this".What do people think? Should we give it a test drive?