Repository navigation
Conversation
|
Thanks for the PR. Could you please instead use For the unweighted case, we should use The two changes together should help make the weighting/repetition semantic check of Please mark |
sample_weight support for QuantileTransformer when fit on dense data
|
Please also don't forget to document your change in a changelog entry by adding a file under |
|
@ogrisel Okay, thank you for your reply! I will fix that |
…rcentile, add XFAIL for sparse_data
|
Are you still interested in working on this? |
| self.references_ = np.linspace(0, 1, self.n_quantiles_, endpoint=True) | ||
| if sparse.issparse(X): | ||
| if sample_weight is not None: | ||
| raise ValueError("sample_weight is not supported for sparse input.") |
There was a problem hiding this comment.
| raise ValueError("sample_weight is not supported for sparse input.") | |
| raise NotImplementedError( | |
| "sample_weight is not supported for sparse input." | |
| ) |
| X_roundtrip = qt_weighted.inverse_transform(Xt_weighted) | ||
| np.testing.assert_allclose( | ||
| X[~np.isnan(X)], X_roundtrip[~np.isnan(X)], rtol=1e-2, atol=1e-2 | ||
| ) |
There was a problem hiding this comment.
I think this test is not necessary since there is already a common test check_sample_weight_equivalence_on_dense_data that should be executed when running the following command:
pytest -v -k "QuantileTransformer and check_sample_weight_equivalence" sklearn/tests/test_common.py
| weights_clean = sample_weight[mask] | ||
| self.quantiles_[:, i] = _averaged_weighted_percentile( | ||
| col_clean, sample_weight=weights_clean, quantile=references / 100.0 | ||
| ) |
There was a problem hiding this comment.
Please check the source code of _averaged_weighted_percentile and the docstring of _weighted_percentile to learn how to use it properly.
In particular:
- it accepts percentile ranks, not quantile ranks;
- it can compute for for a single rank at a time, hence you might need to call this in a list comprehension for all possible ranks passed in the
referencesarray.
There was a problem hiding this comment.
Note that _averaged_weighted_percentile has now been merged into _weighted_percentile via the average=True option.
|
|
||
| self.quantiles_ = np.zeros((len(references), n_features)) | ||
|
|
||
| for i in range(n_features): |
There was a problem hiding this comment.
Please rename i to feature_idx to make the code easier to follow.
|
Closing due to lack of activity from the OP. Happy to reopen if you get to work on this further. |
| for i in range(n_features): | ||
| col = X[:, i] | ||
| mask = ~np.isnan(col) | ||
| col_clean = col[mask] |
There was a problem hiding this comment.
_weighted_percentile has builtin support to ignore nan values, so this should no longer be needed.
|
This earlier PR was really helpful in shaping the current direction of |
Reference Issues/PRs
Fixes #30707
See also the discussion in #30707.
What does this implement/fix? Explain your changes.
This PR adds support for the
sample_weightparameter toQuantileTransformer, allowing users to apply weights to samples when computing quantiles. This makes the transformation more flexible, especially in cases where samples have varying importance or are part of imbalanced datasets.Changes made:
sample_weightparameter tofitand_dense_fit.sample_weight.Any other comments?
sample_weightis not provided.Thanks for the review!