Franco Piccolo
Franco Piccolo

Reputation: 7420

How to calculate NDCG with binary relevances using sklearn?

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

from sklearn.metrics import ndcg_score
y_true = [0, 1, 0]
y_pred = [0, 1, 0]
ndcg_score(y_true, y_pred)

And getting:

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

Is there a way to make this work?

Upvotes: 9

Views: 7741

Answers (1)

Sergey Bushmanov
Sergey Bushmanov

Reputation: 25219

Please try:

from sklearn.metrics import ndcg_score
y_true = [[0, 1, 0]]
y_pred = [[0, 1, 0]]
ndcg_score(y_true, y_pred)
1.0

Note the expected shapes in the docs:

y_true: ndarray, shape (n_samples, n_labels)
y_score: ndarray, shape (n_samples, n_labels)

Upvotes: 12

Related Questions