From 1a89e795741f21fd9acbdd78382ac017fb40aead Mon Sep 17 00:00:00 2001 From: conrad Date: Thu, 29 Aug 2019 00:41:49 +1000 Subject: [PATCH 1/4] avoid writing zero via sparse matrix iterator --- .../methods/cf/normalization/z_score_normalization.hpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/mlpack/methods/cf/normalization/z_score_normalization.hpp b/src/mlpack/methods/cf/normalization/z_score_normalization.hpp index e65841d77a..bd3f7bd23a 100644 --- a/src/mlpack/methods/cf/normalization/z_score_normalization.hpp +++ b/src/mlpack/methods/cf/normalization/z_score_normalization.hpp @@ -92,11 +92,14 @@ class ZScoreNormalization 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; } } From 23204880a328027b456d68646115d47db99ca84e Mon Sep 17 00:00:00 2001 From: conrad Date: Thu, 29 Aug 2019 00:44:49 +1000 Subject: [PATCH 2/4] added TODO to consider using .transform() instead of iterators --- src/mlpack/methods/cf/normalization/z_score_normalization.hpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/mlpack/methods/cf/normalization/z_score_normalization.hpp b/src/mlpack/methods/cf/normalization/z_score_normalization.hpp index bd3f7bd23a..9e0d7f43d9 100644 --- a/src/mlpack/methods/cf/normalization/z_score_normalization.hpp +++ b/src/mlpack/methods/cf/normalization/z_score_normalization.hpp @@ -88,6 +88,8 @@ 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) From 0c7e9decc8b5ae99b3648f2554d59d0bedc187c3 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Thu, 29 Aug 2019 18:14:49 -0400 Subject: [PATCH 3/4] Fix style issues. --- src/mlpack/methods/cf/normalization/z_score_normalization.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mlpack/methods/cf/normalization/z_score_normalization.hpp b/src/mlpack/methods/cf/normalization/z_score_normalization.hpp index 9e0d7f43d9..7296e2de6e 100644 --- a/src/mlpack/methods/cf/normalization/z_score_normalization.hpp +++ b/src/mlpack/methods/cf/normalization/z_score_normalization.hpp @@ -95,12 +95,12 @@ class ZScoreNormalization for (; it != it_end; ++it) { 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 (tmp == 0) tmp = std::numeric_limits::min(); - + *it = tmp; } } From 70c531dc2d823f90bec55077eecf5b9dee9f59c3 Mon Sep 17 00:00:00 2001 From: Ryan Curtin Date: Thu, 29 Aug 2019 18:15:16 -0400 Subject: [PATCH 4/4] Update history. --- HISTORY.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/HISTORY.md b/HISTORY.md index 183f42886c..a2e38c1056 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -33,6 +33,9 @@ * Improve KDE pruning by reclaiming not used error tolerance (#1954, #1984). + * Optimizations for sparse matrix accesses in z-score normalization for CF + (#1989). + ### mlpack 3.1.1 ###### 2019-05-26 * Fix random forest bug for numerical-only data (#1887).