[mlpack-svn] r15864 - 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 12:34:44 EDT 2013
Author: rcurtin
Date: Mon Sep 30 12:34:43 2013
New Revision: 15864
Log:
Fix comment headers for doxygen, remove trailing spaces, and use NMF instead of
ALS because it now supports sparse matrices.
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 12:34:43 2013
@@ -1,17 +1,18 @@
/**
- * @file cf.hpp
+ * @file cf.cpp
* @author Mudit Raj Gupta
*
* Collaborative Filtering.
- *
- * Implementation of CF class to perform Collaborative Filtering on the
+ *
+ * Implementation of CF class to perform Collaborative Filtering on the
* specified data set.
*
- */
-
+ */
#include "cf.hpp"
+#include <mlpack/methods/nmf/nmf.hpp>
+#include <mlpack/methods/nmf/als_update_rules.hpp>
-using namespace mlpack::als;
+using namespace mlpack::nmf;
using namespace std;
namespace mlpack {
@@ -24,7 +25,7 @@
data(data)
{
Log::Info<<"Constructor (param: input data, default: numRecs;neighbourhood)"<<endl;
- this->numRecs = 5;
+ this->numRecs = 5;
this->numUsersForSimilarity = 5;
}
@@ -40,7 +41,7 @@
this->numRecs = 5;
}
else
- this->numRecs = numRecs;
+ this->numRecs = numRecs;
this->numUsersForSimilarity = 5;
}
@@ -57,7 +58,7 @@
this->numRecs = 5;
}
else
- this->numRecs = numRecs;
+ this->numRecs = numRecs;
// Validate neighbourhood size.
if (numUsersForSimilarity < 1)
{
@@ -70,7 +71,7 @@
this->numUsersForSimilarity = numUsersForSimilarity;
}
-void CF::GetRecommendations(arma::Mat<size_t>& recommendations,
+void CF::GetRecommendations(arma::Mat<size_t>& recommendations,
arma::Col<size_t>& users)
{
Log::Info<<"GetRecommendations (param: recommendations,users)"<<endl;
@@ -78,28 +79,28 @@
//Operations Independent of the query
CalculateApproximateRatings();
//Query Dependent Operations
- Query(recommendations,users);
+ Query(recommendations,users);
}
-void CF::CalculateApproximateRatings()
+void CF::CalculateApproximateRatings()
{
Log::Info<<"CalculatineApproximateRating"<<endl;
//Build the initial rating tables with missing values
//if(cleanedData.n_rows==0)
CleanData();
- //Decompose the size_tiial table size_to user and item
+ //Decompose the size_tiial table size_to user and item
Decompose();
//Generate new table by multiplying approximate values
GenerateRating();
}
-void CF::GetRecommendations(arma::Mat<size_t>& recommendations)
+void CF::GetRecommendations(arma::Mat<size_t>& recommendations)
{
Log::Info<<"GetRecommendations (param: recommendations)"<<endl;
//Build the initial rating tables with missing values
CleanData();
//Used to save user IDs
- arma::Col<size_t> users =
+ arma::Col<size_t> users =
arma::zeros<arma::Col<size_t> >(cleanedData.n_cols,1);
//Getting all user IDs
for (size_t i=0;i<cleanedData.n_cols;i++)
@@ -109,21 +110,21 @@
}
void CF::GetRecommendations(arma::Mat<size_t>& recommendations,
- arma::Col<size_t>& users,size_t num)
+ arma::Col<size_t>& users,size_t num)
{
- //Setting Number of Recommendations
- NumRecs(num);
+ //Setting Number of Recommendations
+ NumRecs(num);
//Calling Base Function for Recommendations
GetRecommendations(recommendations,users);
}
void CF::GetRecommendations(arma::Mat<size_t>& recommendations,
- arma::Col<size_t>& users,size_t num,size_t s)
+ arma::Col<size_t>& users,size_t num,size_t s)
{
- //Setting number of users that should be used for calculating
+ //Setting number of users that should be used for calculating
//neighbours
NumUsersForSimilarity(s);
- //Setting Number of Recommendations
+ //Setting Number of Recommendations
NumRecs(num);
//Calling Base Function for Recommendations
GetRecommendations(recommendations,users,num);
@@ -132,7 +133,7 @@
void CF::CleanData()
{
Log::Info<<"CleanData";
- //Temporarily stores max user id
+ //Temporarily stores max user id
double maxUserID;
//Temporarily stores max item id
double maxItemID;
@@ -148,7 +149,7 @@
}
//Temporarily stores sparcely populated rating matrix
arma::sp_mat tmp((size_t)maxItemID,(size_t)maxUserID);
- //Temporarily stores mask matrix
+ //Temporarily stores mask matrix
arma::mat lMask = arma::ones<arma::mat>((size_t)maxItemID,
(size_t)maxUserID);
//Calculates the initial User-Item table
@@ -165,29 +166,29 @@
//data::Save("mask.csv",mask);
}
-void CF::Decompose()
-{
+void CF::Decompose()
+{
Log::Info<<"Decompose"<<endl;
size_t rank = 2;
//Presenltly only ALS is supported as an Optimizer
//Should be converted to a template
- ALS<RandomInitialization, WAlternatingLeastSquaresRule,
+ NMF<RandomInitialization, WAlternatingLeastSquaresRule,
HAlternatingLeastSquaresRule> als(10000, 1e-5);
als.Apply(cleanedData,rank,w,h);
//data::Save("w.csv",w);
//data::Save("h.csv",h);
}
-void CF::GenerateRating()
+void CF::GenerateRating()
{
Log::Info<<"GenerateRatings"<<endl;
- //Calculating approximate rating
+ //Calculating approximate rating
rating = w*h;
//data::Save("rating.csv",rating);
}
void CF::Query(arma::Mat<size_t>& recommendations,
- arma::Col<size_t>& users)
+ arma::Col<size_t>& users)
{
Log::Info<<"Query"<<endl;
//Temproraily stores feature vector of queried users
@@ -197,8 +198,8 @@
//Temporary storage for neighbourhood of the queried users
arma::Mat<size_t> neighbourhood;
//Calculates the neighbourhood of the queried users
- GetNeighbourhood(query,neighbourhood);
- //Temporary storage for storing the average rating for each
+ GetNeighbourhood(query,neighbourhood);
+ //Temporary storage for storing the average rating for each
//user in their neighbourhood
arma::mat averages = arma::zeros<arma::mat>(rating.n_rows,query.n_cols);
//Calculates the average values
@@ -210,15 +211,15 @@
void CF::CreateQuery(arma::mat& query,arma::Col<size_t>& users) const
{
Log::Info<<"CreateQuery"<<endl;
- //Selecting feature vectors of queried users
+ //Selecting feature vectors of queried users
for(size_t i=0;i<users.n_rows;i++)
for(size_t j=0;j<rating.col(i).n_rows;j++)
query(j,i) = rating(j,users(i)-1);
//data::Save("query.csv",query);
}
-void CF::GetNeighbourhood(arma::mat& query,
- arma::Mat<size_t>& neighbourhood)
+void CF::GetNeighbourhood(arma::mat& query,
+ arma::Mat<size_t>& neighbourhood)
{
Log::Info<<"GetNeighbourhood"<<endl;
if(numUsersForSimilarity>rating.n_cols)
@@ -234,12 +235,12 @@
//Temproraily storing distance between neighbours
arma::mat resultingDistances;
//Building neighbourhood
- a.Search(numUsersForSimilarity, neighbourhood,
- resultingDistances);
+ a.Search(numUsersForSimilarity, neighbourhood,
+ resultingDistances);
//data::Save("neighbourhood.csv",neighbourhood);
}
-void CF::CalculateAverage(arma::Mat<size_t>& neighbourhood,
+void CF::CalculateAverage(arma::Mat<size_t>& neighbourhood,
arma::mat& averages) const
{
Log::Info<<"CalculateAverage"<<endl;
@@ -252,14 +253,14 @@
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));
+ tmp += rating.col(neighbourhood(j,i));
//Calculating averages
- averages.col(i) = tmp/j;
+ averages.col(i) = tmp/j;
}
//data::Save("averages.csv",averages);
}
-void CF::CalculateTopRecommendations(arma::Mat<size_t>& recommendations,
+void CF::CalculateTopRecommendations(arma::Mat<size_t>& recommendations,
arma::mat& averages,
arma::Col<size_t>& users) const
{
@@ -280,10 +281,10 @@
size_t count;
//Iterate for all users
for(size_t i=0;i<users.n_rows;i++)
- {
+ {
count=0;
//Dot product between average rating and mask to dilute the ratings
- // of the items that user i has already rated
+ // of the items that user i has already rated
tmp = averages.col(users(i)-1) % mask.col(users(i)-1);
//Mapping Rating to Items
for(size_t j=0;j<tmp.n_rows;j++)
@@ -291,7 +292,7 @@
tmpMap.insert(std::pair<double,size_t>(tmp(j),j+1));
//Iterating over Item-Rating Map
for(iter=tmpMap.rbegin();iter!=tmpMap.rend();++iter)
- {
+ {
//Saving recommendations to the recommendations table
rec(count,i) = (size_t)iter->second;
count++;
@@ -300,12 +301,12 @@
break;
}
//Removing the items from the map
- //note: Item 0 is just to maintain the consistency and it
+ //note: Item 0 is just to maintain the consistency and it
//represents not recommendations were available
for(it=tmpMap.begin();it!=tmpMap.end();++it)
tmpMap.erase(it);
}
- //Saving to recommendations
+ //Saving to recommendations
recommendations = rec;
}
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 12:34:43 2013
@@ -2,95 +2,95 @@
* @file cf.hpp
* @author Mudit Raj Gupta
*
- * Collaborative Filtering.
- *
- * Defines the CF class to perform Collaborative Filtering on the
- * specified data set.
+ * Collaborative filtering.
*
+ * Defines the CF class to perform collaborative filtering on the specified data
+ * set using alternating least squares (ALS).
*/
-
#ifndef __MLPACK_METHODS_CF_CF_HPP
#define __MLPACK_METHODS_CF_CF_HPP
#include <mlpack/core.hpp>
-#include <mlpack/core/optimizers/als/als.hpp>
#include <mlpack/methods/neighbor_search/neighbor_search.hpp>
#include <set>
#include <map>
#include <iostream>
namespace mlpack {
-namespace cf /** Collaborative Filtering. */{
+namespace cf /** Collaborative filtering. */{
/**
- * This class implements Collaborative Filtering (CF). This
- * implementation presently supports Alternating Least Squares
- * for collaborative filtering.
- *
- * The template parameters can (optionally) be supplied are: the algorithm
- * used for CF and the neighbourhood search for user similarity.
+ * This class implements Collaborative Filtering (CF). This implementation
+ * presently supports Alternating Least Squares (ALS) for collaborative
+ * filtering.
*
* A simple example of how to run Collaborative Filtering is shown below.
*
* @code
- * extern arma::mat data; // (user,item,rating) table
+ * extern arma::mat data; // (user, item, rating) table
* extern arma::Col<size_t> users; // users seeking recommendations
* arma::mat recommendations; // Recommendations
- * size_t numRecommendations = 10;
+ * size_t numRecommendations = 10;
*
* CF<> cf(data); // Default options.
*
- * //Default number of Recommendations for all users
+ * // Generate the default number of recommendations for all users.
* cf.GenerateRecommendations(recommendations);
*
- * //Default number of Recommendations for specified users
+ * // Generate the default number of recommendations for specified users.
* cf.GenerateRecommendations(recommendations, users);
*
- * //10 Recommendations for specified users
+ * // Generate 10 recommendations for specified users.
* cf.GenerateRecommendations(recommendations, users, numRecommendations);
*
* @endcode
- *
+ *
+ * The data matrix is a (user, item, rating) table. Each column in the matrix
+ * should have three rows. The first represents the user; the second represents
+ * the item; and the third represents the rating. The user and item, while they
+ * are in a matrix that holds doubles, should hold integer (or size_t) values.
*/
-
class CF
-{
+{
public:
-/**
- * Create a CF object and (optionally) set the parameters which CF
- * will be run with.
- *
- * @param data Initial User,Item,Rating Matrix
- * @param numRecs Number of Recommendations for each user.
- * @param numUsersForSimilarity Size of the neighbourhood.
- */
+ /**
+ * Create a CF object and (optionally) set the parameters with which
+ * collaborative filtering will be run.
+ *
+ * @param data Initial (user,item,rating) matrix.
+ * @param numRecs Desired number of recommendations for each user.
+ * @param numUsersForSimilarity Size of the neighborhood.
+ */
CF(const size_t numRecs,const size_t numUsersForSimilarity,
arma::mat& data);
-/**
- * Create a CF object and (optionally) set the parameters which CF
- * will be run with.
- *
- * @param data Initial User,Item,Rating Matrix
- * @param numRecs Number of Recommendations for each user.
- */
+
+ /**
+ * Create a CF object and (optionally) set the parameters which CF
+ * will be run with.
+ *
+ * @param data Initial User,Item,Rating Matrix
+ * @param numRecs Number of Recommendations for each user.
+ */
CF(const size_t numRecs, arma::mat& data);
-/**
- * Create a CF object and (optionally) set the parameters which CF
- * will be run with.
- *
- * @param data Initial User,Item,Rating Matrix
- */
+
+ /**
+ * Create a CF object and (optionally) set the parameters which CF
+ * will be run with.
+ *
+ * @param data Initial User,Item,Rating Matrix
+ */
CF(arma::mat& data);
+
//! Sets number of Recommendations.
- void NumRecs(size_t recs)
- {
+ void NumRecs(size_t recs)
+ {
if (recs < 1)
{
Log::Warn << "CF::NumRecs(): invalid value (< 1) "
"ignored." << std::endl;
return;
}
- this->numRecs = recs;
+ this->numRecs = recs;
}
//! Sets data
@@ -98,36 +98,37 @@
{
data = d;
}
-
+
//! Gets data
- arma::mat Data()
+ arma::mat Data()
{
return data;
}
//! Gets numRecs
size_t NumRecs()
- {
+ {
return numRecs;
}
//! Sets number of user for calculating similarity.
- void NumUsersForSimilarity(size_t num)
- {
+ void NumUsersForSimilarity(size_t num)
+ {
if (num < 1)
{
Log::Warn << "CF::NumUsersForSimilarity(): invalid value (< 1) "
"ignored." << std::endl;
return;
}
- this->numUsersForSimilarity = num;
+ this->numUsersForSimilarity = num;
}
+
//! Gets number of users for calculating similarity/
size_t NumUsersForSimilarity()
{
return numUsersForSimilarity;
}
-
+
//! Get the User Matrix.
const arma::mat& W() const { return w; }
//! Get the Item Matrix.
@@ -135,43 +136,43 @@
//! Get the Rating Matrix.
const arma::mat& Rating() const { return rating; }
-/*
- * Generates default number of recommendations for all users.
- *
- * @param recommendations Matrix to save recommendations
- */
+ /**
+ * Generates default number of recommendations for all users.
+ *
+ * @param recommendations Matrix to save recommendations
+ */
void GetRecommendations(arma::Mat<size_t>& recommendations);
-/*
- * Generates default number of recommendations for specified users.
- *
- * @param recommendations Matrix to save recommendations
- * @param users Users for which recommendations are to be generated
- */
- void GetRecommendations(arma::Mat<size_t>& recommendations,
+ /**
+ * Generates default number of recommendations for specified users.
+ *
+ * @param recommendations Matrix to save recommendations
+ * @param users Users for which recommendations are to be generated
+ */
+ void GetRecommendations(arma::Mat<size_t>& recommendations,
arma::Col<size_t>& users);
-/*
- * Generates a fixed number of recommendations for specified users.
- *
- * @param recommendations Matrix to save recommendations
- * @param users Users for which recommendations are to be generated
- * @param num Number of Recommendations
- */
- void GetRecommendations(arma::Mat<size_t>& recommendations,
+ /**
+ * Generates a fixed number of recommendations for specified users.
+ *
+ * @param recommendations Matrix to save recommendations
+ * @param users Users for which recommendations are to be generated
+ * @param num Number of Recommendations
+ */
+ void GetRecommendations(arma::Mat<size_t>& recommendations,
arma::Col<size_t>& users, size_t num);
-/*
- * Generates a fixed number of recommendations for specified users.
- *
- * @param recommendations Matrix to save recommendations
- * @param users Users for which recommendations are to be generated
- * @param num Number of Recommendations
- * @param neighbours Number of user to be considered while calculating
- * the neighbourhood
- */
- void GetRecommendations(arma::Mat<size_t>& recommendations,
- arma::Col<size_t>& users, size_t num,
+ /**
+ * Generates a fixed number of recommendations for specified users.
+ *
+ * @param recommendations Matrix to save recommendations
+ * @param users Users for which recommendations are to be generated
+ * @param num Number of Recommendations
+ * @param neighbours Number of user to be considered while calculating
+ * the neighbourhood
+ */
+ void GetRecommendations(arma::Mat<size_t>& recommendations,
+ arma::Col<size_t>& users, size_t num,
size_t neighbours);
private:
@@ -199,46 +200,51 @@
void Decompose();
//!Create ratings from user and item matrices
void GenerateRating();
-/*
- * Queries the obtained rating matrix.
- *
- * @param recommendations Matrix to save recommendations
- * @param users Users for which recommendations are to be generated
- */
+
+ /**
+ * Queries the obtained rating matrix.
+ *
+ * @param recommendations Matrix to save recommendations
+ * @param users Users for which recommendations are to be generated
+ */
void Query(arma::Mat<size_t>& recommendations,arma::Col<size_t>& users);
-/*
- * Selects item preferences of the users
- *
- * @param query Matrix to store the item preference of the user.
- * @param users Users for which recommendations are to be generated
- */
+
+ /**
+ * Selects item preferences of the users
+ *
+ * @param query Matrix to store the item preference of the user.
+ * @param users Users for which recommendations are to be generated
+ */
void CreateQuery(arma::mat& query,arma::Col<size_t>& users) const;
-/*
- * Generates the neighbourhood of users.
- *
- * @param query Matrix to store the item preference of the user.
- * @param modifiedRating Matrix to store the Modified Matix.
- * @param neighbourhood Matrix to store user neighbourhood.
- */
+
+ /**
+ * Generates the neighbourhood of users.
+ *
+ * @param query Matrix to store the item preference of the user.
+ * @param modifiedRating Matrix to store the Modified Matix.
+ * @param neighbourhood Matrix to store user neighbourhood.
+ */
void GetNeighbourhood(arma::mat& query,
arma::Mat<size_t>& neighbourhood);
-/*
- * 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,
+
+ /**
+ * 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.
- *
- * @param neighbourhood Matrix to store user neighbourhood.
- * @param averages stores the average rating for each item.
- */
- void CalculateTopRecommendations(arma::Mat<size_t>& recommendations,
+
+ /**
+ * Calculates the top recommendations given average rating
+ * for each user.
+ *
+ * @param neighbourhood Matrix to store user neighbourhood.
+ * @param averages stores the average rating for each item.
+ */
+ void CalculateTopRecommendations(arma::Mat<size_t>& recommendations,
arma::mat& averages,
arma::Col<size_t>& users) const;
More information about the mlpack-svn
mailing list