[mlpack-svn] r15880 - mlpack/trunk/src/mlpack/methods/cf

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Mon Sep 30 18:37:19 EDT 2013


Author: rcurtin
Date: Mon Sep 30 18:37:19 2013
New Revision: 15880

Log:
Remove CalculateAverages() since it is only called once, and inline it into
Query().  The code has been cleaned up a bit and should run faster now because
it does not use a temporary column vector.


Modified:
   mlpack/trunk/src/mlpack/methods/cf/cf.cpp
   mlpack/trunk/src/mlpack/methods/cf/cf.hpp

Modified: mlpack/trunk/src/mlpack/methods/cf/cf.cpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/cf/cf.cpp	(original)
+++ mlpack/trunk/src/mlpack/methods/cf/cf.cpp	Mon Sep 30 18:37:19 2013
@@ -184,33 +184,20 @@
   // neighborhood.
   arma::mat averages = arma::zeros<arma::mat>(rating.n_rows, query.n_cols);
 
-  // Calculate the average values.
-  CalculateAverage(neighborhood, averages);
+  // Iterate over each query user.
+  for (size_t i = 0; i < neighborhood.n_cols; ++i)
+  {
+    // Iterate over each neighbor of the query user.
+    for (size_t j = 0; j < neighborhood.n_rows; ++j)
+      averages.col(i) += rating.col(neighborhood(j, i));
+    // Normalize average.
+    averages.col(i) /= neighborhood.n_rows;
+  }
 
   // Calculate the top recommendations.
   CalculateTopRecommendations(recommendations, averages, users);
 }
 
-void CF::CalculateAverage(arma::Mat<size_t>& neighbourhood,
-                      arma::mat& averages) const
-{
-  Log::Info<<"CalculateAverage"<<endl;
-  //Temprorary Storage for calculating sum
-  arma::Col<double> tmp = arma::zeros<arma::Col<double> >(rating.n_rows,1);
-  size_t j;
-  //Iterating over all users
-  for(size_t i=0;i<neighbourhood.n_cols;i++)
-  {
-    tmp = arma::zeros<arma::Col<double> >(rating.n_rows,1);
-    //Iterating over all neighbours
-    for(j=0;j<neighbourhood.n_rows;j++)
-      tmp += rating.col(neighbourhood(j,i));
-    //Calculating averages
-    averages.col(i) = tmp/j;
-  }
-  //data::Save("averages.csv",averages);
-}
-
 void CF::CalculateTopRecommendations(arma::Mat<size_t>& recommendations,
                                  arma::mat& averages,
                                  arma::Col<size_t>& users) const

Modified: mlpack/trunk/src/mlpack/methods/cf/cf.hpp
==============================================================================
--- mlpack/trunk/src/mlpack/methods/cf/cf.hpp	(original)
+++ mlpack/trunk/src/mlpack/methods/cf/cf.hpp	Mon Sep 30 18:37:19 2013
@@ -204,16 +204,6 @@
   void Query(arma::Mat<size_t>& recommendations,arma::Col<size_t>& users);
 
   /**
-   * Calculates the Average rating users would have given to unrated items based
-   * on their similarity with other users.
-   *
-   * @param neighbourhood Matrix to store user neighbourhood.
-   * @param averages stores the average rating for each item.
-   */
-  void CalculateAverage(arma::Mat<size_t>& neighbourhood,
-                        arma::mat& averages) const;
-
-  /**
    * Calculates the top recommendations given average rating
    * for each user.
    *



More information about the mlpack-svn mailing list