src.fairreckitlib.evaluation.metrics.rexmex.rexmex_coverage_metric

This module contains the rexmex coverage metric and creation functions.

Classes:

RexmexCoverageMetric: coverage metric implementation for rexmex.

Functions:

create_item_coverage: create the Item coverage metric (factory creation compatible).
create_user_coverage: create the User coverage metric (factory creation compatible).

This program has been developed by students from the bachelor Computer Science at Utrecht University within the Software Project course. © Copyright Utrecht University (Department of Information and Computing Sciences)

 1"""This module contains the rexmex coverage metric and creation functions.
 2
 3Classes:
 4
 5    RexmexCoverageMetric: coverage metric implementation for rexmex.
 6
 7Functions:
 8
 9    create_item_coverage: create the Item coverage metric (factory creation compatible).
10    create_user_coverage: create the User coverage metric (factory creation compatible).
11
12This program has been developed by students from the bachelor Computer Science at
13Utrecht University within the Software Project course.
14© Copyright Utrecht University (Department of Information and Computing Sciences)
15"""
16
17from typing import Any, Callable, Dict
18
19import pandas as pd
20from rexmex.metrics import item_coverage, user_coverage
21
22from ...evaluation_sets import EvaluationSets
23from ..metric_base import ColumnMetric
24
25
26class RexmexCoverageMetric(ColumnMetric):
27    """Coverage metric implementation for the Rexmex framework."""
28
29    def __init__(
30            self,
31            name: str,
32            params: Dict[str, Any],
33            eval_func: Callable[[pd.Series, pd.Series], float]):
34        """Construct the Rexmex coverage metric.
35
36        Args:
37            name: the name of the metric.
38            params: the parameters of the metric.
39            eval_func: the function that uses the user and item columns to compute the evaluation.
40        """
41        ColumnMetric.__init__(
42            self,
43            name,
44            params,
45            eval_func,
46            requires_test_set=False,
47            requires_train_set=True
48        )
49
50    def on_evaluate(self, eval_sets: EvaluationSets) -> float:
51        """Evaluate the sets for the performance of the metric.
52
53        Args:
54            eval_sets: the sets to use for computing the performance of the metric.
55
56        Returns:
57            the evaluated performance.
58        """
59        # Convert recommended user, item columns to list of tuples.
60        tuple_recs = [tuple(r) for r in eval_sets.ratings[['user', 'item']].to_numpy()]
61        # The possible users and items are in the train set
62        possible_users_items = (eval_sets.train['user'], eval_sets.train['item'])
63        return self.eval_func(possible_users_items, tuple_recs)
64
65
66def create_item_coverage(name: str, params: Dict[str, Any], **_) -> RexmexCoverageMetric:
67    """Create the Item coverage metric.
68
69    Args:
70        name: the name of the metric.
71        params: there are no parameters for this metric.
72
73    Returns:
74        the RexmexCoverageMetric wrapper of Item coverage.
75    """
76    return RexmexCoverageMetric(name, params, item_coverage)
77
78
79def create_user_coverage(name: str, params: Dict[str, Any], **_) -> RexmexCoverageMetric:
80    """Create the User coverage metric.
81
82    Args:
83        name: the name of the metric.
84        params: there are no parameters for this metric.
85
86    Returns:
87        the RexmexCoverageMetric wrapper of User coverage.
88    """
89    return RexmexCoverageMetric(name, params, user_coverage)
class RexmexCoverageMetric(src.fairreckitlib.evaluation.metrics.metric_base.ColumnMetric):
27class RexmexCoverageMetric(ColumnMetric):
28    """Coverage metric implementation for the Rexmex framework."""
29
30    def __init__(
31            self,
32            name: str,
33            params: Dict[str, Any],
34            eval_func: Callable[[pd.Series, pd.Series], float]):
35        """Construct the Rexmex coverage metric.
36
37        Args:
38            name: the name of the metric.
39            params: the parameters of the metric.
40            eval_func: the function that uses the user and item columns to compute the evaluation.
41        """
42        ColumnMetric.__init__(
43            self,
44            name,
45            params,
46            eval_func,
47            requires_test_set=False,
48            requires_train_set=True
49        )
50
51    def on_evaluate(self, eval_sets: EvaluationSets) -> float:
52        """Evaluate the sets for the performance of the metric.
53
54        Args:
55            eval_sets: the sets to use for computing the performance of the metric.
56
57        Returns:
58            the evaluated performance.
59        """
60        # Convert recommended user, item columns to list of tuples.
61        tuple_recs = [tuple(r) for r in eval_sets.ratings[['user', 'item']].to_numpy()]
62        # The possible users and items are in the train set
63        possible_users_items = (eval_sets.train['user'], eval_sets.train['item'])
64        return self.eval_func(possible_users_items, tuple_recs)

