MLPACK  1.0.11
Public Member Functions | Private Member Functions | Private Attributes | List of all members
mlpack::cf::CF< FactorizerType > Class Template Reference

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

Public Member Functions

 CF (arma::mat &data, const size_t numUsersForSimilarity=5, const size_t rank=0)
 Initialize the CF object. More...
 
const arma::sp_mat & CleanedData () const
 Get the cleaned data matrix. More...
 
const arma::mat & Data () const
 Get the data matrix. More...
 
void Factorizer (const FactorizerType &f)
 Sets factorizer for NMF. 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, 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...
 
void Rank (const size_t rankValue)
 Sets rank parameter for matrix factorization. More...
 
size_t Rank () const
 Gets rank parameter for matrix factorization. More...
 
const arma::mat & Rating () const
 Get the Rating Matrix. More...
 
std::string ToString () const
 Returns a string representation of this object. More...
 
const arma::mat & W () const
 Get the User Matrix. More...
 

Private Member Functions

void CleanData ()
 Converts the User, Item, Value Matrix to User-Item Table. More...
 
void InsertNeighbor (const size_t queryIndex, const size_t pos, const size_t neighbor, const double value, arma::Mat< size_t > &recommendations, arma::mat &values) const
 Helper function to insert a point into the recommendation matrices. More...
 

Private Attributes

arma::sp_mat cleanedData
 Cleaned data matrix. More...
 
arma::mat data
 Initial data matrix. More...
 
FactorizerType factorizer
 Instantiated factorizer object. 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 rating
 Rating matrix. More...
 
arma::mat w
 User matrix. More...
 

Detailed Description

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
class mlpack::cf::CF< FactorizerType >

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 76 of file cf.hpp.

Constructor & Destructor Documentation

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

Initialize the CF object.

Store a reference to the data that we will be using. 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.

Parameters
dataInitial (user, item, rating) matrix.
numUsersForSimilaritySize of the neighborhood.
rankRank parameter for matrix factorization.

Member Function Documentation

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
void mlpack::cf::CF< FactorizerType >::CleanData ( )
private

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

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
const arma::sp_mat& mlpack::cf::CF< FactorizerType >::CleanedData ( ) const
inline
template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
const arma::mat& mlpack::cf::CF< FactorizerType >::Data ( ) const
inline

Get the data matrix.

Definition at line 136 of file cf.hpp.

References mlpack::cf::CF< FactorizerType >::data.

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
void mlpack::cf::CF< FactorizerType >::Factorizer ( const FactorizerType &  f)
inline

Sets factorizer for NMF.

Definition at line 124 of file cf.hpp.

References mlpack::cf::CF< FactorizerType >::factorizer.

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
void mlpack::cf::CF< FactorizerType >::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.

Referenced by mlpack::cf::CF< FactorizerType >::CleanedData().

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
void mlpack::cf::CF< FactorizerType >::GetRecommendations ( const size_t  numRecs,
arma::Mat< size_t > &  recommendations,
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
template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
const arma::mat& mlpack::cf::CF< FactorizerType >::H ( ) const
inline

Get the Item Matrix.

Definition at line 132 of file cf.hpp.

References mlpack::cf::CF< FactorizerType >::h.

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
void mlpack::cf::CF< FactorizerType >::InsertNeighbor ( const size_t  queryIndex,
const size_t  pos,
const size_t  neighbor,
const double  value,
arma::Mat< size_t > &  recommendations,
arma::mat &  values 
) const
private

Helper function to insert a point into the recommendation matrices.

Parameters
queryIndexIndex of point whose recommendations we are inserting into.
posPosition in list to insert into.
neighborIndex of item being inserted as a recommendation.
valueValue of recommendation.
template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
void mlpack::cf::CF< FactorizerType >::NumUsersForSimilarity ( const size_t  num)
inline

Sets number of users for calculating similarity.

Definition at line 94 of file cf.hpp.

References mlpack::cf::CF< FactorizerType >::numUsersForSimilarity, and mlpack::Log::Warn.

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
size_t mlpack::cf::CF< FactorizerType >::NumUsersForSimilarity ( ) const
inline

Gets number of users for calculating similarity.

Definition at line 106 of file cf.hpp.

References mlpack::cf::CF< FactorizerType >::numUsersForSimilarity.

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
void mlpack::cf::CF< FactorizerType >::Rank ( const size_t  rankValue)
inline

Sets rank parameter for matrix factorization.

Definition at line 112 of file cf.hpp.

References mlpack::cf::CF< FactorizerType >::rank.

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
size_t mlpack::cf::CF< FactorizerType >::Rank ( ) const
inline

Gets rank parameter for matrix factorization.

Definition at line 118 of file cf.hpp.

References mlpack::cf::CF< FactorizerType >::rank.

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
const arma::mat& mlpack::cf::CF< FactorizerType >::Rating ( ) const
inline

Get the Rating Matrix.

Definition at line 134 of file cf.hpp.

References mlpack::cf::CF< FactorizerType >::rating.

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
std::string mlpack::cf::CF< FactorizerType >::ToString ( ) const

Returns a string representation of this object.

Referenced by mlpack::cf::CF< FactorizerType >::CleanedData().

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
const arma::mat& mlpack::cf::CF< FactorizerType >::W ( ) const
inline

Get the User Matrix.

Definition at line 130 of file cf.hpp.

References mlpack::cf::CF< FactorizerType >::w.

Member Data Documentation

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
arma::sp_mat mlpack::cf::CF< FactorizerType >::cleanedData
private

Cleaned data matrix.

Definition at line 181 of file cf.hpp.

Referenced by mlpack::cf::CF< FactorizerType >::CleanedData().

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
arma::mat mlpack::cf::CF< FactorizerType >::data
private

Initial data matrix.

Definition at line 167 of file cf.hpp.

Referenced by mlpack::cf::CF< FactorizerType >::Data().

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
FactorizerType mlpack::cf::CF< FactorizerType >::factorizer
private

Instantiated factorizer object.

Definition at line 173 of file cf.hpp.

Referenced by mlpack::cf::CF< FactorizerType >::Factorizer().

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
arma::mat mlpack::cf::CF< FactorizerType >::h
private

Item matrix.

Definition at line 177 of file cf.hpp.

Referenced by mlpack::cf::CF< FactorizerType >::H().

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
size_t mlpack::cf::CF< FactorizerType >::numUsersForSimilarity
private

Number of users for similarity.

Definition at line 169 of file cf.hpp.

Referenced by mlpack::cf::CF< FactorizerType >::NumUsersForSimilarity().

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
size_t mlpack::cf::CF< FactorizerType >::rank
private

Rank used for matrix factorization.

Definition at line 171 of file cf.hpp.

Referenced by mlpack::cf::CF< FactorizerType >::Rank().

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
arma::mat mlpack::cf::CF< FactorizerType >::rating
private

Rating matrix.

Definition at line 179 of file cf.hpp.

Referenced by mlpack::cf::CF< FactorizerType >::Rating().

template<typename FactorizerType = amf::AMF<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate>>
arma::mat mlpack::cf::CF< FactorizerType >::w
private

User matrix.

Definition at line 175 of file cf.hpp.

Referenced by mlpack::cf::CF< FactorizerType >::W().


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