Skip to content

Commit

Permalink
Added ndcg metric (elixir-nx#251)
Browse files Browse the repository at this point in the history
* Added ndcg metric

* Update lib/scholar/metrics/ranking.ex

Co-authored-by: José Valim <[email protected]>

* Update lib/scholar/metrics/ranking.ex

Co-authored-by: José Valim <[email protected]>

* Update lib/scholar/metrics/ranking.ex

Co-authored-by: Krsto Proroković <[email protected]>

* Update lib/scholar/metrics/ranking.ex

Co-authored-by: Mateusz Sluszniak <[email protected]>

* Update lib/scholar/metrics/ranking.ex

---------

Co-authored-by: José Valim <[email protected]>
Co-authored-by: Krsto Proroković <[email protected]>
Co-authored-by: Mateusz Sluszniak <[email protected]>
  • Loading branch information
4 people authored Apr 7, 2024
1 parent c314152 commit 0d1bcc1
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions lib/scholar/metrics/ranking.ex
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,42 @@ defmodule Scholar.Metrics.Ranking do
end
end
end

@doc """
Computes the normalized discounted cumulative gain (NDCG) based on true relevance scores `y_true` and their respective predicted scores `y_score`.
## Options
#{NimbleOptions.docs(@dcg_opts_schema)}
## Examples
iex> true_relevance = Nx.tensor([10, 0, 0, 1, 5])
iex> scores = Nx.tensor([0.1, 0.2, 0.3, 4, 70])
iex> Scholar.Metrics.Ranking.ndcg_n(true_relevance, scores)
#Nx.Tensor<
f32
0.6956940293312073
>
iex> scores = Nx.tensor([0.05, 1.1, 1.0, 0.5, 0.0])
iex> Scholar.Metrics.Ranking.ndcg_n(true_relevance, scores)
#Nx.Tensor<
f32
0.4936802089214325
>
iex> scores = Nx.tensor([0.05, 1.1, 1.0, 0.5, 0.0])
iex> Scholar.Metrics.Ranking.ndcg_n(true_relevance, scores, k: 4)
#Nx.Tensor<
f32
0.352024108171463
>
iex> Scholar.Metrics.Ranking.ndcg_n(true_relevance, true_relevance, k: 4)
#Nx.Tensor<
f32
1.0
>
"""
defn ndcg_n(y_true, y_score, opts \\ []) do
dcg_n(y_true, y_score, opts) / dcg_n(y_true, y_true, opts)
end
end

0 comments on commit 0d1bcc1

Please sign in to comment.