MLPACK  1.0.11
cf.hpp
Go to the documentation of this file.
1 
25 #ifndef __MLPACK_METHODS_CF_CF_HPP
26 #define __MLPACK_METHODS_CF_CF_HPP
27 
28 #include <mlpack/core.hpp>
33 #include <set>
34 #include <map>
35 #include <iostream>
36 
37 namespace mlpack {
38 namespace cf {
39 
72 template<
73  typename FactorizerType = amf::AMF<amf::SimpleResidueTermination,
76 class CF
77 {
78  public:
89  CF(arma::mat& data,
90  const size_t numUsersForSimilarity = 5,
91  const size_t rank = 0);
92 
94  void NumUsersForSimilarity(const size_t num)
95  {
96  if (num < 1)
97  {
98  Log::Warn << "CF::NumUsersForSimilarity(): invalid value (< 1) "
99  "ignored." << std::endl;
100  return;
101  }
102  this->numUsersForSimilarity = num;
103  }
104 
106  size_t NumUsersForSimilarity() const
107  {
108  return numUsersForSimilarity;
109  }
110 
112  void Rank(const size_t rankValue)
113  {
114  this->rank = rankValue;
115  }
116 
118  size_t Rank() const
119  {
120  return rank;
121  }
122 
124  void Factorizer(const FactorizerType& f)
125  {
126  this->factorizer = f;
127  }
128 
130  const arma::mat& W() const { return w; }
132  const arma::mat& H() const { return h; }
134  const arma::mat& Rating() const { return rating; }
136  const arma::mat& Data() const { return data; }
138  const arma::sp_mat& CleanedData() const { return cleanedData; }
139 
146  void GetRecommendations(const size_t numRecs,
147  arma::Mat<size_t>& recommendations);
148 
156  void GetRecommendations(const size_t numRecs,
157  arma::Mat<size_t>& recommendations,
158  arma::Col<size_t>& users);
159 
163  std::string ToString() const;
164 
165  private:
167  arma::mat data;
171  size_t rank;
173  FactorizerType factorizer;
175  arma::mat w;
177  arma::mat h;
179  arma::mat rating;
181  arma::sp_mat cleanedData;
183  void CleanData();
184 
194  void InsertNeighbor(const size_t queryIndex,
195  const size_t pos,
196  const size_t neighbor,
197  const double value,
198  arma::Mat<size_t>& recommendations,
199  arma::mat& values) const;
200 
201 }; // class CF
202 
203 }; // namespace cf
204 }; // namespace mlpack
205 
206 //Include implementation
207 #include "cf_impl.hpp"
208 
209 #endif
std::string ToString() const
Returns a string representation of this object.
This class implements AMF (alternating matrix factorization) on the given matrix V.
Definition: amf.hpp:79
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:31
arma::mat rating
Rating matrix.
Definition: cf.hpp:179
FactorizerType factorizer
Instantiated factorizer object.
Definition: cf.hpp:173
CF(arma::mat &data, const size_t numUsersForSimilarity=5, const size_t rank=0)
Initialize the CF object.
void InsertNeighbor(const size_t queryIndex, const size_t pos, const size_t neighbor, const double value, arma::Mat< size_t > &recommendations, arma::mat &values) const
Helper function to insert a point into the recommendation matrices.
This class implements a simple residue-based termination policy.
const arma::mat & Data() const
Get the data matrix.
Definition: cf.hpp:136
size_t rank
Rank used for matrix factorization.
Definition: cf.hpp:171
void GetRecommendations(const size_t numRecs, arma::Mat< size_t > &recommendations)
Generates the given number of recommendations for all users.
void Rank(const size_t rankValue)
Sets rank parameter for matrix factorization.
Definition: cf.hpp:112
const arma::mat & Rating() const
Get the Rating Matrix.
Definition: cf.hpp:134
This class implements a method titled &#39;Alternating Least Squares&#39; described in the paper &#39;Positive Ma...
Definition: nmf_als.hpp:38
size_t numUsersForSimilarity
Number of users for similarity.
Definition: cf.hpp:169
void NumUsersForSimilarity(const size_t num)
Sets number of users for calculating similarity.
Definition: cf.hpp:94
size_t NumUsersForSimilarity() const
Gets number of users for calculating similarity.
Definition: cf.hpp:106
const arma::mat & H() const
Get the Item Matrix.
Definition: cf.hpp:132
const arma::mat & W() const
Get the User Matrix.
Definition: cf.hpp:130
arma::mat w
User matrix.
Definition: cf.hpp:175
void Factorizer(const FactorizerType &f)
Sets factorizer for NMF.
Definition: cf.hpp:124
arma::mat data
Initial data matrix.
Definition: cf.hpp:167
arma::mat h
Item matrix.
Definition: cf.hpp:177
size_t Rank() const
Gets rank parameter for matrix factorization.
Definition: cf.hpp:118
const arma::sp_mat & CleanedData() const
Get the cleaned data matrix.
Definition: cf.hpp:138
static util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:92
arma::sp_mat cleanedData
Cleaned data matrix.
Definition: cf.hpp:181
This class implements Collaborative Filtering (CF).
Definition: cf.hpp:76
void CleanData()
Converts the User, Item, Value Matrix to User-Item Table.