---

title: NDCG


keywords: fastai
sidebar: home_sidebar

summary: "Normalized Discounted Cumulative Gain."
description: "Normalized Discounted Cumulative Gain."
nb_path: "nbs/metrics/ndcg.ipynb"
---
<!--

#################################################
### THIS FILE WAS AUTOGENERATED! DO NOT EDIT! ###
#################################################
# file to edit: nbs/metrics/ndcg.ipynb
# command to build the docs after a change: nbdev_build_docs

-->

<div class="container" id="notebook-container">
        
    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="ndcg_at_k" class="doc_header"><code>ndcg_at_k</code><a href="https://github.com/RecoHut-Projects/recohut/tree/master/recohut/metrics/ndcg.py#L9" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>ndcg_at_k</code>(<strong><code>y_true_list</code></strong>, <strong><code>y_reco_list</code></strong>, <strong><code>users</code></strong>=<em><code>None</code></em>, <strong><code>k</code></strong>=<em><code>10</code></em>, <strong><code>next_item</code></strong>=<em><code>False</code></em>, <strong><code>all_item</code></strong>=<em><code>False</code></em>)</p>
</blockquote>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="ndcg_one" class="doc_header"><code>ndcg_one</code><a href="https://github.com/RecoHut-Projects/recohut/tree/master/recohut/metrics/ndcg.py#L43" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>ndcg_one</code>(<strong><code>y_true</code></strong>, <strong><code>y_reco</code></strong>, <strong><code>k</code></strong>)</p>
</blockquote>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="dcg_at_k" class="doc_header"><code>dcg_at_k</code><a href="https://github.com/RecoHut-Projects/recohut/tree/master/recohut/metrics/ndcg.py#L60" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>dcg_at_k</code>(<strong><code>r</code></strong>, <strong><code>k</code></strong>, <strong><code>method</code></strong>=<em><code>0</code></em>)</p>
</blockquote>
<p>Args:
    r: Relevance scores (list or numpy) in rank order
        (first element is the first item)
    k: Number of results to consider
    method: If 0 then weights are [1.0, 1.0, 0.6309, 0.5, 0.4307, ...]
            If 1 then weights are [1.0, 0.6309, 0.5, 0.4307, ...]
Returns:
    Discounted cumulative gain</p>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">result</span> <span class="o">=</span> <span class="n">dcg_at_k</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="mi">6</span><span class="p">)</span>
<span class="n">result</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">



<div class="output_text output_subarea output_execute_result">
<pre>8.097171433256849</pre>
</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">assert</span> <span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">result</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span><span class="o">==</span><span class="mf">8.0972</span>
</pre></div>

    </div>
</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

<div class="output_wrapper">
<div class="output">

<div class="output_area">


<div class="output_markdown rendered_html output_subarea ">
<h4 id="ndcg_at_k_v2" class="doc_header"><code>ndcg_at_k_v2</code><a href="https://github.com/RecoHut-Projects/recohut/tree/master/recohut/metrics/ndcg.py#L82" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>ndcg_at_k_v2</code>(<strong><code>r</code></strong>, <strong><code>k</code></strong>, <strong><code>method</code></strong>=<em><code>0</code></em>)</p>
</blockquote>

</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">result</span> <span class="o">=</span> <span class="n">ndcg_at_k_v2</span><span class="p">([</span><span class="mi">3</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">],</span> <span class="mi">6</span><span class="p">,</span> <span class="n">method</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
<span class="n">result</span>
</pre></div>

    </div>
</div>
</div>

<div class="output_wrapper">
<div class="output">

<div class="output_area">



<div class="output_text output_subarea output_execute_result">
<pre>0.9608081943360617</pre>
</div>

</div>

</div>
</div>

</div>
    {% endraw %}

    {% raw %}
    
<div class="cell border-box-sizing code_cell rendered">
<div class="input">

<div class="inner_cell">
    <div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">assert</span> <span class="n">np</span><span class="o">.</span><span class="n">round</span><span class="p">(</span><span class="n">result</span><span class="p">,</span><span class="mi">4</span><span class="p">)</span><span class="o">==</span><span class="mf">0.9608</span>
</pre></div>

    </div>
</div>
</div>

</div>
    {% endraw %}

</div>
 

