recpack.metrics.DiscountedGainK
- class recpack.metrics.DiscountedGainK(K)
Computes the discounted gain of every item in the Top-K recommendations of a user.
Relevant items that are ranked higher in the Top-K recommendations have a higher gain.
Detailed
results
show the gain of each item in the list of Top-K recommended items for every user.For each item \(i \in \text{TopK}(u)\) the discounted gain is computed as
\[\text{DiscountedGain(u,i)} = \frac{y^{true}_{u,i}}{\log_2(\text{rank}(u,i) + 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 summing up scores for every user then 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: pandas.core.frame.DataFrame
Get the detailed results for this metric.
Contains an entry for every user-item pair in the Top-K recommendations list of every user.
If there is a user with no recommendations, the results DataFrame will contain K rows for that user with item_id = NaN and score = 0.
- Returns
The results DataFrame with columns: user_id, item_id, score
- Return type
pd.DataFrame
- property value
Global metric value obtained by summing up scores for every user then taking the average over all users.