---
name: "mlpack_cf_CF"
section: "3"
platform: "linux"
package: "mlpack-doc"
version: "1.0.10-1"
release: "Debian 8 (Jessie)"
platforms: ["linux"]
url: "https://new.carta.tech/mlpack_cf_CF"
---

# mlpack\_cf\_CF(3)

This class implements Collaborative Filtering (CF).

## NAME

mlpack::cf::CF\< FactorizerType \> -

This class implements Collaborative Filtering (**CF**).

## SYNOPSIS

### Public Member Functions

**CF** (arma::mat &**data**, const size\_t **numUsersForSimilarity**=5, const size\_t **rank**=0)\
 *Initialize the* **CF** *object.*  const arma::sp\_mat & **CleanedData** () const \
 *Get the cleaned data matrix.*  const arma::mat & **Data** () const \
 *Get the data matrix.*  void **Factorizer** (const FactorizerType &f)\
 *Sets factorizer for NMF.*  void **GetRecommendations** (const size\_t numRecs, arma::Mat\< size\_t \> &recommendations)\
 *Generates the given number of recommendations for all users.*  void **GetRecommendations** (const size\_t numRecs, arma::Mat\< size\_t \> &recommendations, arma::Col\< size\_t \> &users)\
 *Generates the given number of recommendations for the specified users.*  const arma::mat & **H** () const \
 *Get the Item Matrix.*  void **NumUsersForSimilarity** (const size\_t num)\
 *Sets number of users for calculating similarity.*  size\_t **NumUsersForSimilarity** () const \
 *Gets number of users for calculating similarity.*  void **Rank** (const size\_t rankValue)\
 *Sets rank parameter for matrix factorization.*  size\_t **Rank** () const \
 *Gets rank parameter for matrix factorization.*  const arma::mat & **Rating** () const \
 *Get the Rating Matrix.*  std::string **ToString** () const \
 *Returns a string representation of this object.*  const arma::mat & **W** () const \
 *Get the User Matrix.*

### Private Member Functions

void **CleanData** ()\
 *Converts the User, Item, Value Matrix to User-Item Table.*  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 \
 *Helper function to insert a point into the recommendation matrices.*

### Private Attributes

arma::sp\_mat **cleanedData**\
 *Cleaned data matrix.*  arma::mat **data**\
 *Initial data matrix.*  FactorizerType **factorizer**\
 *Instantiated factorizer object.*  arma::mat **h**\
 *Item matrix.*  size\_t **numUsersForSimilarity**\
 *Number of users for similarity.*  size\_t **rank**\
 *Rank used for matrix factorization.*  arma::mat **rating**\
 *Rating matrix.*  arma::mat **w**\
 *User matrix.*

## Detailed Description

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\>class mlpack::cf::CF\< FactorizerType \>

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.

```
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);
```

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.

**Template Parameters:**

*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).

Definition at line 76 of file cf.hpp.

## Constructor & Destructor Documentation

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> mlpack::cf::CF\< FactorizerType \>::CF (arma::mat &data, const size\_tnumUsersForSimilarity = `5`, const size\_trank = `0`)

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.

**Parameters:**

*data* Initial (user, item, rating) matrix. \
 *numUsersForSimilarity* Size of the neighborhood. \
 *rank* Rank parameter for matrix factorization.

## Member Function Documentation

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> void mlpack::cf::CF\< FactorizerType \>::CleanData ()` [private]`

Converts the User, Item, Value Matrix to User-Item Table.

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> const arma::sp\_mat& mlpack::cf::CF\< FactorizerType \>::CleanedData () const` [inline]`

Get the cleaned data matrix.

Definition at line 138 of file cf.hpp.

References mlpack::cf::CF\< FactorizerType \>::cleanedData.

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> const arma::mat& mlpack::cf::CF\< FactorizerType \>::Data () const` [inline]`

Get the data matrix.

Definition at line 136 of file cf.hpp.

References mlpack::cf::CF\< FactorizerType \>::data.

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> void mlpack::cf::CF\< FactorizerType \>::Factorizer (const FactorizerType &f)` [inline]`

Sets factorizer for NMF.

Definition at line 124 of file cf.hpp.

References mlpack::cf::CF\< FactorizerType \>::factorizer.

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> void mlpack::cf::CF\< FactorizerType \>::GetRecommendations (const size\_tnumRecs, arma::Mat\< size\_t \> &recommendations)

Generates the given number of recommendations for all users.

**Parameters:**

*numRecs* Number of Recommendations \
 *recommendations* Matrix to save recommendations into.

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> void mlpack::cf::CF\< FactorizerType \>::GetRecommendations (const size\_tnumRecs, arma::Mat\< size\_t \> &recommendations, arma::Col\< size\_t \> &users)

Generates the given number of recommendations for the specified users.

**Parameters:**

