Search code examples
pythonmachine-learningscikit-learnranking

Getting error while calculating NDCG using sklearn


I'm trying to calculate the NDCG score for binary relevances:

from sklearn import metrics

y_true = [[3]]
y_score = [[5]]
metrics.ndcg_score(y_true, y_score)

And getting error

ValueError: Only ('multilabel-indicator', 'continuous-multioutput', 'multiclass-multioutput') formats are supported. Got binary instead

Solution

  • Trying to obtain such metrics which include ranking (see the docs) for single true-predicted pairs does not make any sense (although admittedly the error message is not very informative here); you need at least two pairs:

    y_true = [[3,3]]
    y_score = [[5, 5]]
    metrics.ndcg_score(y_true, y_score)
    # 0.9999999999999998