src.fairreckitlib.model.algorithms.base_algorithm
This module contains the base class for all algorithms.
Classes:
BaseAlgorithm: the base class for algorithms.
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 base class for all algorithms. 2 3Classes: 4 5 BaseAlgorithm: the base class for algorithms. 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 abc import ABCMeta, abstractmethod 13from typing import Any, Dict, Optional 14 15from .matrix import Matrix 16 17 18class BaseAlgorithm(metaclass=ABCMeta): 19 """Base class for FairRecKit algorithms. 20 21 An algorithm is used for carrying out recommender system experiments. 22 Derived algorithms are expected to implement the abstract interface. 23 24 Abstract methods: 25 26 on_train 27 28 Public methods: 29 30 get_name 31 get_num_threads 32 get_params 33 get_train_set 34 train 35 """ 36 37 def __init__(self): 38 """Construct the algorithm.""" 39 self.train_set = None 40 41 @abstractmethod 42 def get_name(self) -> str: 43 """Get the name of the algorithm. 44 45 Returns: 46 the algorithm name. 47 """ 48 raise NotImplementedError() 49 50 @abstractmethod 51 def get_num_threads(self) -> int: 52 """Get the max number of threads the algorithm can use. 53 54 Returns: 55 the number of threads. 56 """ 57 raise NotImplementedError() 58 59 @abstractmethod 60 def get_params(self) -> Dict[str, Any]: 61 """Get the parameters of the algorithm. 62 63 Returns: 64 the algorithm parameters. 65 """ 66 raise NotImplementedError() 67 68 def get_train_set(self) -> Optional[Matrix]: 69 """Get the train set that the algorithm was trained on. 70 71 Returns: 72 the train set matrix or None if the algorithm is not trained yet. 73 """ 74 return self.train_set 75 76 def train(self, train_set: Matrix) -> None: 77 """Train the algorithm on the specified train set. 78 79 Args: 80 train_set: the matrix train set. 81 82 Raises: 83 ArithmeticError: possibly raised by an algorithm on training. 84 MemoryError: possibly raised by an algorithm on training. 85 RuntimeError: possibly raised by an algorithm on training. 86 TypeError: when the specified train set does not have the correct matrix format. 87 """ 88 self.train_set = train_set 89 self.on_train(self.train_set.get_matrix()) 90 91 @abstractmethod 92 def on_train(self, train_set: Any) -> None: 93 """Train the algorithm on the train set. 94 95 Derived classes should implement the training logic 96 of the algorithm. The train set can be anything depending 97 on the matrix that is used. 98 99 Args: 100 train_set: the set to train the algorithm with. 101 """ 102 raise NotImplementedError()
19class BaseAlgorithm(metaclass=ABCMeta): 20 """Base class for FairRecKit algorithms. 21 22 An algorithm is used for carrying out recommender system experiments. 23 Derived algorithms are expected to implement the abstract interface. 24 25 Abstract methods: 26 27 on_train 28 29 Public methods: 30 31 get_name 32 get_num_threads 33 get_params 34 get_train_set 35 train 36 """ 37 38 def __init__(self): 39 """Construct the algorithm.""" 40 self.train_set = None 41 42 @abstractmethod 43 def get_name(self) -> str: 44 """Get the name of the algorithm. 45 46 Returns: 47 the algorithm name. 48 """ 49 raise NotImplementedError() 50 51 @abstractmethod 52 def get_num_threads(self) -> int: 53 """Get the max number of threads the algorithm can use. 54 55 Returns: 56 the number of threads. 57 """ 58 raise NotImplementedError() 59 60 @abstractmethod 61 def get_params(self) -> Dict[str, Any]: 62 """Get the parameters of the algorithm. 63 64 Returns: 65 the algorithm parameters. 66 """ 67 raise NotImplementedError() 68 69 def get_train_set(self) -> Optional[Matrix]: 70 """Get the train set that the algorithm was trained on. 71 72 Returns: 73 the train set matrix or None if the algorithm is not trained yet. 74 """ 75 return self.train_set 76 77 def train(self, train_set: Matrix) -> None: 78 """Train the algorithm on the specified train set. 79 80 Args: 81 train_set: the matrix train set. 82 83 Raises: 84 ArithmeticError: possibly raised by an algorithm on training. 85 MemoryError: possibly raised by an algorithm on training. 86 RuntimeError: possibly raised by an algorithm on training. 87 TypeError: when the specified train set does not have the correct matrix format. 88 """ 89 self.train_set = train_set 90 self.on_train(self.train_set.get_matrix()) 91 92 @abstractmethod 93 def on_train(self, train_set: Any) -> None: 94 """Train the algorithm on the train set. 95 96 Derived classes should implement the training logic 97 of the algorithm. The train set can be anything depending 98 on the matrix that is used. 99 100 Args: 101 train_set: the set to train the algorithm with. 102 """ 103 raise NotImplementedError()
Base class for FairRecKit algorithms.
An algorithm is used for carrying out recommender system experiments. Derived algorithms are expected to implement the abstract interface.
Abstract methods:
on_train
Public methods:
get_name get_num_threads get_params get_train_set train
42 @abstractmethod 43 def get_name(self) -> str: 44 """Get the name of the algorithm. 45 46 Returns: 47 the algorithm name. 48 """ 49 raise NotImplementedError()
Get the name of the algorithm.
Returns: the algorithm name.
51 @abstractmethod 52 def get_num_threads(self) -> int: 53 """Get the max number of threads the algorithm can use. 54 55 Returns: 56 the number of threads. 57 """ 58 raise NotImplementedError()
Get the max number of threads the algorithm can use.
Returns: the number of threads.
60 @abstractmethod 61 def get_params(self) -> Dict[str, Any]: 62 """Get the parameters of the algorithm. 63 64 Returns: 65 the algorithm parameters. 66 """ 67 raise NotImplementedError()
Get the parameters of the algorithm.
Returns: the algorithm parameters.
69 def get_train_set(self) -> Optional[Matrix]: 70 """Get the train set that the algorithm was trained on. 71 72 Returns: 73 the train set matrix or None if the algorithm is not trained yet. 74 """ 75 return self.train_set
Get the train set that the algorithm was trained on.
Returns: the train set matrix or None if the algorithm is not trained yet.
77 def train(self, train_set: Matrix) -> None: 78 """Train the algorithm on the specified train set. 79 80 Args: 81 train_set: the matrix train set. 82 83 Raises: 84 ArithmeticError: possibly raised by an algorithm on training. 85 MemoryError: possibly raised by an algorithm on training. 86 RuntimeError: possibly raised by an algorithm on training. 87 TypeError: when the specified train set does not have the correct matrix format. 88 """ 89 self.train_set = train_set 90 self.on_train(self.train_set.get_matrix())
Train the algorithm on the specified train set.
Args: train_set: the matrix train set.
Raises: ArithmeticError: possibly raised by an algorithm on training. MemoryError: possibly raised by an algorithm on training. RuntimeError: possibly raised by an algorithm on training. TypeError: when the specified train set does not have the correct matrix format.
92 @abstractmethod 93 def on_train(self, train_set: Any) -> None: 94 """Train the algorithm on the train set. 95 96 Derived classes should implement the training logic 97 of the algorithm. The train set can be anything depending 98 on the matrix that is used. 99 100 Args: 101 train_set: the set to train the algorithm with. 102 """ 103 raise NotImplementedError()
Train the algorithm on the train set.
Derived classes should implement the training logic of the algorithm. The train set can be anything depending on the matrix that is used.
Args: train_set: the set to train the algorithm with.