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>
28 #include <set>
29 #include <map>
30 #include <iostream>
31 
32 namespace mlpack {
33 namespace cf {
68 template<typename DecompositionPolicy = NMFPolicy,
69  typename NormalizationType = NoNormalization>
70 class CFType
71 {
72  public:
77  CFType(const size_t numUsersForSimilarity = 5, const size_t rank = 0);
78 
103  template<typename MatType>
104  CFType(const MatType& data,
105  const DecompositionPolicy& decomposition = DecompositionPolicy(),
106  const size_t numUsersForSimilarity = 5,
107  const size_t rank = 0,
108  const size_t maxIterations = 1000,
109  const double minResidue = 1e-5,
110  const bool mit = false);
111 
123  void Train(const arma::mat& data,
124  const DecompositionPolicy& decomposition,
125  const size_t maxIterations = 1000,
126  const double minResidue = 1e-5,
127  const bool mit = false);
128 
140  void Train(const arma::sp_mat& data,
141  const DecompositionPolicy& decomposition,
142  const size_t maxIterations = 1000,
143  const double minResidue = 1e-5,
144  const bool mit = false);
145 
147  void NumUsersForSimilarity(const size_t num)
148  {
149  if (num < 1)
150  {
151  Log::Warn << "CFType::NumUsersForSimilarity(): invalid value (< 1) "
152  "ignored." << std::endl;
153  return;
154  }
155  this->numUsersForSimilarity = num;
156  }
157 
159  size_t NumUsersForSimilarity() const
160  {
161  return numUsersForSimilarity;
162  }
163 
165  void Rank(const size_t rankValue)
166  {
167  this->rank = rankValue;
168  }
169 
171  size_t Rank() const
172  {
173  return rank;
174  }
175 
177  const DecompositionPolicy& Decomposition() const { return decomposition; }
178 
180  const arma::sp_mat& CleanedData() const { return cleanedData; }
181 
183  const NormalizationType& Normalization() const { return normalization; }
184 
196  template<typename NeighborSearchPolicy = EuclideanSearch,
197  typename InterpolationPolicy = AverageInterpolation>
198  void GetRecommendations(const size_t numRecs,
199  arma::Mat<size_t>& recommendations);
200 
213  template<typename NeighborSearchPolicy = EuclideanSearch,
214  typename InterpolationPolicy = AverageInterpolation>
215  void GetRecommendations(const size_t numRecs,
216  arma::Mat<size_t>& recommendations,
217  const arma::Col<size_t>& users);
218 
220  static void CleanData(const arma::mat& data, arma::sp_mat& cleanedData);
221 
233  template<typename NeighborSearchPolicy = EuclideanSearch,
234  typename InterpolationPolicy = AverageInterpolation>
235  double Predict(const size_t user, const size_t item) const;
236 
254  template<typename NeighborSearchPolicy = EuclideanSearch,
255  typename InterpolationPolicy = AverageInterpolation>
256  void Predict(const arma::Mat<size_t>& combinations,
257  arma::vec& predictions) const;
258 
262  template<typename Archive>
263  void serialize(Archive& ar, const uint32_t /* version */);
264 
265  private:
267  size_t numUsersForSimilarity;
269  size_t rank;
271  DecompositionPolicy decomposition;
273  arma::sp_mat cleanedData;
275  NormalizationType normalization;
276 
278  typedef std::pair<double, size_t> Candidate;
279 
281  struct CandidateCmp {
282  bool operator()(const Candidate& c1, const Candidate& c2)
283  {
284  return c1.first > c2.first;
285  };
286  };
287 }; // class CFType
288 
289 } // namespace cf
290 } // namespace mlpack
291 
292 // Include implementation of templated functions.
293 #include "cf_impl.hpp"
294 
295 #endif
LMetricSearch< 2 > EuclideanSearch
const arma::sp_mat & CleanedData() const
Get the cleaned data matrix.
Definition: cf.hpp:180
void NumUsersForSimilarity(const size_t num)
Sets number of users for calculating similarity.
Definition: cf.hpp:147
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
Implementation of the NMF policy to act as a wrapper when accessing NMF from within CFType...
Definition: nmf_method.hpp:43
double Predict(const size_t user, const size_t item) const
Predict the rating of an item by a particular user.
This class performs average interpolation to generate interpolation weights for neighborhood-based co...
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:171
void Train(const arma::mat &data, const DecompositionPolicy &decomposition, const size_t maxIterations=1000, const double minResidue=1e-5, const bool mit=false)
Train the CFType model (i.e.
This normalization class doesn&#39;t perform any normalization.
const NormalizationType & Normalization() const
Get the normalization object.
Definition: cf.hpp:183
const DecompositionPolicy & Decomposition() const
Gets decomposition object.
Definition: cf.hpp:177
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:70
size_t NumUsersForSimilarity() const
Gets number of users for calculating similarity.
Definition: cf.hpp:159
CFType(const size_t numUsersForSimilarity=5, const size_t rank=0)
Initialize the CFType object without performing any factorization.
void Rank(const size_t rankValue)
Sets rank parameter for matrix factorization.
Definition: cf.hpp:165
void serialize(Archive &ar, const uint32_t)
Serialize the CFType model to the given archive.