mlpack  master
Classes | Public Member Functions | Static Public Member Functions | Private Types | Private Attributes | List of all members
mlpack::cf::CF Class Reference

This class implements Collaborative Filtering (CF). More...

Classes

struct  CandidateCmp
 Compare two candidates based on the value. More...
 

Public Member Functions

 CF (const size_t numUsersForSimilarity=5, const size_t rank=0)
 Initialize the CF object without performing any factorization. More...
 
template<typename FactorizerType = amf::NMFALSFactorizer>
 CF (const arma::mat &data, FactorizerType factorizer=FactorizerType(), const size_t numUsersForSimilarity=5, const size_t rank=0)
 Initialize the CF object using an instantiated factorizer, immediately factorizing the given data to create a model. More...
 
template<typename FactorizerType = amf::NMFALSFactorizer>
 CF (const arma::sp_mat &data, FactorizerType factorizer=FactorizerType(), const size_t numUsersForSimilarity=5, const size_t rank=0, const typename std::enable_if_t< !FactorizerTraits< FactorizerType >::UsesCoordinateList > *=0)
 Initialize the CF object using an instantiated factorizer, 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 CF model to the given archive. More...
 
template<typename FactorizerType = amf::NMFALSFactorizer>
void Train (const arma::mat &data, FactorizerType factorizer=FactorizerType())
 Train the CF model (i.e. More...
 
template<typename FactorizerType = amf::NMFALSFactorizer>
void Train (const arma::sp_mat &data, FactorizerType factorizer=FactorizerType(), const typename std::enable_if_t< !FactorizerTraits< FactorizerType >::UsesCoordinateList > *=0)
 Train the CF 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...
 

Private Types

typedef std::pair< double, size_t > Candidate
 Candidate represents a possible recommendation (value, item). More...
 

Private Attributes

arma::sp_mat cleanedData
 Cleaned data matrix. More...
 
arma::mat h
 Item matrix. More...
 
size_t numUsersForSimilarity
 Number of users for similarity. More...
 
size_t rank
 Rank used for matrix factorization. More...
 
arma::mat w
 User matrix. More...
 

Detailed Description

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.

extern arma::mat data; // (user, item, rating) table
extern arma::Col<size_t> users; // users seeking recommendations
arma::Mat<size_t> recommendations; // Recommendations
CF cf(data); // Default options.
// Generate 10 recommendations for all users.
cf.GetRecommendations(10, recommendations);
// Generate 10 recommendations for specified users.
cf.GetRecommendations(10, recommendations, users);

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.

Template Parameters
FactorizerTypeThe type of matrix factorization to use to decompose the rating matrix (a W and H matrix). This must implement the method Apply(arma::sp_mat& data, size_t rank, arma::mat& W, arma::mat& H).

Definition at line 80 of file cf.hpp.

Member Typedef Documentation

typedef std::pair<double, size_t> mlpack::cf::CF::Candidate
private

Candidate represents a possible recommendation (value, item).

Definition at line 266 of file cf.hpp.

Constructor & Destructor Documentation

mlpack::cf::CF::CF ( const size_t  numUsersForSimilarity = 5,
const size_t  rank = 0 
)

Initialize the CF object without performing any factorization.

Be sure to call Train() before calling GetRecommendations() or any other functions!

template<typename FactorizerType = amf::NMFALSFactorizer>
mlpack::cf::CF::CF ( const arma::mat &  data,
FactorizerType  factorizer = FactorizerType(),
const size_t  numUsersForSimilarity = 5,
const size_t  rank = 0 
)

Initialize the CF object using an instantiated factorizer, 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 should be a coordinate list; that is, a 3-row matrix where each column corresponds to a (user, item, rating) entry in the matrix.

Parameters
dataData matrix: coordinate list or dense matrix.
factorizerInstantiated factorizer object.
numUsersForSimilaritySize of the neighborhood.
rankRank parameter for matrix factorization.
template<typename FactorizerType = amf::NMFALSFactorizer>
mlpack::cf::CF::CF ( const arma::sp_mat &  data,
FactorizerType  factorizer = FactorizerType(),
const size_t  numUsersForSimilarity = 5,
const size_t  rank = 0,
const typename std::enable_if_t< !FactorizerTraits< FactorizerType >::UsesCoordinateList > *  = 0 
)

Initialize the CF object using an instantiated factorizer, 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. Data will be considered in the format of items vs. users and will be passed directly to the factorizer without cleaning. This overload of the constructor will only be available if the factorizer does not use a coordinate list (i.e. if UsesCoordinateList is false).

The U and T template parameters are for SFINAE, so that this overload is only available when the FactorizerType uses a coordinate list.

Parameters
dataSparse matrix data.
factorizerInstantiated factorizer object.
numUsersForSimilaritySize of the neighborhood.
rankRank parameter for matrix factorization.

Member Function Documentation

static void mlpack::cf::CF::CleanData ( const arma::mat &  data,
arma::sp_mat &  cleanedData 
)
static

Converts the User, Item, Value Matrix to User-Item Table.

const arma::sp_mat& mlpack::cf::CF::CleanedData ( ) const
inline

Get the cleaned data matrix.

Definition at line 199 of file cf.hpp.

void mlpack::cf::CF::GetRecommendations ( const size_t  numRecs,
arma::Mat< size_t > &  recommendations 
)

Generates the given number of recommendations for all users.

Parameters
numRecsNumber of Recommendations
recommendationsMatrix to save recommendations into.
void mlpack::cf::CF::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.

Parameters
numRecsNumber of Recommendations
recommendationsMatrix to save recommendations
usersUsers for which recommendations are to be generated
const arma::mat& mlpack::cf::CF::H ( ) const
inline

Get the Item Matrix.

Definition at line 197 of file cf.hpp.

void mlpack::cf::CF::NumUsersForSimilarity ( const size_t  num)
inline

Sets number of users for calculating similarity.

Definition at line 165 of file cf.hpp.

References mlpack::Log::Warn.

size_t mlpack::cf::CF::NumUsersForSimilarity ( ) const
inline

Gets number of users for calculating similarity.

Definition at line 177 of file cf.hpp.

double mlpack::cf::CF::Predict ( const size_t  user,
const size_t  item 
) const

Predict the rating of an item by a particular user.

Parameters
userUser to predict for.
itemItem to predict for.
void mlpack::cf::CF::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).