Coverage metric implementation for the Rexmex framework.

RexmexCoverageMetric( name: str, params: Dict[str, Any], eval_func: Callable[[pandas.core.series.Series, pandas.core.series.Series], float])
30    def __init__(
31            self,
32            name: str,
33            params: Dict[str, Any],
34            eval_func: Callable[[pd.Series, pd.Series], float]):
35        """Construct the Rexmex coverage metric.
36
37        Args:
38            name: the name of the metric.
39            params: the parameters of the metric.
40            eval_func: the function that uses the user and item columns to compute the evaluation.
41        """
42        ColumnMetric.__init__(
43            self,
44            name,
45            params,
46            eval_func,
47            requires_test_set=False,
48            requires_train_set=True
49        )

Construct the Rexmex coverage metric.

Args: name: the name of the metric. params: the parameters of the metric. eval_func: the function that uses the user and item columns to compute the evaluation.

def on_evaluate( self, eval_sets: src.fairreckitlib.evaluation.evaluation_sets.EvaluationSets) -> float:
51    def on_evaluate(self, eval_sets: EvaluationSets) -> float:
52        """Evaluate the sets for the performance of the metric.
53
54        Args:
55            eval_sets: the sets to use for computing the performance of the metric.
56
57        Returns:
58            the evaluated performance.
59        """
60        # Convert recommended user, item columns to list of tuples.
61        tuple_recs = [tuple(r) for r in eval_sets.ratings[['user', 'item']].to_numpy()]
62        # The possible users and items are in the train set
63        possible_users_items = (eval_sets.train['user'], eval_sets.train['item'])
64        return self.eval_func(possible_users_items, tuple_recs)

Evaluate the sets for the performance of the metric.

Args: eval_sets: the sets to use for computing the performance of the metric.

Returns: the evaluated performance.

def create_item_coverage( name: str, params: Dict[str, Any], **_) -> src.fairreckitlib.evaluation.metrics.rexmex.rexmex_coverage_metric.RexmexCoverageMetric:
67def create_item_coverage(name: str, params: Dict[str, Any], **_) -> RexmexCoverageMetric:
68    """Create the Item coverage metric.
69
70    Args:
71        name: the name of the metric.
72        params: there are no parameters for this metric.
73
74    Returns:
75        the RexmexCoverageMetric wrapper of Item coverage.
76    """
77    return RexmexCoverageMetric(name, params, item_coverage)

Create the Item coverage metric.

Args: name: the name of the metric. params: there are no parameters for this metric.

Returns: the RexmexCoverageMetric wrapper of Item coverage.

def create_user_coverage( name: str, params: Dict[str, Any], **_) -> src.fairreckitlib.evaluation.metrics.rexmex.rexmex_coverage_metric.RexmexCoverageMetric:
80def create_user_coverage(name: str, params: Dict[str, Any], **_) -> RexmexCoverageMetric:
81    """Create the User coverage metric.
82
83    Args:
84        name: the name of the metric.
85        params: there are no parameters for this metric.
86
87    Returns:
88        the RexmexCoverageMetric wrapper of User coverage.
89    """
90    return RexmexCoverageMetric(name, params, user_coverage)

Create the User coverage metric.

Args: name: the name of the metric. params: there are no parameters for this metric.

Returns: the RexmexCoverageMetric wrapper of User coverage.