overall_mean_normalization.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_METHODS_CF_NORMALIZATION_OVERALL_MEAN_NORMALIZATION_HPP
14 #define MLPACK_METHODS_CF_NORMALIZATION_OVERALL_MEAN_NORMALIZATION_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 
18 namespace mlpack {
19 namespace cf {
20 
40 {
41  public:
42  // Empty constructor.
43  OverallMeanNormalization() : mean(0) { }
44 
50  void Normalize(arma::mat& data)
51  {
52  mean = arma::mean(data.row(2));
53  data.row(2) -= mean;
54  // The algorithm omits rating of zero. If normalized rating equals zero,
55  // it is set to the smallest positive double value.
56  data.row(2).for_each([](double& x)
57  {
58  if (x == 0)
59  x = std::numeric_limits<double>::min();
60  });
61  }
62 
68  void Normalize(arma::sp_mat& cleanedData)
69  {
70  // Caculate mean of all non zero ratings.
71  if (cleanedData.n_nonzero != 0)
72  {
73  mean = arma::accu(cleanedData) / cleanedData.n_nonzero;
74  // Subtract mean from all non zero ratings.
75  arma::sp_mat::iterator it = cleanedData.begin();
76  arma::sp_mat::iterator it_end = cleanedData.end();
77  for (; it != it_end; ++it)
78  {
79  *it = *it - mean;
80  // The algorithm omits rating of zero. If normalized rating equals zero,
81  // it is set to the smallest positive double value.
82  if (*it == 0)
83  *it = std::numeric_limits<double>::min();
84  }
85  }
86  else
87  {
88  mean = 0;
89  // cleanedData remains the same when mean == 0.
90  }
91  }
92 
100  double Denormalize(const size_t /* user */,
101  const size_t /* item */,
102  const double rating) const
103  {
104  return rating + mean;
105  }
106 
113  void Denormalize(const arma::Mat<size_t>& /* combinations */,
114  arma::vec& predictions) const
115  {
116  predictions += mean;
117  }
118 
122  double Mean() const
123  {
124  return mean;
125  }
126 
130  template<typename Archive>
131  void serialize(Archive& ar, const unsigned int /* version */)
132  {
133  ar & BOOST_SERIALIZATION_NVP(mean);
134  }
135 
136  private:
138  double mean;
139 };
140 
141 } // namespace cf
142 } // namespace mlpack
143 
144 #endif
void serialize(Archive &ar, const unsigned int)
Serialization.
double Denormalize(const size_t, const size_t, const double rating) const
Denormalize computed rating by adding mean.
.hpp
Definition: add_to_po.hpp:21
void Normalize(arma::sp_mat &cleanedData)
Normalize the data by subtracting the mean of all existing ratings.
The core includes that mlpack expects; standard C++ includes and Armadillo.
void Denormalize(const arma::Mat< size_t > &, arma::vec &predictions) const
Denormalize computed rating by adding mean.
This normalization class performs overall mean normalization on raw ratings.
void Normalize(arma::mat &data)
Normalize the data by subtracting the mean of all existing ratings.