mlpack
Classes | Public Member Functions | Static Public Member Functions | List of all members
mlpack::cf::CFType< DecompositionPolicy, NormalizationType > Class Template Reference

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

#include <cf.hpp>

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 >
 CFType (const MatType &data, const 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...
 
void Train (const arma::mat &data, const DecompositionPolicy &decomposition, const size_t maxIterations=1000, const double minResidue=1e-5, const bool mit=false)
 Train the CFType model (i.e. More...
 
void Train (const arma::sp_mat &data, const DecompositionPolicy &decomposition, const size_t maxIterations=1000, const double minResidue=1e-5, const bool mit=false)
 Train the CFType model (i.e. More...
 
void NumUsersForSimilarity (const size_t num)
 Sets number of users for calculating similarity.
 
size_t NumUsersForSimilarity () const
 Gets number of users for calculating similarity.
 
void Rank (const size_t rankValue)
 Sets rank parameter for matrix factorization.
 
size_t Rank () const
 Gets rank parameter for matrix factorization.
 
const DecompositionPolicy & Decomposition () const
 Gets decomposition object.
 
const arma::sp_mat & CleanedData () const
 Get the cleaned data matrix.
 
const NormalizationType & Normalization () const
 Get the normalization object.
 
template<typename NeighborSearchPolicy = EuclideanSearch, typename InterpolationPolicy = AverageInterpolation>
void GetRecommendations (const size_t numRecs, arma::Mat< size_t > &recommendations)
 Generates the given number of recommendations for all users. More...
 
template<typename NeighborSearchPolicy = EuclideanSearch, typename InterpolationPolicy = AverageInterpolation>
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...
 
template<typename NeighborSearchPolicy = EuclideanSearch, typename InterpolationPolicy = AverageInterpolation>
double Predict (const size_t user, const size_t item) const
 Predict the rating of an item by a particular user. More...
 
template<typename NeighborSearchPolicy = EuclideanSearch, typename InterpolationPolicy = AverageInterpolation>
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...
 
template<typename Archive >
void serialize (Archive &ar, const uint32_t)
 Serialize the CFType model to the given archive. 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.
 

Detailed Description

template<typename DecompositionPolicy = NMFPolicy, typename NormalizationType = NoNormalization>
class mlpack::cf::CFType< DecompositionPolicy, NormalizationType >

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
CFType<> 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
DecompositionPolicyThe policy used to decompose the rating matrix. It also provides methods to compute prediction and neighborhood.
NormalizationTypeThe type of normalization performed on raw data. Data is normalized before calling Train() method. Predicted rating is denormalized before return.

Constructor & Destructor Documentation

◆ CFType() [1/2]

template<typename DecompositionPolicy , typename NormalizationType >
mlpack::cf::CFType< DecompositionPolicy, NormalizationType >::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() [2/2]

template<typename DecompositionPolicy, typename NormalizationType >
template<typename MatType >
mlpack::cf::CFType< DecompositionPolicy, NormalizationType >::CFType ( const MatType &  data,
const 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.

Construct the CF object using an instantiated decomposition policy.

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.

Template Parameters
MatTypeThe type of input matrix, which is expected to be either arma::mat (table of (user, item, rating)) or arma::sp_mat (sparse rating matrix where row is item and column is user).
Parameters
dataData matrix: dense matrix (coordinate lists) or sparse matrix(cleaned).
decompositionInstantiated DecompositionPolicy object.
numUsersForSimilaritySize of the neighborhood.
rankRank parameter for matrix factorization.
maxIterationsMaximum number of iterations.
minResidueResidue required to terminate.
mitWhether to terminate only when maxIterations is reached.

Member Function Documentation

◆ GetRecommendations() [1/2]

template<typename DecompositionPolicy , typename NormalizationType >
template<typename NeighborSearchPolicy , typename InterpolationPolicy >
void mlpack::cf::CFType< DecompositionPolicy, NormalizationType >::GetRecommendations ( const size_t  numRecs,
arma::Mat< size_t > &  recommendations 
)

Generates the given number of recommendations for all users.

Template Parameters
NeighborSearchPolicyThe policy used to search neighbors of query set in referece set.
InterpolationPolicyThe policy used to calculate interpolation weights.
Parameters
numRecsNumber of Recommendations.
recommendationsMatrix to save recommendations into.

◆ GetRecommendations() [2/2]

template<typename DecompositionPolicy , typename NormalizationType >
template<typename NeighborSearchPolicy , typename InterpolationPolicy >
void mlpack::cf::CFType< DecompositionPolicy, NormalizationType >::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.

Template Parameters
NeighborSearchPolicyThe policy used to search neighbors of query set in referece set.
InterpolationPolicyThe policy used to calculate interpolation weights.
Parameters
numRecsNumber of Recommendations.
recommendationsMatrix to save recommendations.
usersUsers for which recommendations are to be generated.

◆ Predict() [1/2]

template<typename DecompositionPolicy , typename NormalizationType >
template<typename NeighborSearchPolicy , typename InterpolationPolicy >
double mlpack::cf::CFType< DecompositionPolicy, NormalizationType >::Predict ( const size_t  user,
const size_t  item 
) const

Predict the rating of an item by a particular user.

Template Parameters
NeighborSearchPolicyThe policy used to search neighbors of query set in referece set.
InterpolationPolicyThe policy used to calculate interpolation weights.
Parameters
userUser to predict for.
itemItem to predict for.

◆ Predict() [2/2]

template<typename DecompositionPolicy , typename NormalizationType >
template<typename NeighborSearchPolicy , typename InterpolationPolicy >
void mlpack::cf::CFType< DecompositionPolicy, NormalizationType >::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).

Template Parameters
NeighborSearchPolicyThe policy used to search neighbors of query set in referece set.
InterpolationPolicyThe policy used to calculate interpolation weights.
Parameters
combinationsUser/item combinations to predict.
predictionsPredicted ratings for each user/item combination.

◆ serialize()

template<typename DecompositionPolicy , typename NormalizationType >
template<typename Archive >
void mlpack::cf::CFType< DecompositionPolicy, NormalizationType >::serialize ( Archive &  ar,
const uint32_t   
)

Serialize the CFType model to the given archive.

Serialize the model.

◆ Train() [1/2]

template<typename DecompositionPolicy, typename NormalizationType >
void mlpack::cf::CFType< DecompositionPolicy, NormalizationType >::Train ( const arma::mat &  data,
const 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.

Parameters
dataInput dataset; dense matrix (coordinate lists).
decompositionInstantiated DecompositionPolicy object.
maxIterationsMaximum number of iterations.
minResidueResidue required to terminate.
mitWhether to terminate only when maxIterations is reached.

◆ Train() [2/2]

template<typename DecompositionPolicy, typename NormalizationType >
void mlpack::cf::CFType< DecompositionPolicy, NormalizationType >::Train ( const arma::sp_mat &  data,
const 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.

Parameters
dataInput dataset; sparse matrix (user item table).
decompositionInstantiated DecompositionPolicy object.
maxIterationsMaximum number of iterations.
minResidueResidue required to terminate.
mitWhether to terminate only when maxIterations is reached.

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