Skip to content

Test Ratings methodology

TestRatingsMethodology(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 TestRatingsMethodology, given a user \(u\), items to recommend for \(u\) are simply those items that appear in its test set

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
143
144
def __init__(self, only_greater_eq: float = None):
    super(TestRatingsMethodology, self).__init__(only_greater_eq)

filter_single(user_idx, train_set, test_set)

Method that returns items that need to be part of the recommendation list of a single user. Since it's the TestRatings Methodology, only items that appear in the test set of the user will be returned.

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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
def filter_single(self, user_idx: int, train_set: Ratings, test_set: Ratings) -> np.ndarray:
    """
    Method that returns items that need to be part of the recommendation list of a single user.
    Since it's the TestRatings Methodology, only items that appear in the *test set* of the user will be returned.

    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
    """
    uir_user = test_set.get_user_interactions(user_idx)

    if self._threshold is not None:
        result = pd.unique(uir_user[:, 1][np.where(uir_user[:, 2] >= self._threshold)])
    else:
        # TestRatings just returns the test set of the user
        result = pd.unique(uir_user[:, 1])

    return result.astype(int)