mlpack  2.2.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
cf.hpp
Go to the documentation of this file.
1 
16 #ifndef MLPACK_METHODS_CF_CF_HPP
17 #define MLPACK_METHODS_CF_CF_HPP
18 
19 #include <mlpack/prereqs.hpp>
24 #include <set>
25 #include <map>
26 #include <iostream>
27 
28 namespace mlpack {
29 namespace cf {
30 
38 template<typename FactorizerType>
40 {
45  static const bool UsesCoordinateList = false;
46 };
47 
80 class CF
81 {
82  public:
87  CF(const size_t numUsersForSimilarity = 5,
88  const size_t rank = 0);
89 
106  template<typename FactorizerType = amf::NMFALSFactorizer>
107  CF(const arma::mat& data,
108  FactorizerType factorizer = FactorizerType(),
109  const size_t numUsersForSimilarity = 5,
110  const size_t rank = 0);
111 
130  template<typename FactorizerType = amf::NMFALSFactorizer>
131  CF(const arma::sp_mat& data,
132  FactorizerType factorizer = FactorizerType(),
133  const size_t numUsersForSimilarity = 5,
134  const size_t rank = 0,
135  const typename boost::disable_if_c<
137 
146  template<typename FactorizerType = amf::NMFALSFactorizer>
147  void Train(const arma::mat& data,
148  FactorizerType factorizer = FactorizerType());
149 
158  template<typename FactorizerType = amf::NMFALSFactorizer>
159  void Train(const arma::sp_mat& data,
160  FactorizerType factorizer = FactorizerType(),
161  const typename boost::disable_if_c<
163  = 0);
164 
166  void NumUsersForSimilarity(const size_t num)
167  {
168  if (num < 1)
169  {
170  Log::Warn << "CF::NumUsersForSimilarity(): invalid value (< 1) "
171  "ignored." << std::endl;
172  return;
173  }
174  this->numUsersForSimilarity = num;
175  }
176 
178  size_t NumUsersForSimilarity() const
179  {
180  return numUsersForSimilarity;
181  }
182 
184  void Rank(const size_t rankValue)
185  {
186  this->rank = rankValue;
187  }
188 
190  size_t Rank() const
191  {
192  return rank;
193  }
194 
196  const arma::mat& W() const { return w; }
198  const arma::mat& H() const { return h; }
200  const arma::sp_mat& CleanedData() const { return cleanedData; }
201 
208  void GetRecommendations(const size_t numRecs,
209  arma::Mat<size_t>& recommendations);
210 
218  void GetRecommendations(const size_t numRecs,
219  arma::Mat<size_t>& recommendations,
220  arma::Col<size_t>& users);
221 
223  static void CleanData(const arma::mat& data, arma::sp_mat& cleanedData);
224 
231  double Predict(const size_t user, const size_t item) const;
232 
245  void Predict(const arma::Mat<size_t>& combinations,
246  arma::vec& predictions) const;
247 
251  template<typename Archive>
252  void Serialize(Archive& ar, const unsigned int /* version */);
253 
254  private:
256  size_t numUsersForSimilarity;
258  size_t rank;
260  arma::mat w;
262  arma::mat h;
264  arma::sp_mat cleanedData;
265 
267  typedef std::pair<double, size_t> Candidate;
268 
270  struct CandidateCmp {
271  bool operator()(const Candidate& c1, const Candidate& c2)
272  {
273  return c1.first > c2.first;
274  };
275  };
276 }; // class CF
277 
278 } // namespace cf
279 } // namespace mlpack
280 
281 // Include implementation of templated functions.
282 #include "cf_impl.hpp"
283 
284 #endif
double Predict(const size_t user, const size_t item) const
Predict the rating of an item by a particular user.
CF(const size_t numUsersForSimilarity=5, const size_t rank=0)
Initialize the CF object without performing any factorization.
const arma::mat & W() const
Get the User Matrix.
Definition: cf.hpp:196
const arma::sp_mat & CleanedData() const
Get the cleaned data matrix.
Definition: cf.hpp:200
static const bool UsesCoordinateList
If true, then the passed data matrix is used for factorizer.Apply().
Definition: cf.hpp:45
The core includes that mlpack expects; standard C++ includes and Armadillo.
const arma::mat & H() const
Get the Item Matrix.
Definition: cf.hpp:198
size_t NumUsersForSimilarity() const
Gets number of users for calculating similarity.
Definition: cf.hpp:178
void Train(const arma::mat &data, FactorizerType factorizer=FactorizerType())
Train the CF model (i.e.
void Rank(const size_t rankValue)
Sets rank parameter for matrix factorization.
Definition: cf.hpp:184
size_t Rank() const
Gets rank parameter for matrix factorization.
Definition: cf.hpp:190
static void CleanData(const arma::mat &data, arma::sp_mat &cleanedData)
Converts the User, Item, Value Matrix to User-Item Table.
Template class for factorizer traits.
Definition: cf.hpp:39
static MLPACK_EXPORT util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:87
This class implements Collaborative Filtering (CF).
Definition: cf.hpp:80
void NumUsersForSimilarity(const size_t num)
Sets number of users for calculating similarity.
Definition: cf.hpp:166
void GetRecommendations(const size_t numRecs, arma::Mat< size_t > &recommendations)
Generates the given number of recommendations for all users.
void Serialize(Archive &ar, const unsigned int)
Serialize the CF model to the given archive.