New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
FIX Enables label_ranking_average_precision_score to support sparse y_true #23442
FIX Enables label_ranking_average_precision_score to support sparse y_true #23442
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the PR!
sklearn/utils/validation.py
Outdated
if accept_sparse is False: | ||
accept_sparse = ["csr", "csc"] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can not do this because it changes the behavior of accept_sparse
in check_array
and is backward incompatible, which is causing many CI failures.
To fix this issue, we need to adjust label_ranking_average_precision_score
itself to accept y_true
as sparse, by setting accept_sparse="csr"
here:
scikit-learn/sklearn/metrics/_ranking.py
Line 1073 in 735d3c6
y_true = check_array(y_true, ensure_2d=False) |
From looking at the code, it maybe a little difficult to do since type_of_target
does not really work with sparse targets. The quick fix is to adjust label_ranking_average_precision_score
as follows:
y_true = check_array(y_true, ensure_2d=False, accept_sparse="csr")
...
if not issparse(y_true):
# Handle badly formatted array and the degenerate case with one label
y_type = type_of_target(y_true, input_name="y_true")
if y_type != "multilabel-indicator" and not (
y_type == "binary" and y_true.ndim == 2
):
raise ValueError("{0} format is not supported".format(y_type))
y_true = csr_matrix(y_true)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the feedback. I have included this change by adding the if not issparse(y_true):
line in a subsequent commit now. Let me know if I'm missing anything.
…g 'if not issparse(y_true)' line
y_true = csr_matrix(np.array([[1, 0, 0], [0, 0, 1]])) | ||
y_score = np.array([[0.5, 0.9, 0.6], [0, 0, 1]]) | ||
result = label_ranking_average_precision_score(y_true, y_score) | ||
assert result == pytest.approx(0.6666666666666666) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: This should work and looks a little bit better:
assert result == pytest.approx(0.6666666666666666) | |
assert result == pytest.approx(2/3) |
Edit: Changed 1/3 to 2/3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The 1/3
value should be 2/3
right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yea it's 2/3, I mistyped.
sklearn/utils/validation.py
Outdated
@@ -811,6 +811,7 @@ def check_array( | |||
|
|||
if sp.issparse(array): | |||
_ensure_no_complex_data(array) | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This diff can be reverted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!
…; reverted unwanted diffs
…; reverted unwanted diffs
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Merging with main
should fix the ci failure.
Please add an entry to the change log at doc/whats_new/v1.2.rst
with tag |Fix|. Like the other entries there, please reference this pull request with :pr:
and credit yourself (and other contributors if applicable) with :user:
.
sklearn/metrics/_ranking.py
Outdated
@@ -37,6 +37,7 @@ | |||
from ..preprocessing import label_binarize | |||
from ..utils._encode import _encode, _unique | |||
|
|||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This diff is not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, removed now
sklearn/metrics/_ranking.py
Outdated
if y_type != "multilabel-indicator" and not ( | ||
y_type == "binary" and y_true.ndim == 2 | ||
): | ||
raise ValueError("{0} format is not supported".format(y_type)) | ||
|
||
y_true = csr_matrix(y_true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We only need to convert if y_true
is dense:
y_true = csr_matrix(y_true) | |
y_true = csr_matrix(y_true) |
check_array
with accept_sparse="csr"
will convert a sparse matrix to CSR if it needs to:
scikit-learn/sklearn/utils/validation.py
Lines 641 to 642 in 996c7e2
'csr', etc. If the input is sparse but not in the allowed format, | |
it will be converted to the first listed format. True allows the input |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, fixed now
I have added an entry to |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor comment, otherwise LGTM.
I'm okay with this temporary solution, because type_of_target
does not support sparse data yet.
Co-authored-by: Thomas J. Fan <[email protected]>
Co-authored-by: Thomas J. Fan <[email protected]>
Hi @ShehanAT, you already have one approval, do you mind synchronizing with upstream and fixing conflicts? |
Sure, I have merged and fixed conflicts involving the Is it okay that all the recent upstream main commits got lumped into this PR or do you want me to create a new PR with the same changes? |
Thanks @ShehanAT. Upstream commits should not be in the history of your PR. A new PR is not needed. $ git rebase main Note that some relics of conflicts ( Let me know if you need any help in the process. |
Hi @cmarmo. Thanks for the guidance but the rebasing with main is still being identified as new commits for some reason. What you do think is the most convenient way to resolve this issue? |
Hi @ShehanAT the last commit before the synchronization is a29b92d.
Then redo the synchronization
You will have conflicts in the
At the end of the process, you might want to check if everything went fine with
which should output (at the time of me writing this)
The three files you modified if I am not mistaken.
|
499fd06
to
b383b9d
Compare
Hi @cmarmo, thanks for the instructions. My main branch is synchronized and my feature branch is rebased against the main branch now. Let me know if any further changes are needed. |
Co-authored-by: Chiara Marmo <[email protected]>
…trix-fix-ShehanAT
All green and up-to-date! Thanks @ShehanAT. Hoping that a second review will arrive soon... |
Co-authored-by: Christian Lorentzen <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
I guess we can rely on type_of_target
to be tested.
…_true (scikit-learn#23442) Allow y_true to be in CSR format.
Reference Issues/PRs
Fixes #22575
What does this implement/fix? Explain your changes.
Since the
label_ranking_average_precision_score
metric now uses thecheck_array
function(Link to relevant commit) in favour of the previously usedcheck_arrays
function(Link to relevant commit), passing aNone
value for thesparse_format
parameter now raises aTypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array
error.This PR changes the value of the
sparse_format
parameter so that whenever it is passed aNone
value, it is automatically assigned a value of["csr", "csc"]
. Doing so prevents the aforementioned error from being raised.I have added a corresponding test for this fix in the following file:
sklearn/metrics/tests/test_ranking.py, # def test_label_ranking_avg_precision_score_should_allow_csr_matrix_for_y_true_input()
I have ran the
pytest sklearn/metrics/tests/test_ranking.py
command for which all the tests, including the newly added one, have passed.