src.fairreckitlib.model.algorithms.surprise.surprise_matrix
This module contains a matrix implementation for the surprise package.
Classes:
MatrixSurprise: the surprise matrix class.
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 a matrix implementation for the surprise package. 2 3Classes: 4 5 MatrixSurprise: the surprise matrix class. 6 7This program has been developed by students from the bachelor Computer Science at 8Utrecht University within the Software Project course. 9© Copyright Utrecht University (Department of Information and Computing Sciences) 10""" 11 12from typing import Tuple 13 14import numpy as np 15import surprise 16 17from ....data.ratings.convert_constants import RATING_TYPE_THRESHOLD 18from ..matrix import Matrix 19 20 21class MatrixSurprise(Matrix): 22 """Matrix implementation with a surprise.Trainset.""" 23 24 def __init__(self, file_path: str, rating_scale: Tuple[float, float]): 25 """Construct the CSR Matrix. 26 27 The surprise matrix is expected to be stored in a tab separated file without header, 28 with the 'user', 'item', 'rating' columns in this order. 29 The matrix is loaded into a surprise dataset and converted to a full train set. 30 31 Args: 32 file_path: the file path to where the matrix is stored. 33 rating_scale: the minimum and maximum rating in the loaded set. 34 35 Raises: 36 RuntimeError: when the max of the rating scale is larger than the RATING_TYPE_THRESHOLD. 37 """ 38 Matrix.__init__(self, file_path) 39 if rating_scale[1] > RATING_TYPE_THRESHOLD: 40 raise RuntimeError('Surprise only supports explicit ratings') 41 42 reader = surprise.Reader(rating_scale=rating_scale) 43 self.matrix = surprise.Dataset.load_from_df(self.matrix, reader) 44 self.matrix = self.matrix.build_full_trainset() 45 46 def get_matrix(self) -> surprise.Trainset: 47 """Get the matrix. 48 49 Returns: 50 the surprise.Trainset matrix. 51 """ 52 return self.matrix 53 54 def _get_user_rated_items(self, user: int) -> np.ndarray: 55 """Get the rated items for the specified user. 56 57 The user ratings are stored in the 'ur' dictionary of the surprise.Trainset. 58 59 Args: 60 user: the user to get the rated items of. 61 62 Returns: 63 a list of item IDs that are rated by the user. 64 """ 65 uid = self.matrix.to_inner_uid(user) 66 return np.array([self.matrix.to_raw_iid(i) for (i, _) in self.matrix.ur[uid]])
22class MatrixSurprise(Matrix): 23 """Matrix implementation with a surprise.Trainset.""" 24 25 def __init__(self, file_path: str, rating_scale: Tuple[float, float]): 26 """Construct the CSR Matrix. 27 28 The surprise matrix is expected to be stored in a tab separated file without header, 29 with the 'user', 'item', 'rating' columns in this order. 30 The matrix is loaded into a surprise dataset and converted to a full train set. 31 32 Args: 33 file_path: the file path to where the matrix is stored. 34 rating_scale: the minimum and maximum rating in the loaded set. 35 36 Raises: 37 RuntimeError: when the max of the rating scale is larger than the RATING_TYPE_THRESHOLD. 38 """ 39 Matrix.__init__(self, file_path) 40 if rating_scale[1] > RATING_TYPE_THRESHOLD: 41 raise RuntimeError('Surprise only supports explicit ratings') 42 43 reader = surprise.Reader(rating_scale=rating_scale) 44 self.matrix = surprise.Dataset.load_from_df(self.matrix, reader) 45 self.matrix = self.matrix.build_full_trainset() 46 47 def get_matrix(self) -> surprise.Trainset: 48 """Get the matrix. 49 50 Returns: 51 the surprise.Trainset matrix. 52 """ 53 return self.matrix 54 55 def _get_user_rated_items(self, user: int) -> np.ndarray: 56 """Get the rated items for the specified user. 57 58 The user ratings are stored in the 'ur' dictionary of the surprise.Trainset. 59 60 Args: 61 user: the user to get the rated items of. 62 63 Returns: 64 a list of item IDs that are rated by the user. 65 """ 66 uid = self.matrix.to_inner_uid(user) 67 return np.array([self.matrix.to_raw_iid(i) for (i, _) in self.matrix.ur[uid]])
Matrix implementation with a surprise.Trainset.
25 def __init__(self, file_path: str, rating_scale: Tuple[float, float]): 26 """Construct the CSR Matrix. 27 28 The surprise matrix is expected to be stored in a tab separated file without header, 29 with the 'user', 'item', 'rating' columns in this order. 30 The matrix is loaded into a surprise dataset and converted to a full train set. 31 32 Args: 33 file_path: the file path to where the matrix is stored. 34 rating_scale: the minimum and maximum rating in the loaded set. 35 36 Raises: 37 RuntimeError: when the max of the rating scale is larger than the RATING_TYPE_THRESHOLD. 38 """ 39 Matrix.__init__(self, file_path) 40 if rating_scale[1] > RATING_TYPE_THRESHOLD: 41 raise RuntimeError('Surprise only supports explicit ratings') 42 43 reader = surprise.Reader(rating_scale=rating_scale) 44 self.matrix = surprise.Dataset.load_from_df(self.matrix, reader) 45 self.matrix = self.matrix.build_full_trainset()
Construct the CSR Matrix.
The surprise matrix is expected to be stored in a tab separated file without header, with the 'user', 'item', 'rating' columns in this order. The matrix is loaded into a surprise dataset and converted to a full train set.
Args: file_path: the file path to where the matrix is stored. rating_scale: the minimum and maximum rating in the loaded set.
Raises: RuntimeError: when the max of the rating scale is larger than the RATING_TYPE_THRESHOLD.