Skip to content

Training Items methodology

TrainingItemsMethodology(only_greater_eq=None)

Bases: Methodology

Class which, given a train set and a test set, has the task to calculate which items must be used in order to generate a recommendation list

With TrainingItemsMethodology, given a user \(u\), items to recommend for \(u\) are all items that appear in the 'train set' of every user excluding those items that appear in the 'train set' of \(u\)

If the only_greater_eq parameter is set, then only items with rating score \(>=\) only_greater_eq will be returned

PARAMETER DESCRIPTION
only_greater_eq

float which acts as a filter, if specified only items with rating score \(>=\) only_greater_eq will be returned

TYPE: float DEFAULT: None

Source code in clayrs/recsys/methodology.py
257
258
259
260
def __init__(self, only_greater_eq: float = None):
    super(TrainingItemsMethodology, self).__init__(only_greater_eq)

    self._filtered_train_set_items: Optional[Set] = None

filter_single(user_idx, train_set, test_set)

Method that returns items that needs to be part of the recommendation list of a single user. Since it's the TrainingItems Methodology, all items that appear in the train set of every user will be returned, except for those that appear in the train set of the user passed as parameter

PARAMETER DESCRIPTION
user_idx

User idx (meaning its mapped integer) of which we want to calculate items that must appear in its recommendation list

TYPE: int

train_set

Ratings object which contains the train set of every user

TYPE: Ratings

test_set

Ratings object which contains the test set of every user

TYPE: Ratings

Source code in clayrs/recsys/methodology.py
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
def filter_single(self, user_idx: int, train_set: Ratings, test_set: Ratings) -> np.ndarray:
    """
    Method that returns items that needs to be part of the recommendation list of a single user.
    Since it's the TrainingItems Methodology, all items that appear in the *train set* of every user will be
    returned, except for those that appear in the *train set* of the user passed as parameter

    Args:
        user_idx: User idx (meaning its mapped integer) of which we want to calculate items that must appear in its
            recommendation list
        train_set: `Ratings` object which contains the train set of every user
        test_set: `Ratings` object which contains the test set of every user
    """
    already_seen_items_it = pd.unique(train_set.get_user_interactions(user_idx)[:, 1].astype(int))

    self._query_vector[already_seen_items_it] = False

    result = self._items_arr[self._query_vector]

    self._query_vector[self._filtered_train_set_items] = True

    return result.astype(int)