mlpack  1.0.12
cf.hpp
Go to the documentation of this file.
1 
17 #ifndef __MLPACK_METHODS_CF_CF_HPP
18 #define __MLPACK_METHODS_CF_CF_HPP
19 
20 #include <mlpack/core.hpp>
25 #include <set>
26 #include <map>
27 #include <iostream>
28 
29 namespace mlpack {
30 namespace cf {
31 
64 template<
65  typename FactorizerType = amf::AMF<amf::SimpleResidueTermination,
68 class CF
69 {
70  public:
81  CF(arma::mat& data,
82  const size_t numUsersForSimilarity = 5,
83  const size_t rank = 0);
84 
86  void NumUsersForSimilarity(const size_t num)
87  {
88  if (num < 1)
89  {
90  Log::Warn << "CF::NumUsersForSimilarity(): invalid value (< 1) "
91  "ignored." << std::endl;
92  return;
93  }
94  this->numUsersForSimilarity = num;
95  }
96 
98  size_t NumUsersForSimilarity() const
99  {
100  return numUsersForSimilarity;
101  }
102 
104  void Rank(const size_t rankValue)
105  {
106  this->rank = rankValue;
107  }
108 
110  size_t Rank() const
111  {
112  return rank;
113  }
114 
116  void Factorizer(const FactorizerType& f)
117  {
118  this->factorizer = f;
119  }
120 
122  const arma::mat& W() const { return w; }
124  const arma::mat& H() const { return h; }
126  const arma::mat& Rating() const { return rating; }
128  const arma::mat& Data() const { return data; }
130  const arma::sp_mat& CleanedData() const { return cleanedData; }
131 
138  void GetRecommendations(const size_t numRecs,
139  arma::Mat<size_t>& recommendations);
140 
148  void GetRecommendations(const size_t numRecs,
149  arma::Mat<size_t>& recommendations,
150  arma::Col<size_t>& users);
151 
155  std::string ToString() const;
156 
157  private:
159  arma::mat data;
163  size_t rank;
165  FactorizerType factorizer;
167  arma::mat w;
169  arma::mat h;
171  arma::mat rating;
173  arma::sp_mat cleanedData;
175  void CleanData();
176 
186  void InsertNeighbor(const size_t queryIndex,
187  const size_t pos,
188  const size_t neighbor,
189  const double value,
190  arma::Mat<size_t>& recommendations,
191  arma::mat& values) const;
192 
193 }; // class CF
194 
195 }; // namespace cf
196 }; // namespace mlpack
197 
198 //Include implementation
199 #include "cf_impl.hpp"
200 
201 #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:71
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: load.hpp:23
arma::mat rating
Rating matrix.
Definition: cf.hpp:171
FactorizerType factorizer
Instantiated factorizer object.
Definition: cf.hpp:165
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:128
size_t rank
Rank used for matrix factorization.
Definition: cf.hpp:163
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:104
const arma::mat & Rating() const
Get the Rating Matrix.
Definition: cf.hpp:126
This class implements a method titled 'Alternating Least Squares' described in the paper 'Positive Ma...
Definition: nmf_als.hpp:30
size_t numUsersForSimilarity
Number of users for similarity.
Definition: cf.hpp:161
void NumUsersForSimilarity(const size_t num)
Sets number of users for calculating similarity.
Definition: cf.hpp:86
size_t NumUsersForSimilarity() const
Gets number of users for calculating similarity.
Definition: cf.hpp:98
const arma::mat & H() const
Get the Item Matrix.
Definition: cf.hpp:124
const arma::mat & W() const
Get the User Matrix.
Definition: cf.hpp:122
arma::mat w
User matrix.
Definition: cf.hpp:167
void Factorizer(const FactorizerType &f)
Sets factorizer for NMF.
Definition: cf.hpp:116
arma::mat data
Initial data matrix.
Definition: cf.hpp:159
arma::mat h
Item matrix.
Definition: cf.hpp:169
size_t Rank() const
Gets rank parameter for matrix factorization.
Definition: cf.hpp:110
const arma::sp_mat & CleanedData() const
Get the cleaned data matrix.
Definition: cf.hpp:130
static util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:84
arma::sp_mat cleanedData
Cleaned data matrix.
Definition: cf.hpp:173
This class implements Collaborative Filtering (CF).
Definition: cf.hpp:68
void CleanData()
Converts the User, Item, Value Matrix to User-Item Table.