mlpack  2.0.1
cf.hpp
Go to the documentation of this file.
1 
18 #ifndef __MLPACK_METHODS_CF_CF_HPP
19 #define __MLPACK_METHODS_CF_CF_HPP
20 
21 #include <mlpack/core.hpp>
26 #include <set>
27 #include <map>
28 #include <iostream>
29 
30 namespace mlpack {
31 namespace cf {
32 
40 template<typename FactorizerType>
42 {
47  static const bool UsesCoordinateList = false;
48 };
49 
82 class CF
83 {
84  public:
89  CF(const size_t numUsersForSimilarity = 5,
90  const size_t rank = 0);
91 
108  template<typename FactorizerType = amf::NMFALSFactorizer>
109  CF(const arma::mat& data,
110  FactorizerType factorizer = FactorizerType(),
111  const size_t numUsersForSimilarity = 5,
112  const size_t rank = 0);
113 
132  template<typename FactorizerType = amf::NMFALSFactorizer>
133  CF(const arma::sp_mat& data,
134  FactorizerType factorizer = FactorizerType(),
135  const size_t numUsersForSimilarity = 5,
136  const size_t rank = 0,
137  const typename boost::disable_if_c<
139 
148  template<typename FactorizerType = amf::NMFALSFactorizer>
149  void Train(const arma::mat& data,
150  FactorizerType factorizer = FactorizerType());
151 
160  template<typename FactorizerType = amf::NMFALSFactorizer>
161  void Train(const arma::sp_mat& data,
162  FactorizerType factorizer = FactorizerType(),
163  const typename boost::disable_if_c<
165  = 0);
166 
168  void NumUsersForSimilarity(const size_t num)
169  {
170  if (num < 1)
171  {
172  Log::Warn << "CF::NumUsersForSimilarity(): invalid value (< 1) "
173  "ignored." << std::endl;
174  return;
175  }
176  this->numUsersForSimilarity = num;
177  }
178 
180  size_t NumUsersForSimilarity() const
181  {
182  return numUsersForSimilarity;
183  }
184 
186  void Rank(const size_t rankValue)
187  {
188  this->rank = rankValue;
189  }
190 
192  size_t Rank() const
193  {
194  return rank;
195  }
196 
198  const arma::mat& W() const { return w; }
200  const arma::mat& H() const { return h; }
202  const arma::sp_mat& CleanedData() const { return cleanedData; }
203 
210  void GetRecommendations(const size_t numRecs,
211  arma::Mat<size_t>& recommendations);
212 
220  void GetRecommendations(const size_t numRecs,
221  arma::Mat<size_t>& recommendations,
222  arma::Col<size_t>& users);
223 
225  static void CleanData(const arma::mat& data, arma::sp_mat& cleanedData);
226 
233  double Predict(const size_t user, const size_t item) const;
234 
247  void Predict(const arma::Mat<size_t>& combinations,
248  arma::vec& predictions) const;
249 
253  template<typename Archive>
254  void Serialize(Archive& ar, const unsigned int /* version */);
255 
256  private:
260  size_t rank;
262  arma::mat w;
264  arma::mat h;
266  arma::sp_mat cleanedData;
267 
277  void InsertNeighbor(const size_t queryIndex,
278  const size_t pos,
279  const size_t neighbor,
280  const double value,
281  arma::Mat<size_t>& recommendations,
282  arma::mat& values) const;
283 
284 }; // class CF
285 
286 } // namespace cf
287 } // namespace mlpack
288 
289 // Include implementation of templated functions.
290 #include "cf_impl.hpp"
291 
292 #endif
const arma::sp_mat & CleanedData() const
Get the cleaned data matrix.
Definition: cf.hpp:202
arma::sp_mat cleanedData
Cleaned data matrix.
Definition: cf.hpp:266
size_t NumUsersForSimilarity() const
Gets number of users for calculating similarity.
Definition: cf.hpp:180
size_t rank
Rank used for matrix factorization.
Definition: cf.hpp:260
size_t Rank() const
Gets rank parameter for matrix factorization.
Definition: cf.hpp:192
const arma::mat & W() const
Get the User Matrix.
Definition: cf.hpp:198
Linear algebra utility functions, generally performed on matrices or vectors.
arma::mat h
Item matrix.
Definition: cf.hpp:264
static const bool UsesCoordinateList
If true, then the passed data matrix is used for factorizer.Apply().
Definition: cf.hpp:47
void NumUsersForSimilarity(const size_t num)
Sets number of users for calculating similarity.
Definition: cf.hpp:168
const arma::mat & H() const
Get the Item Matrix.
Definition: cf.hpp:200
arma::mat w
User matrix.
Definition: cf.hpp:262
Include all of the base components required to write MLPACK methods, and the main MLPACK Doxygen docu...
size_t numUsersForSimilarity
Number of users for similarity.
Definition: cf.hpp:258
Template class for factorizer traits.
Definition: cf.hpp:41
static util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:84
void Rank(const size_t rankValue)
Sets rank parameter for matrix factorization.
Definition: cf.hpp:186
This class implements Collaborative Filtering (CF).
Definition: cf.hpp:82