sklearn.metrics.ndcg_score

sklearn.metrics.ndcg_score(y_true, y_score, k=5)[source]

Normalized discounted cumulative gain (NDCG) at rank K.

Normalized Discounted Cumulative Gain (NDCG) measures the performance of a recommendation system based on the graded relevance of the recommended entities. It varies from 0.0 to 1.0, with 1.0 representing the ideal ranking of the entities.

Parameters:

y_true : array, shape = [n_samples]

Ground truth (true labels represended as integers).

y_score : array, shape = [n_samples, n_classes]

Predicted probabilities.

k : int

Rank.

Returns:

score : float

References

[R537537]Kaggle entry for the Normalized Discounted Cumulative Gain

Examples

>>> y_true = [1, 0, 2]
>>> y_score = [[0.15, 0.55, 0.2], [0.7, 0.2, 0.1], [0.06, 0.04, 0.9]]
>>> ndcg_score(y_true, y_score, k=2)
1.0
>>> y_score = [[0.9, 0.5, 0.8], [0.7, 0.2, 0.1], [0.06, 0.04, 0.9]]
>>> ndcg_score(y_true, y_score, k=2)
0.66666666666666663