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 float 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  double tmp = *it - mean;
80 
81  // The algorithm omits rating of zero. If normalized rating equals zero,
82  // it is set to the smallest positive float value.
83  if (tmp == 0)
84  tmp = std::numeric_limits<float>::min();
85 
86  *it = tmp;
87  }
88  }
89  else
90  {
91  mean = 0;
92  // cleanedData remains the same when mean == 0.
93  }
94  }
95 
103  double Denormalize(const size_t /* user */,
104  const size_t /* item */,
105  const double rating) const
106  {
107  return rating + mean;
108  }
109 
116  void Denormalize(const arma::Mat<size_t>& /* combinations */,
117  arma::vec& predictions) const
118  {
119  predictions += mean;
120  }
121 
125  double Mean() const
126  {
127  return mean;
128  }
129 
133  template<typename Archive>
134  void serialize(Archive& ar, const uint32_t /* version */)
135  {
136  ar(CEREAL_NVP(mean));
137  }
138 
139  private:
141  double mean;
142 };
143 
144 } // namespace cf
145 } // namespace mlpack
146 
147 #endif
double Denormalize(const size_t, const size_t, const double rating) const
Denormalize computed rating by adding mean.
Linear algebra utility functions, generally performed on matrices or vectors.
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.
void serialize(Archive &ar, const uint32_t)
Serialization.