induwara.lk
induwara.lkAI · Machine learning

NDCG Calculator (Normalized Discounted Cumulative Gain)

Paste a ranked list of relevance scores and get NDCG@k, plus the DCG, IDCG, ideal ordering and every position's gain and log discount. Linear or exponential gain, matches scikit-learn's ndcg_score. No signup, nothing uploaded.

By Induwara AshinsanaUpdated Jun 10, 2026
NDCG calculator

The graded relevance of each result, in the order your model ranked them. Separate with commas, spaces, or new lines. Non-negative numbers only.

Gain type

Score only the top k results (e.g. 10 for NDCG@10). Leave empty to use the whole list.

Examples
NDCG@6
0.9608
0 to 1 · higher is better
DCG@6
6.8611
Your ranking
IDCG@6
7.1410
Ideal ranking
Reading
Excellent ranking
NDCG@696.08% of ideal

Cross-check. The row-by-row form (each gᵢ divided by its log2 discount) gives 0.9608; the independent gain·discount vector form — how scikit-learn computes it — gives 0.9608. They reconcile, as they must.

Ideal ordering (used for IDCG)

[3, 3, 2, 2, 1, 0]

Per-position working (your ranking)

Rank irelᵢgain gᵢlog2(i+1)gᵢ / log2(i+1)
13.00003.00001.00003.0000
22.00002.00001.58501.2619
33.00003.00002.00001.5000
40.00000.00002.32190.0000
51.00001.00002.58500.3869
62.00002.00002.80740.7124
DCG@6 total6.8611
DCG@6 = 6.8611
IDCG@6 = 7.1410
NDCG@6 = 6.8611 / 7.1410 = 0.9608

Method: NDCG@k = DCG@k / IDCG@k, with DCG@k = Σ gᵢ / log2(i+1) and IDCG@k the same on relevances sorted descending — scikit-learn ndcg_score and Järvelin & Kekäläinen (2002). Nothing leaves this page.

How it works

NDCG (Normalized Discounted Cumulative Gain)measures how good a ranked list is, rewarding relevant results placed near the top. It is the headline quality metric for search ranking, recommender systems and learning-to-rank models. The definition here follows Järvelin & Kekäläinen (2002) and the implementation in scikit-learn's ndcg_score.

Positions are 1-indexed. For a predicted ranking with relevance scores rel₁ … relₙ and cutoff k, the score is built in four steps:

  1. Gain. Turn each relevance into a gain. Linear gain is gᵢ = relᵢ; exponential gain is gᵢ = 2^relᵢ − 1 (Burges et al., 2005), which rewards highly relevant hits more.
  2. Discount. Divide each gain by a position discount dᵢ = log2(i + 1), so rank 1 has discount log2(2) = 1 and lower ranks are penalised more.
  3. DCG@k. Add the discounted gains over the top k: DCG@k = Σᵢ₌₁..ₖ gᵢ / log2(i+1).
  4. Normalise. Compute IDCG@k — the DCG of the ideal ranking, the same relevances sorted from best to worst — then NDCG@k = DCG@k / IDCG@k, a value in [0, 1].

When IDCG@k is 0 — every relevance is 0 — NDCG is undefined because it would divide by zero. The tool reports 0 and flags it, the same convention scikit-learn uses. If you set k larger than the number of items, it is clamped to the list length with a notice. As a credibility check, the calculator computes NDCG a second way — the gain·discount vector form scikit-learn uses internally — and confirms the two routes agree to floating-point precision. This version scores one ranking and derives the ideal order by sorting the relevances you enter; mean NDCG across many queries is a separate calculation.

Worked examples

Full list, linear gain — rel = [3, 2, 3, 0, 1, 2], k = 6

  1. DCG = 3/1 + 2/log2(3) + 3/log2(4) + 0 + 1/log2(6) + 2/log2(7)
  2. = 3 + 1.2618595 + 1.5 + 0 + 0.3868528 + 0.7124143 = 6.8611266
  3. ideal order = [3, 3, 2, 2, 1, 0]
  4. IDCG = 3/1 + 3/log2(3) + 2/log2(4) + 2/log2(5) + 1/log2(6) + 0
  5. = 3 + 1.8927893 + 1 + 0.8613531 + 0.3868528 = 7.1409952
  6. NDCG = 6.8611266 / 7.1409952 = 0.9608

NDCG@3, exponential gain — rel = [2, 0, 1, 3, 2], k = 3

  1. gains (2^rel − 1) = [3, 0, 1, 7, 3]
  2. DCG@3 = 3/log2(2) + 0/log2(3) + 1/log2(4) = 3 + 0 + 0.5 = 3.5
  3. ideal gains (rel sorted [3,2,2,1,0]) = [7, 3, 3, 1, 0]; top-3 = [7, 3, 3]
  4. IDCG@3 = 7/1 + 3/log2(3) + 3/log2(4) = 7 + 1.8927893 + 1.5 = 10.3927893
  5. NDCG@3 = 3.5 / 10.3927893 = 0.3368

Edge case — all relevances 0, rel = [0, 0, 0]

  1. Every gain is 0, so DCG = 0 and IDCG = 0
  2. NDCG = 0 / 0 is undefined (division by zero)
  3. Reported as 0 with a note — the scikit-learn convention

Frequently asked questions

Sources & references

Related tools

Rate this tool
Be the first to rate

Comments & feedback

Spotted a bug or want an improvement? Tell us — our team reviews every comment, and good ideas get built. Comments are public and anonymous.

Found a bug, edge case, or want to suggest an improvement?

Email me at [email protected] — most fixes ship within 24 hours.