*numRecs* Number of Recommendations \
 *recommendations* Matrix to save recommendations \
 *users* Users for which recommendations are to be generated

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> const arma::mat& mlpack::cf::CF\< FactorizerType \>::H () const` [inline]`

Get the Item Matrix.

Definition at line 132 of file cf.hpp.

References mlpack::cf::CF\< FactorizerType \>::h.

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> void mlpack::cf::CF\< FactorizerType \>::InsertNeighbor (const size\_tqueryIndex, const size\_tpos, const size\_tneighbor, const doublevalue, arma::Mat\< size\_t \> &recommendations, arma::mat &values) const` [private]`

Helper function to insert a point into the recommendation matrices.

**Parameters:**

*queryIndex* Index of point whose recommendations we are inserting into. \
 *pos* Position in list to insert into. \
 *neighbor* Index of item being inserted as a recommendation. \
 *value* Value of recommendation.

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> void mlpack::cf::CF\< FactorizerType \>::NumUsersForSimilarity (const size\_tnum)` [inline]`

Sets number of users for calculating similarity.

Definition at line 94 of file cf.hpp.

References mlpack::cf::CF\< FactorizerType \>::numUsersForSimilarity, and mlpack::Log::Warn.

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> size\_t mlpack::cf::CF\< FactorizerType \>::NumUsersForSimilarity () const` [inline]`

Gets number of users for calculating similarity.

Definition at line 106 of file cf.hpp.

References mlpack::cf::CF\< FactorizerType \>::numUsersForSimilarity.

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> void mlpack::cf::CF\< FactorizerType \>::Rank (const size\_trankValue)` [inline]`

Sets rank parameter for matrix factorization.

Definition at line 112 of file cf.hpp.

References mlpack::cf::CF\< FactorizerType \>::rank.

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> size\_t mlpack::cf::CF\< FactorizerType \>::Rank () const` [inline]`

Gets rank parameter for matrix factorization.

Definition at line 118 of file cf.hpp.

References mlpack::cf::CF\< FactorizerType \>::rank.

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> const arma::mat& mlpack::cf::CF\< FactorizerType \>::Rating () const` [inline]`

Get the Rating Matrix.

Definition at line 134 of file cf.hpp.

References mlpack::cf::CF\< FactorizerType \>::rating.

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> std::string mlpack::cf::CF\< FactorizerType \>::ToString () const

Returns a string representation of this object.

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> const arma::mat& mlpack::cf::CF\< FactorizerType \>::W () const` [inline]`

Get the User Matrix.

Definition at line 130 of file cf.hpp.

References mlpack::cf::CF\< FactorizerType \>::w.

## Member Data Documentation

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> arma::sp\_mat mlpack::cf::CF\< FactorizerType \>::cleanedData` [private]`

Cleaned data matrix.

Definition at line 181 of file cf.hpp.

Referenced by mlpack::cf::CF\< FactorizerType \>::CleanedData().

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> arma::mat mlpack::cf::CF\< FactorizerType \>::data` [private]`

Initial data matrix.

Definition at line 167 of file cf.hpp.

Referenced by mlpack::cf::CF\< FactorizerType \>::Data().

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> FactorizerType mlpack::cf::CF\< FactorizerType \>::factorizer` [private]`

Instantiated factorizer object.

Definition at line 173 of file cf.hpp.

Referenced by mlpack::cf::CF\< FactorizerType \>::Factorizer().

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> arma::mat mlpack::cf::CF\< FactorizerType \>::h` [private]`

Item matrix.

Definition at line 177 of file cf.hpp.

Referenced by mlpack::cf::CF\< FactorizerType \>::H().

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> size\_t mlpack::cf::CF\< FactorizerType \>::numUsersForSimilarity` [private]`

Number of users for similarity.

Definition at line 169 of file cf.hpp.

Referenced by mlpack::cf::CF\< FactorizerType \>::NumUsersForSimilarity().

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> size\_t mlpack::cf::CF\< FactorizerType \>::rank` [private]`

Rank used for matrix factorization.

Definition at line 171 of file cf.hpp.

Referenced by mlpack::cf::CF\< FactorizerType \>::Rank().

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> arma::mat mlpack::cf::CF\< FactorizerType \>::rating` [private]`

Rating matrix.

Definition at line 179 of file cf.hpp.

Referenced by mlpack::cf::CF\< FactorizerType \>::Rating().

### template\<typename FactorizerType = amf::AMF\<amf::SimpleResidueTermination, amf::RandomInitialization, amf::NMFALSUpdate\>\> arma::mat mlpack::cf::CF\< FactorizerType \>::w` [private]`

User matrix.

Definition at line 175 of file cf.hpp.

Referenced by mlpack::cf::CF\< FactorizerType \>::W().

## Author

Generated automatically by Doxygen for MLPACK from the source code.
