/usr/include/mlpack/methods/cf/cf.hpp is in libmlpack-dev 1.0.10-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 | /**
* @file cf.hpp
* @author Mudit Raj Gupta
*
* Collaborative filtering.
*
* Defines the CF class to perform collaborative filtering on the specified data
* set using alternating least squares (ALS).
*
* This file is part of MLPACK 1.0.10.
*
* MLPACK is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* MLPACK is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
* details (LICENSE.txt).
*
* You should have received a copy of the GNU General Public License along with
* MLPACK. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef __MLPACK_METHODS_CF_CF_HPP
#define __MLPACK_METHODS_CF_CF_HPP
#include <mlpack/core.hpp>
#include <mlpack/methods/neighbor_search/neighbor_search.hpp>
#include <mlpack/methods/amf/amf.hpp>
#include <mlpack/methods/amf/update_rules/nmf_als.hpp>
#include <mlpack/methods/amf/termination_policies/simple_residue_termination.hpp>
#include <set>
#include <map>
#include <iostream>
namespace mlpack {
namespace cf /** Collaborative filtering. */{
/**
* 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::Col<size_t> users; // users seeking recommendations
* arma::Mat<size_t> recommendations; // Recommendations
*
* CF<> cf(data); // Default options.
*
* // Generate 10 recommendations for all users.
* cf.GetRecommendations(10, recommendations);
*
* // Generate 10 recommendations for specified users.
* cf.GetRecommendations(10, recommendations, users);
*
* @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.
* The user and item indices are assumed to start at 0.
*
* @tparam FactorizerType The type of matrix factorization to use to decompose
* the rating matrix (a W and H matrix). This must implement the method
* Apply(arma::sp_mat& data, size_t rank, arma::mat& W, arma::mat& H).
*/
template<
typename FactorizerType = amf::AMF<amf::SimpleResidueTermination,
amf::RandomInitialization,
amf::NMFALSUpdate> >
class CF
{
public:
/**
* Initialize the CF object. Store a reference to the data that we
* will be using. There are parameters that can be set; default values
* are provided for each of them. If the rank is left unset (or is set to 0),
* a simple density-based heuristic will be used to choose a rank.
*
* @param data Initial (user, item, rating) matrix.
* @param numUsersForSimilarity Size of the neighborhood.
* @param rank Rank parameter for matrix factorization.
*/
CF(arma::mat& data,
const size_t numUsersForSimilarity = 5,
const size_t rank = 0);
//! Sets number of users for calculating similarity.
void NumUsersForSimilarity(const size_t num)
{
if (num < 1)
{
Log::Warn << "CF::NumUsersForSimilarity(): invalid value (< 1) "
"ignored." << std::endl;
return;
}
this->numUsersForSimilarity = num;
}
//! Gets number of users for calculating similarity.
size_t NumUsersForSimilarity() const
{
return numUsersForSimilarity;
}
//! Sets rank parameter for matrix factorization.
void Rank(const size_t rankValue)
{
this->rank = rankValue;
}
//! Gets rank parameter for matrix factorization.
size_t Rank() const
{
return rank;
}
//! Sets factorizer for NMF
void Factorizer(const FactorizerType& f)
{
this->factorizer = f;
}
//! Get the User Matrix.
const arma::mat& W() const { return w; }
//! Get the Item Matrix.
const arma::mat& H() const { return h; }
//! Get the Rating Matrix.
const arma::mat& Rating() const { return rating; }
//! Get the data matrix.
const arma::mat& Data() const { return data; }
//! Get the cleaned data matrix.
const arma::sp_mat& CleanedData() const { return cleanedData; }
/**
* Generates the given number of recommendations for all users.
*
* @param numRecs Number of Recommendations
* @param recommendations Matrix to save recommendations into.
*/
void GetRecommendations(const size_t numRecs,
arma::Mat<size_t>& recommendations);
/**
* Generates the given number of recommendations for the specified users.
*
* @param numRecs Number of Recommendations
* @param recommendations Matrix to save recommendations
* @param users Users for which recommendations are to be generated
*/
void GetRecommendations(const size_t numRecs,
arma::Mat<size_t>& recommendations,
arma::Col<size_t>& users);
/**
* Returns a string representation of this object.
*/
std::string ToString() const;
private:
//! Initial data matrix.
arma::mat data;
//! Number of users for similarity.
size_t numUsersForSimilarity;
//! Rank used for matrix factorization.
size_t rank;
//! Instantiated factorizer object.
FactorizerType factorizer;
//! User matrix.
arma::mat w;
//! Item matrix.
arma::mat h;
//! Rating matrix.
arma::mat rating;
//! Cleaned data matrix.
arma::sp_mat cleanedData;
//! Converts the User, Item, Value Matrix to User-Item Table
void CleanData();
/**
* Helper function to insert a point into the recommendation matrices.
*
* @param queryIndex Index of point whose recommendations we are inserting
* into.
* @param pos Position in list to insert into.
* @param neighbor Index of item being inserted as a recommendation.
* @param value Value of recommendation.
*/
void InsertNeighbor(const size_t queryIndex,
const size_t pos,
const size_t neighbor,
const double value,
arma::Mat<size_t>& recommendations,
arma::mat& values) const;
}; // class CF
}; // namespace cf
}; // namespace mlpack
//Include implementation
#include "cf_impl.hpp"
#endif
|