z_score_normalization.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_METHODS_CF_NORMALIZATION_Z_SCORE_NORMALIZATION_HPP
13 #define MLPACK_METHODS_CF_NORMALIZATION_Z_SCORE_NORMALIZATION_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 
17 namespace mlpack {
18 namespace cf {
19 
39 {
40  public:
41  // Empty constructor.
42  ZScoreNormalization() : mean(0), stddev(1) { }
43 
49  void Normalize(arma::mat& data)
50  {
51  mean = arma::mean(data.row(2));
52  stddev = arma::stddev(data.row(2));
53 
54  if (std::fabs(stddev) < 1e-14)
55  {
56  Log::Fatal << "Standard deviation of all existing ratings is 0! "
57  << "This may indicate that all existing ratings are the same."
58  << std::endl;
59  }
60 
61  data.row(2) = (data.row(2) - mean) / stddev;
62  // The algorithm omits rating of zero. If normalized rating equals zero,
63  // it is set to the smallest positive double value.
64  data.row(2).for_each([](double& x)
65  {
66  if (x == 0)
67  x = std::numeric_limits<double>::min();
68  });
69  }
70 
76  void Normalize(arma::sp_mat& cleanedData)
77  {
78  // Caculate mean and stdev of all non zero ratings.
79  arma::vec ratings = arma::nonzeros(cleanedData);
80  mean = arma::mean(ratings);
81  stddev = arma::stddev(ratings);
82 
83  if (std::fabs(stddev) < 1e-14)
84  {
85  Log::Fatal << "Standard deviation of all existing ratings is 0! "
86  << "This may indicate that all existing ratings are the same."
87  << std::endl;
88  }
89 
90  // Subtract mean from existing rating and divide it by stddev.
91  arma::sp_mat::iterator it = cleanedData.begin();
92  arma::sp_mat::iterator it_end = cleanedData.end();
93  for (; it != it_end; ++it)
94  {
95  *it = (*it - mean) / stddev;
96  // The algorithm omits rating of zero. If normalized rating equals zero,
97  // it is set to the smallest positive double value.
98  if (*it == 0)
99  *it = std::numeric_limits<double>::min();
100  }
101  }
102 
110  double Denormalize(const size_t /* user */,
111  const size_t /* item */,
112  const double rating) const
113  {
114  return rating * stddev + mean;
115  }
116 
123  void Denormalize(const arma::Mat<size_t>& /* combinations */,
124  arma::vec& predictions) const
125  {
126  predictions = predictions * stddev + mean;
127  }
128 
132  double Mean() const
133  {
134  return mean;
135  }
136 
140  double Stddev() const
141  {
142  return stddev;
143  }
144 
148  template<typename Archive>
149  void serialize(Archive& ar, const unsigned int /* version */)
150  {
151  ar & BOOST_SERIALIZATION_NVP(mean);
152  ar & BOOST_SERIALIZATION_NVP(stddev);
153  }
154 
155  private:
157  double mean;
159  double stddev;
160 };
161 
162 } // namespace cf
163 } // namespace mlpack
164 
165 #endif
void Denormalize(const arma::Mat< size_t > &, arma::vec &predictions) const
Denormalize computed rating by adding mean and multiplying stddev.
.hpp
Definition: add_to_po.hpp:21
double Stddev() const
Return stddev.
The core includes that mlpack expects; standard C++ includes and Armadillo.
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
void Normalize(arma::mat &data)
Normalize the data to zero mean and one standard deviation.
double Denormalize(const size_t, const size_t, const double rating) const
Denormalize computed rating by adding mean and multiplying stddev.
double Mean() const
Return mean.
void Normalize(arma::sp_mat &cleanedData)
Normalize the data to zero mean and one standard deviation.
void serialize(Archive &ar, const unsigned int)
Serialization.
This normalization class performs z-score normalization on raw ratings.