14 #ifndef MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_SVDPLUSPLUS_METHOD_HPP 15 #define MLPACK_METHODS_CF_DECOMPOSITION_POLICIES_SVDPLUSPLUS_METHOD_HPP 52 const double alpha = 0.001,
53 const double lambda = 0.1) :
54 maxIterations(maxIterations),
73 void Apply(
const arma::mat& data,
76 const size_t maxIterations,
83 arma::mat implicitDenseData = data.submat(0, 0, 1, data.n_cols - 1);
84 svdpp.
CleanData(implicitDenseData, implicitData, data);
87 svdpp.
Apply(data, implicitDenseData, rank, w, h, p, q, y);
96 double GetRating(
const size_t user,
const size_t item)
const 100 arma::vec userVec(h.n_rows, arma::fill::zeros);
101 arma::sp_mat::const_iterator it = implicitData.begin_col(user);
102 arma::sp_mat::const_iterator it_end = implicitData.end_col(user);
103 size_t implicitCount = 0;
104 for (; it != it_end; ++it)
106 userVec += y.col(it.row());
109 if (implicitCount != 0)
110 userVec /= std::sqrt(implicitCount);
111 userVec += h.col(user);
114 arma::as_scalar(w.row(item) * userVec) + p(item) + q(user);
128 arma::vec userVec(h.n_rows, arma::fill::zeros);
129 arma::sp_mat::const_iterator it = implicitData.begin_col(user);
130 arma::sp_mat::const_iterator it_end = implicitData.end_col(user);
131 size_t implicitCount = 0;
132 for (; it != it_end; ++it)
134 userVec += y.col(it.row());
137 if (implicitCount != 0)
138 userVec /= std::sqrt(implicitCount);
139 userVec += h.col(user);
141 rating = w * userVec + p + q(user);
156 template<
typename NeighborSearchPolicy>
158 const size_t numUsersForSimilarity,
159 arma::Mat<size_t>& neighborhood,
160 arma::mat& similarities)
const 164 arma::mat query(h.n_rows, users.n_elem);
166 for (
size_t i = 0; i < users.n_elem; ++i)
167 query.col(i) = h.col(users(i));
169 NeighborSearchPolicy neighborSearch(h);
170 neighborSearch.Search(
171 query, numUsersForSimilarity, neighborhood, similarities);
175 const arma::mat&
W()
const {
return w; }
177 const arma::mat&
H()
const {
return h; }
179 const arma::vec&
Q()
const {
return q; }
181 const arma::vec&
P()
const {
return p; }
183 const arma::mat&
Y()
const {
return y; }
193 double Alpha()
const {
return alpha; }
205 template<
typename Archive>
208 ar(CEREAL_NVP(maxIterations));
209 ar(CEREAL_NVP(alpha));
210 ar(CEREAL_NVP(lambda));
216 ar(CEREAL_NVP(implicitData));
221 size_t maxIterations;
237 arma::sp_mat implicitData;
SVD++ is a matrix decomposition tenique used in collaborative filtering.
double GetRating(const size_t user, const size_t item) const
Return predicted rating given user ID and item ID.
size_t & MaxIterations()
Modify the number of iterations.
Linear algebra utility functions, generally performed on matrices or vectors.
static void CleanData(const arma::mat &implicitData, arma::sp_mat &cleanedData, const arma::mat &data)
Converts the User, Item matrix of implicit data to Item-User Table.
void GetRatingOfUser(const size_t user, arma::vec &rating) const
Get predicted ratings for a user.
const arma::mat & H() const
Get the User Matrix.
void serialize(Archive &ar, const uint32_t)
Serialization.
void Apply(const arma::mat &data, const arma::sp_mat &, const size_t rank, const size_t maxIterations, const double, const bool)
Apply Collaborative Filtering to the provided data set using the svdplusplus.
The core includes that mlpack expects; standard C++ includes and Armadillo.
double Lambda() const
Get regularization parameter.
const arma::vec & Q() const
Get the User Bias Vector.
const arma::sp_mat & ImplicitData() const
Get Implicit Feedback Data.
size_t MaxIterations() const
Get the number of iterations.
const arma::mat & W() const
Get the Item Matrix.
Implementation of the SVDPlusPlus policy to act as a wrapper when accessing SVDPlusPlus from within C...
const arma::vec & P() const
Get the Item Bias Vector.
double & Alpha()
Modify learning rate.
double Alpha() const
Get learning rate.
void GetNeighborhood(const arma::Col< size_t > &users, const size_t numUsersForSimilarity, arma::Mat< size_t > &neighborhood, arma::mat &similarities) const
Get the neighborhood and corresponding similarities for a set of users.
SVDPlusPlusPolicy(const size_t maxIterations=10, const double alpha=0.001, const double lambda=0.1)
Use SVDPlusPlus method to perform collaborative filtering.
void Apply(const arma::mat &data, const arma::mat &implicitData, const size_t rank, arma::mat &u, arma::mat &v, arma::vec &p, arma::vec &q, arma::mat &y)
Trains the model and obtains user/item matrices, user/item bias, and item implicit matrix...
double & Lambda()
Modify regularization parameter.
const arma::mat & Y() const
Get the Item Implicit Matrix.