matchzoo.metrics.discounted_cumulative_gain

Discounted cumulative gain metric for ranking.

Module Contents

class matchzoo.metrics.discounted_cumulative_gain.DiscountedCumulativeGain(k:int=1, threshold:float=0.0)

Bases: matchzoo.engine.base_metric.RankingMetric

Disconunted cumulative gain metric.

ALIAS = ['discounted_cumulative_gain', 'dcg']
__repr__(self)
Returns:Formated string representation of the metric.
__call__(self, y_true:np.array, y_pred:np.array)

Calculate discounted cumulative gain (dcg).

Relevance is positive real values or binary values.

Example

>>> y_true = [0, 1, 2, 0]
>>> y_pred = [0.4, 0.2, 0.5, 0.7]
>>> DiscountedCumulativeGain(1)(y_true, y_pred)
0.0
>>> round(DiscountedCumulativeGain(k=-1)(y_true, y_pred), 2)
0.0
>>> round(DiscountedCumulativeGain(k=2)(y_true, y_pred), 2)
2.73
>>> round(DiscountedCumulativeGain(k=3)(y_true, y_pred), 2)
2.73
>>> type(DiscountedCumulativeGain(k=1)(y_true, y_pred))
<class 'float'>
Parameters:
  • y_true – The ground true label of each document.
  • y_pred – The predicted scores of each document.
Returns:

Discounted cumulative gain.