This class implements Collaborative Filtering (CF). More...
Public Member Functions | |
| CFType (const size_t numUsersForSimilarity=5, const size_t rank=0) | |
| Initialize the CFType object without performing any factorization. More... | |
template < typename MatType , typename DecompositionPolicy = NMFPolicy > | |
| CFType (const MatType &data, DecompositionPolicy &decomposition=DecompositionPolicy(), const size_t numUsersForSimilarity=5, const size_t rank=0, const size_t maxIterations=1000, const double minResidue=1e-5, const bool mit=false) | |
| Initialize the CFType object using any decomposition method, immediately factorizing the given data to create a model. More... | |
| const arma::sp_mat & | CleanedData () const |
| Get the cleaned data matrix. More... | |
| void | GetRecommendations (const size_t numRecs, arma::Mat< size_t > &recommendations) |
| Generates the given number of recommendations for all users. More... | |
| void | GetRecommendations (const size_t numRecs, arma::Mat< size_t > &recommendations, const arma::Col< size_t > &users) |
| Generates the given number of recommendations for the specified users. More... | |
| const arma::mat & | H () const |
| Get the Item Matrix. More... | |
| void | NumUsersForSimilarity (const size_t num) |
| Sets number of users for calculating similarity. More... | |
| size_t | NumUsersForSimilarity () const |
| Gets number of users for calculating similarity. More... | |
| double | Predict (const size_t user, const size_t item) const |
| Predict the rating of an item by a particular user. More... | |
| void | Predict (const arma::Mat< size_t > &combinations, arma::vec &predictions) const |
| Predict ratings for each user-item combination in the given coordinate list matrix. More... | |
| void | Rank (const size_t rankValue) |
| Sets rank parameter for matrix factorization. More... | |
| size_t | Rank () const |
| Gets rank parameter for matrix factorization. More... | |
template < typename Archive > | |
| void | serialize (Archive &ar, const unsigned int) |
| Serialize the CFType model to the given archive. More... | |
template < typename DecompositionPolicy > | |
| void | Train (const arma::mat &data, DecompositionPolicy &decomposition, const size_t maxIterations=1000, const double minResidue=1e-5, const bool mit=false) |
| Train the CFType model (i.e. More... | |
template < typename DecompositionPolicy > | |
| void | Train (const arma::sp_mat &data, DecompositionPolicy &decomposition, const size_t maxIterations=1000, const double minResidue=1e-5, const bool mit=false) |
| Train the CFType model (i.e. More... | |
| const arma::mat & | W () const |
| Get the User Matrix. More... | |
Static Public Member Functions | |
| static void | CleanData (const arma::mat &data, arma::sp_mat &cleanedData) |
| Converts the User, Item, Value Matrix to User-Item Table. More... | |
This class implements Collaborative Filtering (CF).
This implementation presently supports Alternating Least Squares (ALS) for collaborative filtering.
A simple example of how to run Collaborative Filtering is shown below.
The data matrix is a (user, item, rating) table. Each column in the matrix should have three rows. The first represents the user; the second represents the item; and the third represents the rating. The user and item, while they are in a matrix that holds doubles, should hold integer (or size_t) values. The user and item indices are assumed to start at 0.
| DecompositionPolicy | The algorithm to use to decompose the rating matrix (a W and H matrix). |
| CFType | ( | const size_t | numUsersForSimilarity = 5, |
| const size_t | rank = 0 |
||
| ) |
Initialize the CFType object without performing any factorization.
Be sure to call Train() before calling GetRecommendations() or any other functions!
| CFType | ( | const MatType & | data, |
| DecompositionPolicy & | decomposition = DecompositionPolicy(), |
||
| const size_t | numUsersForSimilarity = 5, |
||
| const size_t | rank = 0, |
||
| const size_t | maxIterations = 1000, |
||
| const double | minResidue = 1e-5, |
||
| const bool | mit = false |
||
| ) |
Initialize the CFType object using any decomposition method, immediately factorizing the given data to create a model.
There are parameters that can be set; default values are provided for each of them. If the rank is left unset (or is set to 0), a simple density-based heuristic will be used to choose a rank.
The provided dataset can be a coordinate list; that is, a 3-row matrix where each column corresponds to a (user, item, rating) entry in the matrix or a sparse matrix representing (user, item) table.
| data | Data matrix: dense matrix (coordinate lists) or sparse matrix(cleaned). |
| decomposition | Instantiated DecompositionPolicy object. |
| numUsersForSimilarity | Size of the neighborhood. |
| rank | Rank parameter for matrix factorization. |
| maxIterations | Maximum number of iterations. |
| minResidue | Residue required to terminate. |
| mit | Whether to terminate only when maxIterations is reached. |
|
static |
Converts the User, Item, Value Matrix to User-Item Table.
Referenced by CFType::CleanedData().
|
inline |
Get the cleaned data matrix.
Definition at line 172 of file cf.hpp.
References CFType::CleanData(), CFType::GetRecommendations(), CFType::Predict(), and CFType::serialize().
| void GetRecommendations | ( | const size_t | numRecs, |
| arma::Mat< size_t > & | recommendations | ||
| ) |
Generates the given number of recommendations for all users.
| numRecs | Number of Recommendations. |
| recommendations | Matrix to save recommendations into. |
Referenced by CFType::CleanedData().
| void GetRecommendations | ( | const size_t | numRecs, |
| arma::Mat< size_t > & | recommendations, | ||
| const arma::Col< size_t > & | users | ||
| ) |
Generates the given number of recommendations for the specified users.
| numRecs | Number of Recommendations. |
| recommendations | Matrix to save recommendations. |
| users | Users for which recommendations are to be generated. |
|
inline |
|
inline |
| double Predict | ( | const size_t | user, |
| const size_t | item | ||
| ) | const |
Predict the rating of an item by a particular user.
| user | User to predict for. |
| item | Item to predict for. |
Referenced by CFType::CleanedData().
| void Predict | ( | const arma::Mat< size_t > & | combinations, |
| arma::vec & | predictions | ||
| ) | const |
Predict ratings for each user-item combination in the given coordinate list matrix.
The matrix 'combinations' should have two rows and number of columns equal to the number of desired predictions. The first element of each column corresponds to the user index, and the second element of each column corresponds to the item index. The output vector 'predictions' will have length equal to combinations.n_cols, and predictions[i] will be equal to the prediction for the user/item combination in combinations.col(i).
| combinations | User/item combinations to predict. |
| predictions | Predicted ratings for each user/item combination. |
|
inline |
|
inline |
| void serialize | ( | Archive & | ar, |
| const unsigned | int | ||
| ) |
Serialize the CFType model to the given archive.
Referenced by CFType::CleanedData().
| void Train | ( | const arma::mat & | data, |
| DecompositionPolicy & | decomposition, | ||
| const size_t | maxIterations = 1000, |
||
| const double | minResidue = 1e-5, |
||
| const bool | mit = false |
||
| ) |
Train the CFType model (i.e.
factorize the input matrix) using the parameters that have already been set for the model (specifically, the rank parameter), and optionally, using the given DecompositionPolicy.
| data | Input dataset; dense matrix (coordinate lists). |
| decomposition | Instantiated DecompositionPolicy object. |
| maxIterations | Maximum number of iterations. |
| minResidue | Residue required to terminate. |
| mit | Whether to terminate only when maxIterations is reached. |
| void Train | ( | const arma::sp_mat & | data, |
| DecompositionPolicy & | decomposition, | ||
| const size_t | maxIterations = 1000, |
||
| const double | minResidue = 1e-5, |
||
| const bool | mit = false |
||
| ) |
Train the CFType model (i.e.
factorize the input matrix) using the parameters that have already been set for the model (specifically, the rank parameter), and optionally, using the given DecompositionPolicy.
| data | Input dataset; sparse matrix (user item table). |
| decomposition | Instantiated DecompositionPolicy object. |
| maxIterations | Maximum number of iterations. |
| minResidue | Residue required to terminate. |
| mit | Whether to terminate only when maxIterations is reached. |