Skip to content

user_item_knowledge_base

logger = logging.getLogger(__name__) module-attribute

UserItemKnowledgeBase dataclass

Unknown and known user/item base.

This class is used to store the status of the user and item base. The class stores the known and unknown user and item set. The class also provides methods to update the known and unknown user and item set.

Source code in src/recnexteval/evaluators/core/user_item_knowledge_base.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
@dataclass
class UserItemKnowledgeBase:
    """Unknown and known user/item base.

    This class is used to store the status of the user and item base. The class
    stores the known and unknown user and item set. The class also provides
    methods to update the known and unknown user and item set.
    """

    unknown_user: set[int] = field(default_factory=set)
    known_user: set[int] = field(default_factory=set)
    unknown_item: set[int] = field(default_factory=set)
    known_item: set[int] = field(default_factory=set)

    @property
    def known_shape(self) -> tuple[int, int]:
        """Known number of user id and item id.

        id are zero-indexed and the shape returns the max id + 1.

        Note:
            `max` is used over `len` as there may be gaps in the id sequence
            and we are only concerned with the shape of the
            user-item interaction matrix.

        Returns:
            Tuple of (|user|, |item|).
        """
        return (max(self.known_user) + 1, max(self.known_item) + 1)

    @property
    def global_shape(self) -> tuple[int, int]:
        """Global number of user id and item id.

        This is the shape of the user-item interaction matrix considering all
        the users and items that has been possibly exposed. The global shape
        considers the fact that an unknown user/item can be exposed during the
        prediction stage when an unknown user/item id is requested for prediction
        on the algorithm.

        Returns:
            Tuple of (|user|, |item|).
        """
        return (
            max(max(self.known_user), max(self.unknown_user)) + 1,
            max(max(self.known_item), max(self.unknown_item)) + 1,
        )

    def update_known_user_item_base(self, data: InteractionMatrix) -> None:
        """Updates the known user and item set with the data."""
        self.known_item.update(data.item_ids)
        self.known_user.update(data.user_ids)

    def update_unknown_user_item_base(self, data: InteractionMatrix) -> None:
        """Updates the unknown user and item set with the data. """
        self.unknown_user = data.user_ids - self.known_user
        self.unknown_item = data.item_ids - self.known_item

    def reset_unknown_user_item_base(self) -> None:
        """Clears the unknown user and item set."""
        self.unknown_user.clear()
        self.unknown_item.clear()

unknown_user = field(default_factory=set) class-attribute instance-attribute

known_user = field(default_factory=set) class-attribute instance-attribute

unknown_item = field(default_factory=set) class-attribute instance-attribute

known_item = field(default_factory=set) class-attribute instance-attribute

known_shape property

Known number of user id and item id.

id are zero-indexed and the shape returns the max id + 1.

Note

max is used over len as there may be gaps in the id sequence and we are only concerned with the shape of the user-item interaction matrix.

Returns:

Type Description
tuple[int, int]

Tuple of (|user|, |item|).

global_shape property

Global number of user id and item id.

This is the shape of the user-item interaction matrix considering all the users and items that has been possibly exposed. The global shape considers the fact that an unknown user/item can be exposed during the prediction stage when an unknown user/item id is requested for prediction on the algorithm.

Returns:

Type Description
tuple[int, int]

Tuple of (|user|, |item|).

update_known_user_item_base(data)

Updates the known user and item set with the data.

Source code in src/recnexteval/evaluators/core/user_item_knowledge_base.py
58
59
60
61
def update_known_user_item_base(self, data: InteractionMatrix) -> None:
    """Updates the known user and item set with the data."""
    self.known_item.update(data.item_ids)
    self.known_user.update(data.user_ids)

update_unknown_user_item_base(data)

Updates the unknown user and item set with the data.

Source code in src/recnexteval/evaluators/core/user_item_knowledge_base.py
63
64
65
66
def update_unknown_user_item_base(self, data: InteractionMatrix) -> None:
    """Updates the unknown user and item set with the data. """
    self.unknown_user = data.user_ids - self.known_user
    self.unknown_item = data.item_ids - self.known_item

reset_unknown_user_item_base()

Clears the unknown user and item set.

Source code in src/recnexteval/evaluators/core/user_item_knowledge_base.py
68
69
70
71
def reset_unknown_user_item_base(self) -> None:
    """Clears the unknown user and item set."""
    self.unknown_user.clear()
    self.unknown_item.clear()