mlpack  master
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 std::enable_if_t<
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 std::enable_if_t<
163 
165  void NumUsersForSimilarity(const size_t num)
166  {
167  if (num < 1)
168  {
169  Log::Warn << "CF::NumUsersForSimilarity(): invalid value (< 1) "
170  "ignored." << std::endl;
171  return;
172  }
173  this->numUsersForSimilarity = num;
174  }
175 
177  size_t NumUsersForSimilarity() const
178  {
179  return numUsersForSimilarity;
180  }
181 
183  void Rank(const size_t rankValue)
184  {
185  this->rank = rankValue;
186  }
187 
189  size_t Rank() const
190  {
191  return rank;
192  }
193 
195  const arma::mat& W() const { return w; }
197  const arma::mat& H() const { return h; }
199  const arma::sp_mat& CleanedData() const { return cleanedData; }
200 
207  void GetRecommendations(const size_t numRecs,
208  arma::Mat<size_t>& recommendations);
209 
217  void GetRecommendations(const size_t numRecs,
218  arma::Mat<size_t>& recommendations,
219  const arma::Col<size_t>& users);
220 
222  static void CleanData(const arma::mat& data, arma::sp_mat& cleanedData);
223 
230  double Predict(const size_t user, const size_t item) const;
231 
244  void Predict(const arma::Mat<size_t>& combinations,
245  arma::vec& predictions) const;
246 
250  template<typename Archive>
251  void Serialize(Archive& ar, const unsigned int /* version */);
252 
253  private:
257  size_t rank;
259  arma::mat w;
261  arma::mat h;
263  arma::sp_mat cleanedData;
264 
266  typedef std::pair<double, size_t> Candidate;
267 
269  struct CandidateCmp {
270  bool operator()(const Candidate& c1, const Candidate& c2)
271  {
272  return c1.first > c2.first;
273  };
274  };
275 }; // class CF
276 
277 } // namespace cf
278 } // namespace mlpack
279 
280 // Include implementation of templated functions.
281 #include "cf_impl.hpp"
282 
283 #endif
const arma::sp_mat & CleanedData() const
Get the cleaned data matrix.
Definition: cf.hpp:199
arma::sp_mat cleanedData
Cleaned data matrix.
Definition: cf.hpp:263
size_t NumUsersForSimilarity() const
Gets number of users for calculating similarity.
Definition: cf.hpp:177
size_t rank
Rank used for matrix factorization.
Definition: cf.hpp:257
size_t Rank() const
Gets rank parameter for matrix factorization.
Definition: cf.hpp:189
const arma::mat & W() const
Get the User Matrix.
Definition: cf.hpp:195
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: binarize.hpp:18
The core includes that mlpack expects; standard C++ includes and Armadillo.
Compare two candidates based on the value.
Definition: cf.hpp:269
arma::mat h
Item matrix.
Definition: cf.hpp:261
bool operator()(const Candidate &c1, const Candidate &c2)
Definition: cf.hpp:270
static MLPACK_EXPORT util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:87
static const bool UsesCoordinateList
If true, then the passed data matrix is used for factorizer.Apply().
Definition: cf.hpp:45
void NumUsersForSimilarity(const size_t num)
Sets number of users for calculating similarity.
Definition: cf.hpp:165
const arma::mat & H() const
Get the Item Matrix.
Definition: cf.hpp:197
arma::mat w
User matrix.
Definition: cf.hpp:259
size_t numUsersForSimilarity
Number of users for similarity.
Definition: cf.hpp:255
std::pair< double, size_t > Candidate
Candidate represents a possible recommendation (value, item).
Definition: cf.hpp:266
Template class for factorizer traits.
Definition: cf.hpp:39
void Rank(const size_t rankValue)
Sets rank parameter for matrix factorization.
Definition: cf.hpp:183
This class implements Collaborative Filtering (CF).
Definition: cf.hpp:80
typename enable_if< B, T >::type enable_if_t
Definition: prereqs.hpp:59