Repository navigation
Fix bugs in Scholar.Metrics.Ranking - #375
Merged
Merged
Conversation
dcg sorted by y_score and then sorted the result by y_true, so each group of tied scores came out in the order most favourable to the model, and a constant score got NDCG 1.0. Each group now gets the mean gain of its items, as in scikit-learn. The discounts past :k are now zeroed instead of splitting the tensor, which also fixes k equal to the number of elements and a tied group cut by k. :k must be a positive integer or nil, dcg_n validates its options, and f64 inputs keep f64 precision.
The ideal DCG is now computed from y_true sorted, without averaging ties, and an ideal DCG of zero gives 0 instead of NaN, as in scikit-learn.
josevalim
approved these changes
Nov 18, 2026
Contributor
|
Thank you!!! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #360.
dcgsorted byy_scoreand then sorted the result byy_true, so each group of tied scores came out in its best order, and a constant score got NDCG 1.0. Each group now gets the mean gain of its items, which is the expected DCG when ties are broken at random. scikit-learn does the same, following McSherry and Najork (ECIR 2008).kequal to the number of elements raises.Nx.splitneeds a split smaller than the axis. The discounts pastkare now zeroed instead, which also averages a tied group cut byk, as scikit-learn does.:kaccepts any positive number.Nx.splitreads a float as a fraction, sok: 0.5kept half of the list andk: 2.0raised. It now takes a positive integer ornil, like scikit-learn'sk.dcg_nandndcg_ndon't validate their options. Onlydcg/3did, sondcg_n(y_true, y_score, k: -1)returned the same ask: 4and unknown options were ignored.dcg_valuecast to:f32, andNx.log2in Nx 1.0 computeslog(2)in f32 (fixed on Nx main in Keep the input type in Nx.log2, Nx.log10 and Nx.log with a number base nx#1871, not released yet). The result type now follows the inputs, and the discount usesln 2directly.ranking_test.exshad nodoctest, and the expected values were not whatinspectprints.dcg/3had no documentation either, since the@docsat ondcg_n/3. It now holds the docs, with a tie example.Checked against scikit-learn 1.6.1 on 600 random lists of 1 to 39 items, 440 of them with ties and 359 with
k, in f32 and f64, on the default backend and under EXLA. DCG and NDCG agree to about 1e-7 in f32 and below 1e-15 in f64. Before, 411 of them disagreed and 25 raised.The tests cover the examples from the issue, a tied group cut by
k,kequal to the number of elements, f64 precision, more than 255 groups of tied scores, zero relevances, invalid options andjit_apply.