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