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>
25 #include <set>
26 #include <map>
27 #include <iostream>
28 
29 namespace mlpack {
30 namespace cf {
62 class CFType
63 {
64  public:
69  CFType(const size_t numUsersForSimilarity = 5,
70  const size_t rank = 0);
71 
92  template<typename MatType, typename DecompositionPolicy = NMFPolicy>
93  CFType(const MatType& data,
94  DecompositionPolicy& decomposition = DecompositionPolicy(),
95  const size_t numUsersForSimilarity = 5,
96  const size_t rank = 0,
97  const size_t maxIterations = 1000,
98  const double minResidue = 1e-5,
99  const bool mit = false);
100 
112  template<typename DecompositionPolicy>
113  void Train(const arma::mat& data,
114  DecompositionPolicy& decomposition,
115  const size_t maxIterations = 1000,
116  const double minResidue = 1e-5,
117  const bool mit = false);
118 
130  template<typename DecompositionPolicy>
131  void Train(const arma::sp_mat& data,
132  DecompositionPolicy& decomposition,
133  const size_t maxIterations = 1000,
134  const double minResidue = 1e-5,
135  const bool mit = false);
136 
138  void NumUsersForSimilarity(const size_t num)
139  {
140  if (num < 1)
141  {
142  Log::Warn << "CFType::NumUsersForSimilarity(): invalid value (< 1) "
143  "ignored." << std::endl;
144  return;
145  }
146  this->numUsersForSimilarity = num;
147  }
148 
150  size_t NumUsersForSimilarity() const
151  {
152  return numUsersForSimilarity;
153  }
154 
156  void Rank(const size_t rankValue)
157  {
158  this->rank = rankValue;
159  }
160 
162  size_t Rank() const
163  {
164  return rank;
165  }
166 
168  const arma::mat& W() const { return w; }
170  const arma::mat& H() const { return h; }
172  const arma::sp_mat& CleanedData() const { return cleanedData; }
173 
180  void GetRecommendations(const size_t numRecs,
181  arma::Mat<size_t>& recommendations);
182 
190  void GetRecommendations(const size_t numRecs,
191  arma::Mat<size_t>& recommendations,
192  const arma::Col<size_t>& users);
193 
195  static void CleanData(const arma::mat& data, arma::sp_mat& cleanedData);
196 
203  double Predict(const size_t user, const size_t item) const;
204 
217  void Predict(const arma::Mat<size_t>& combinations,
218  arma::vec& predictions) const;
219 
223  template<typename Archive>
224  void serialize(Archive& ar, const unsigned int /* version */);
225 
226  private:
228  size_t numUsersForSimilarity;
230  size_t rank;
232  arma::mat w;
234  arma::mat h;
236  arma::sp_mat cleanedData;
237 
239  typedef std::pair<double, size_t> Candidate;
240 
242  struct CandidateCmp {
243  bool operator()(const Candidate& c1, const Candidate& c2)
244  {
245  return c1.first > c2.first;
246  };
247  };
248 }; // class CFType
249 
250 } // namespace cf
251 } // namespace mlpack
252 
253 // Include implementation of templated functions.
254 #include "cf_impl.hpp"
255 
256 #endif
const arma::sp_mat & CleanedData() const
Get the cleaned data matrix.
Definition: cf.hpp:172
void NumUsersForSimilarity(const size_t num)
Sets number of users for calculating similarity.
Definition: cf.hpp:138
.hpp
Definition: add_to_po.hpp:21
void serialize(Archive &ar, const unsigned int)
Serialize the CFType model to the given archive.
The core includes that mlpack expects; standard C++ includes and Armadillo.
const arma::mat & H() const
Get the Item Matrix.
Definition: cf.hpp:170
static void CleanData(const arma::mat &data, arma::sp_mat &cleanedData)
Converts the User, Item, Value Matrix to User-Item Table.
size_t Rank() const
Gets rank parameter for matrix factorization.
Definition: cf.hpp:162
void GetRecommendations(const size_t numRecs, arma::Mat< size_t > &recommendations)
Generates the given number of recommendations for all users.
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:62
size_t NumUsersForSimilarity() const
Gets number of users for calculating similarity.
Definition: cf.hpp:150
CFType(const size_t numUsersForSimilarity=5, const size_t rank=0)
Initialize the CFType object without performing any factorization.
void Train(const arma::mat &data, DecompositionPolicy &decomposition, const size_t maxIterations=1000, const double minResidue=1e-5, const bool mit=false)
Train the CFType model (i.e.
double Predict(const size_t user, const size_t item) const
Predict the rating of an item by a particular user.
const arma::mat & W() const
Get the User Matrix.
Definition: cf.hpp:168
void Rank(const size_t rankValue)
Sets rank parameter for matrix factorization.
Definition: cf.hpp:156