recpack.metrics.NDCGK
- class recpack.metrics.NDCGK(K)
Computes the normalized sum of gains of all items in a recommendation list.
The normalized Discounted Cumulative Gain (nDCG) is similar to DCG, but normalizes by dividing the resulting sum of cumulative gains by the best possible discounted cumulative gain for a list of recommendations of length K for a user with history length N.
Scores are always in the interval [0, 1]
\[\text{NormalizedDiscountedCumulativeGain}(u) = \frac{\text{DCG}(u)}{\text{IDCG}(u)}\]where IDCG stands for Ideal Discounted Cumulative Gain, computed as:
\[\text{IDCG}(u) = \sum\limits_{j=1}^{\text{min}(K, |y^{true}_u|)} \frac{1}{\log_2 (j + 1)}\]- Parameters
K (int) – Size of the recommendation list consisting of the Top-K item predictions.
Methods
calculate
(y_true, y_pred)Computes metric given true labels
y_true
and predicted scoresy_pred
.Attributes
The names of the columns in the results DataFrame.
Name of the metric.
Dimension of the item-space in both
y_true
andy_pred
Dimension of the user-space in both
y_true
andy_pred
after elimination of users without interactions iny_true
.Get the detailed results for this metric.
Global metric value obtained by taking the average over all users.
- calculate(y_true: scipy.sparse._csr.csr_matrix, y_pred: scipy.sparse._csr.csr_matrix) None
Computes metric given true labels
y_true
and predicted scoresy_pred
. Only Top-K recommendations are considered.Detailed metric results can be retrieved with
results
. Global aggregate metric value is retrieved asvalue
.- Parameters
y_true (csr_matrix) – True user-item interactions.
y_pred (csr_matrix) – Predicted affinity of users for items.
- property col_names
The names of the columns in the results DataFrame.
- property name
Name of the metric.
- property num_items: int
Dimension of the item-space in both
y_true
andy_pred
- property num_users: int
Dimension of the user-space in both
y_true
andy_pred
after elimination of users without interactions iny_true
.
- property results
Get the detailed results for this metric.
Contains an entry for every user.
- Returns
The results DataFrame with columns: user_id, score
- Return type
pd.DataFrame
- property value
Global metric value obtained by taking the average over all users.