AvgRecPopularity
- class rectools.metrics.popularity.AvgRecPopularity(k: int, normalize: bool = False)[source]
Bases:
MetricAtK
Average Recommendations Popularity metric.
Calculate the average popularity of the recommended items in each list, where “popularity” of an item is the number of previous interactions with this item.
\[ARP@k = \frac{1}{|U_{t}|}\sum_{u\in U_{t}^{}}\frac{\sum_{i\in L_{u}}\phi (i)}{|L_{u}|}\]\[Normalized ARP@k = \frac{1}{|U_t|}\sum_{u\in U_t^{}}\frac{(\sum_{i\in L_u}\phi(i))/|interactions|}{|L_u|}\]- where
\(\phi (i)\) is the number of previous interactions with item i;
\(|U_{t}|\) is the number of users in the test set;
\(|interactions|\) is the total number of interactions;
\(L_{u}\) is the list of top k recommended items for user u.
- Parameters
k (int) – Number of items at the top of recommendations list that will be used to calculate metric.
normalize (bool) – Flag, which says whether to normalize metric or not. Normalization is done on total items popularity. This gives a probabilistic interpretation of the metric that can be easily applied to any data.
Examples
>>> reco = pd.DataFrame( ... { ... Columns.User: [1, 1, 2, 2, 2, 3, 3], ... Columns.Item: [1, 2, 3, 1, 2, 3, 2], ... Columns.Rank: [1, 2, 1, 2, 3, 1, 2], ... } ... ) >>> prev_interactions = pd.DataFrame( ... { ... Columns.User: [1, 1, 2, 2, 3, 3], ... Columns.Item: [1, 2, 1, 3, 1, 2], ... } ... ) >>> AvgRecPopularity(k=1).calc_per_user(reco, prev_interactions).values array([3., 1., 1.]) >>> AvgRecPopularity(k=3).calc_per_user(reco, prev_interactions).values array([2.5, 2. , 1.5]) >>> AvgRecPopularity(k=3, normalize=True).calc_per_user(reco, prev_interactions).values array([0.41666667, 0.33333333, 0.25 ])
- Inherited-members
- Parameters
k (int) –
normalize (bool) –
Methods
calc
(reco, prev_interactions)Calculate metric value.
calc_per_user
(reco, prev_interactions)Calculate metric values for all users.
Attributes
normalize
- calc(reco: DataFrame, prev_interactions: DataFrame) float [source]
Calculate metric value.
- Parameters
reco (pd.DataFrame) – Recommendations table with columns Columns.User, Columns.Item, Columns.Rank.
prev_interactions (pd.DataFrame) – Table with previous user-item interactions, with columns Columns.User, Columns.Item.
- Returns
Value of metric (average between users).
- Return type
float
- calc_per_user(reco: DataFrame, prev_interactions: DataFrame) Series [source]
Calculate metric values for all users.
- Parameters
reco (pd.DataFrame) – Recommendations table with columns Columns.User, Columns.Item, Columns.Rank.
prev_interactions (pd.DataFrame) – Table with previous user-item interactions, with columns Columns.User, Columns.Item.
- Returns
Values of metric (index - user id, values - metric value for every user).
- Return type
pd.Series