diff --git a/src/mlpack/methods/cf/normalization/z_score_normalization.hpp b/src/mlpack/methods/cf/normalization/z_score_normalization.hpp index e65841d77a..9e0d7f43d9 100644 --- a/src/mlpack/methods/cf/normalization/z_score_normalization.hpp +++ b/src/mlpack/methods/cf/normalization/z_score_normalization.hpp @@ -88,15 +88,20 @@ class ZScoreNormalization } // Subtract mean from existing rating and divide it by stddev. + // TODO: consider using spmat::transform() instead of spmat iterators + // TODO: http://arma.sourceforge.net/docs.html#transform arma::sp_mat::iterator it = cleanedData.begin(); arma::sp_mat::iterator it_end = cleanedData.end(); for (; it != it_end; ++it) { - *it = (*it - mean) / stddev; + double tmp = (*it - mean) / stddev; + // The algorithm omits rating of zero. If normalized rating equals zero, // it is set to the smallest positive double value. - if (*it == 0) - *it = std::numeric_limits::min(); + if (tmp == 0) + tmp = std::numeric_limits::min(); + + *it = tmp; } }