13 #ifndef MLPACK_METHODS_CF_NORMALIZATION_USER_MEAN_NORMALIZATION_HPP 14 #define MLPACK_METHODS_CF_NORMALIZATION_USER_MEAN_NORMALIZATION_HPP 52 const size_t userNum = arma::max(data.row(0)) + 1;
53 userMean = arma::vec(userNum, arma::fill::zeros);
55 arma::Row<size_t> ratingNum(userNum, arma::fill::zeros);
58 data.each_col([&](arma::vec& datapoint)
60 const size_t user = (size_t) datapoint(0);
61 const double rating = datapoint(2);
62 userMean(user) += rating;
68 for (
size_t i = 0; i < userNum; ++i)
70 if (ratingNum(i) != 0)
71 userMean(i) /= ratingNum(i);
74 data.each_col([&](arma::vec& datapoint)
76 const size_t user = (size_t) datapoint(0);
77 datapoint(2) -= userMean(user);
80 if (datapoint(2) == 0)
81 datapoint(2) = std::numeric_limits<double>::min();
93 userMean = arma::vec(cleanedData.n_cols, arma::fill::zeros);
94 arma::Col<size_t> ratingNum(cleanedData.n_cols, arma::fill::zeros);
95 arma::sp_mat::iterator it = cleanedData.begin();
96 arma::sp_mat::iterator it_end = cleanedData.end();
97 for (; it != it_end; ++it)
99 userMean(it.col()) += *it;
100 ratingNum(it.col()) += 1;
102 for (
size_t i = 0; i < userMean.n_elem; ++i)
104 if (ratingNum(i) != 0)
105 userMean(i) /= ratingNum(i);
109 it = cleanedData.begin();
110 for (; it != cleanedData.end(); ++it)
112 double tmp = *it - userMean(it.col());
117 tmp = std::numeric_limits<float>::min();
132 const double rating)
const 134 return rating + userMean(user);
144 arma::vec& predictions)
const 146 for (
size_t i = 0; i < predictions.n_elem; ++i)
148 const size_t user = combinations(0, i);
149 predictions(i) += userMean(user);
156 const arma::vec&
Mean()
const {
return userMean; }
161 template<
typename Archive>
164 ar(CEREAL_NVP(userMean));
void Normalize(arma::mat &data)
Normalize the data by subtracting user mean from each of existing ratings.
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Denormalize(const arma::Mat< size_t > &combinations, arma::vec &predictions) const
Denormalize computed rating by adding user mean.
const arma::vec & Mean() const
Return user mean.
void Normalize(arma::sp_mat &cleanedData)
Normalize the data by subtracting user mean from each of existing rating.
This normalization class performs user mean normalization on raw ratings.
double Denormalize(const size_t user, const size_t, const double rating) const
Denormalize computed rating by adding user mean.
void serialize(Archive &ar, const uint32_t)
Serialization.