Parameters
combinationsUser/item combinations to predict.
predictionsPredicted ratings for each user/item combination.
void mlpack::cf::CF::Rank ( const size_t  rankValue)
inline

Sets rank parameter for matrix factorization.

Definition at line 183 of file cf.hpp.

size_t mlpack::cf::CF::Rank ( ) const
inline

Gets rank parameter for matrix factorization.

Definition at line 189 of file cf.hpp.

template<typename Archive >
void mlpack::cf::CF::Serialize ( Archive &  ar,
const unsigned  int 
)

Serialize the CF model to the given archive.

template<typename FactorizerType = amf::NMFALSFactorizer>
void mlpack::cf::CF::Train ( const arma::mat &  data,
FactorizerType  factorizer = FactorizerType() 
)

Train the CF 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 FactorizerType.

Parameters
dataInput dataset; coordinate list or dense matrix.
factorizerInstantiated factorizer.
template<typename FactorizerType = amf::NMFALSFactorizer>
void mlpack::cf::CF::Train ( const arma::sp_mat &  data,
FactorizerType  factorizer = FactorizerType(),
const typename std::enable_if_t< !FactorizerTraits< FactorizerType >::UsesCoordinateList > *  = 0 
)

Train the CF 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 FactorizerType.

Parameters
dataSparse matrix data.
factorizerInstantiated factorizer.
const arma::mat& mlpack::cf::CF::W ( ) const
inline

Get the User Matrix.

Definition at line 195 of file cf.hpp.

Member Data Documentation

arma::sp_mat mlpack::cf::CF::cleanedData
private

Cleaned data matrix.

Definition at line 263 of file cf.hpp.

arma::mat mlpack::cf::CF::h
private

Item matrix.

Definition at line 261 of file cf.hpp.

size_t mlpack::cf::CF::numUsersForSimilarity
private

Number of users for similarity.

Definition at line 255 of file cf.hpp.

size_t mlpack::cf::CF::rank
private

Rank used for matrix factorization.

Definition at line 257 of file cf.hpp.

arma::mat mlpack::cf::CF::w
private

User matrix.

Definition at line 259 of file cf.hpp.


The documentation for this class was generated from the following file: