228 lines
32 KiB
HTML
228 lines
32 KiB
HTML
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
|
<html xmlns="http://www.w3.org/1999/xhtml">
|
|
<head>
|
|
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
|
|
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
|
|
<meta name="generator" content="Doxygen 1.8.11"/>
|
|
<title>mlpack: Collaborative filtering tutorial</title>
|
|
<link href="tabs.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="jquery.js"></script>
|
|
<script type="text/javascript" src="dynsections.js"></script>
|
|
<link href="search/search.css" rel="stylesheet" type="text/css"/>
|
|
<script type="text/javascript" src="search/searchdata.js"></script>
|
|
<script type="text/javascript" src="search/search.js"></script>
|
|
<script type="text/javascript">
|
|
$(document).ready(function() { init_search(); });
|
|
</script>
|
|
<link href="doxygen.css" rel="stylesheet" type="text/css" />
|
|
<link href="extra-stylesheet.css" rel="stylesheet" type="text/css"/>
|
|
</head>
|
|
<body>
|
|
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
|
|
<div id="titlearea">
|
|
<table cellspacing="0" cellpadding="0">
|
|
<tbody>
|
|
<tr style="height: 56px;">
|
|
<td id="projectalign" style="padding-left: 0.5em;">
|
|
<div id="projectname">mlpack
|
|
 <span id="projectnumber">master</span>
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
<!-- end header part -->
|
|
<!-- Generated by Doxygen 1.8.11 -->
|
|
<script type="text/javascript">
|
|
var searchBox = new SearchBox("searchBox", "search",false,'Search');
|
|
</script>
|
|
<div id="navrow1" class="tabs">
|
|
<ul class="tablist">
|
|
<li><a href="index.html"><span>Main Page</span></a></li>
|
|
<li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
|
|
<li><a href="namespaces.html"><span>Namespaces</span></a></li>
|
|
<li><a href="annotated.html"><span>Classes</span></a></li>
|
|
<li><a href="files.html"><span>Files</span></a></li>
|
|
<li>
|
|
<div id="MSearchBox" class="MSearchBoxInactive">
|
|
<span class="left">
|
|
<img id="MSearchSelect" src="search/mag_sel.png"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
alt=""/>
|
|
<input type="text" id="MSearchField" value="Search" accesskey="S"
|
|
onfocus="searchBox.OnSearchFieldFocus(true)"
|
|
onblur="searchBox.OnSearchFieldFocus(false)"
|
|
onkeyup="searchBox.OnSearchFieldChange(event)"/>
|
|
</span><span class="right">
|
|
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
|
|
</span>
|
|
</div>
|
|
</li>
|
|
</ul>
|
|
</div>
|
|
<!-- window showing the filter options -->
|
|
<div id="MSearchSelectWindow"
|
|
onmouseover="return searchBox.OnSearchSelectShow()"
|
|
onmouseout="return searchBox.OnSearchSelectHide()"
|
|
onkeydown="return searchBox.OnSearchSelectKey(event)">
|
|
</div>
|
|
|
|
<!-- iframe showing the search results (closed by default) -->
|
|
<div id="MSearchResultsWindow">
|
|
<iframe src="javascript:void(0)" frameborder="0"
|
|
name="MSearchResults" id="MSearchResults">
|
|
</iframe>
|
|
</div>
|
|
|
|
</div><!-- top -->
|
|
<div class="header">
|
|
<div class="headertitle">
|
|
<div class="title">Collaborative filtering tutorial </div> </div>
|
|
</div><!--header-->
|
|
<div class="contents">
|
|
<div class="textblock"><h1><a class="anchor" id="intro_cftut"></a>
|
|
Introduction</h1>
|
|
<p>Collaborative filtering is an increasingly popular approach for recommender systems. A typical formulation of the problem is as follows: there are <img class="formulaInl" alt="$n$" src="form_33.png"/> users and <img class="formulaInl" alt="$m$" src="form_105.png"/> items, and each user has rated some of the items. We want to provide each user with a recommendation for an item they have not rated yet, which they are likely to rate highly. In another formulation, we may want to predict a user's rating of an item. This type of problem has been considered extensively, especially in the context of the Netflix prize. The winning approach for the Netflix prize was a collaborative filtering approach which utilized matrix decomposition. More information on their approach can be found in the following paper:</p>
|
|
<div class="fragment"><div class="line">@article{koren2009matrix,</div><div class="line"> title={Matrix factorization techniques <span class="keywordflow">for</span> recommender systems},</div><div class="line"> author={Koren, Yehuda and Bell, Robert and Volinsky, Chris},</div><div class="line"> journal={Computer},</div><div class="line"> number={8},</div><div class="line"> pages={30--37},</div><div class="line"> year={2009},</div><div class="line"> publisher={IEEE}</div><div class="line">}</div></div><!-- fragment --><p>The key to this approach is that the data is represented as an incomplete matrix <img class="formulaInl" alt="$V \in \Re^{n \times m}$" src="form_106.png"/>, where <img class="formulaInl" alt="$V_{ij}$" src="form_107.png"/> represents user <img class="formulaInl" alt="$i$" src="form_108.png"/>'s rating of item <img class="formulaInl" alt="$j$" src="form_109.png"/>, if that rating exists. The task, then, is to complete the entries of the matrix.</p>
|
|
<p>In the matrix factorization framework, the matrix <img class="formulaInl" alt="$V$" src="form_110.png"/> is assumed to be low-rank and decomposed into components as <img class="formulaInl" alt="$V \approx WH$" src="form_111.png"/> according to some heuristic.</p>
|
|
<p>In order to solve problems of this form, <b>mlpack</b> provides:</p>
|
|
<ul>
|
|
<li>a <a class="el" href="cftutorial.html#cli_cftut">simple command-line interface</a> to perform collaborative filtering</li>
|
|
<li>a <a class="el" href="cftutorial.html#cf_cftut">simple C++ interface</a> to perform collaborative filtering</li>
|
|
<li>an <a class="el" href="cftutorial.html#cpp_cftut">extensible C++ interface</a> for implementing new collaborative filtering techniques</li>
|
|
</ul>
|
|
<h1><a class="anchor" id="toc_cftut"></a>
|
|
Table of Contents</h1>
|
|
<ul>
|
|
<li><a class="el" href="cftutorial.html#intro_cftut">Introduction</a></li>
|
|
<li><a class="el" href="cftutorial.html#toc_cftut">Table of Contents</a></li>
|
|
<li><a class="el" href="cftutorial.html#cli_cftut">The 'mlpack_cf' program</a><ul>
|
|
<li><a class="el" href="cftutorial.html#cli_input_format">Input format for mlpack_cf</a></li>
|
|
<li><a class="el" href="cftutorial.html#ex1_cf_cli">mlpack_cf with default parameters</a></li>
|
|
<li><a class="el" href="cftutorial.html#ex1a_cf_cli">Saving mlpack_cf models</a></li>
|
|
<li><a class="el" href="cftutorial.html#ex1b_cf_cli">Loading mlpack_cf models</a></li>
|
|
<li><a class="el" href="cftutorial.html#ex2_cf_cli">Specifying rank of mlpack_cf decomposition</a></li>
|
|
<li><a class="el" href="cftutorial.html#ex3_cf_cli">mlpack_cf with single-user recommendation</a></li>
|
|
<li><a class="el" href="cftutorial.html#ex4_cf_cli">mlpack_cf with non-default factorizer</a></li>
|
|
<li><a class="el" href="cftutorial.html#ex5_cf_cli">mlpack_cf with non-default neighborhood size</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a class="el" href="cftutorial.html#cf_cftut">The 'CF' class</a><ul>
|
|
<li><a class="el" href="cftutorial.html#ex1_cf_cpp">CF with default parameters</a></li>
|
|
<li><a class="el" href="cftutorial.html#ex2_cf_cpp">CF with other factorizers</a></li>
|
|
<li><a class="el" href="cftutorial.html#ex3_cf_cpp">Predicting individual user/item ratings</a></li>
|
|
<li><a class="el" href="cftutorial.html#ex4_cf_cpp">Other operations with the W and H matrices</a></li>
|
|
</ul>
|
|
</li>
|
|
<li><a class="el" href="cftutorial.html#cpp_cftut">Template parameters for the 'CF' class</a></li>
|
|
<li><a class="el" href="cftutorial.html#further_doc_cftut">Further documentation</a></li>
|
|
</ul>
|
|
<h1><a class="anchor" id="cli_cftut"></a>
|
|
The 'mlpack_cf' program</h1>
|
|
<p><b>mlpack</b> provides a command-line program, <code>mlpack_cf</code>, which is used to perform collaborative filtering on a given dataset. It can provide neighborhood-based recommendations for users. The algorithm used for matrix factorization is configurable, and the parameters of each algorithm are also configurable.</p>
|
|
<p>The following examples detail usage of the <code>mlpack_cf</code> program. Note that you can get documentation on all the possible parameters by typing:</p>
|
|
<div class="fragment"><div class="line">$ mlpack_cf --help</div></div><!-- fragment --><h2><a class="anchor" id="cli_input_format"></a>
|
|
Input format for mlpack_cf</h2>
|
|
<p>The input file for the <code>mlpack_cf</code> program is specified with the <code>–training_file</code> or <code>-t</code> option. This file is a coordinate-format sparse matrix, similar to the Matrix Market (MM) format. The first coordinate is the user id; the second coordinate is the item id; and the third coordinate is the rating. So, for instance, a dataset with 3 users and 2 items, and ratings between 1 and 5, might look like the following:</p>
|
|
<div class="fragment"><div class="line">$ cat dataset.csv</div><div class="line">0, 1, 4</div><div class="line">1, 0, 5</div><div class="line">1, 1, 1</div><div class="line">2, 0, 2</div></div><!-- fragment --><p>This dataset has four ratings: user 0 has rated item 1 with a rating of 4; user 1 has rated item 0 with a rating of 5; user 1 has rated item 1 with a rating of 1; and user 2 has rated item 0 with a rating of 2. Note that the user and item indices start from 0, and the identifiers must be numeric indices, and not names.</p>
|
|
<p>The type does not necessarily need to be a csv; it can be any supported storage format, assuming that it is a coordinate-format file in the format specified above. For more information on mlpack file formats, see the documentation for <a class="el" href="namespacemlpack_1_1data.html#a19805d6585ac8b0be7c4e4b7f081977c" title="Loads a matrix from file, guessing the filetype from the extension. ">mlpack::data::Load()</a>.</p>
|
|
<h2><a class="anchor" id="ex1_cf_cli"></a>
|
|
mlpack_cf with default parameters</h2>
|
|
<p>In this example, we have a dataset from MovieLens, and we want to use <code>mlpack_cf</code> with the default parameters, which will provide 5 recommendations for each user, and we wish to save the results in the file <code>recommendations.csv</code>. Assuming that our dataset is in the file <code>MovieLens-100k.csv</code> and it is in the correct format, we may use the <code>mlpack_cf</code> executable as below:</p>
|
|
<div class="fragment"><div class="line">$ mlpack_cf -t MovieLens-100k.csv -v -o recommendations.csv</div></div><!-- fragment --><p>The <code>-v</code> option provides verbose output, and may be omitted if desired. Now, for each user, we have recommendations in <code>recommendations.csv:</code> </p>
|
|
<div class="fragment"><div class="line">$ head recommendations.csv</div><div class="line">317,422,482,356,495</div><div class="line">116,120,180,6,327</div><div class="line">312,49,116,99,236</div><div class="line">312,116,99,236,285</div><div class="line">55,190,317,194,63</div><div class="line">171,209,180,175,95</div><div class="line">208,0,94,87,57</div><div class="line">99,97,0,203,172</div><div class="line">257,99,180,287,0</div><div class="line">171,203,172,209,88</div></div><!-- fragment --><p>So, for user 0, the top 5 recommended items that user 0 has not rated are items 317, 422, 482, 356, and 495. For user 5, the recommendations are on the sixth line: 171, 209, 180, 175, 95.</p>
|
|
<p>The <code>mlpack_cf</code> program can be built into a larger recommendation framework, with a preprocessing step that can turn user information and item information into numeric IDs, and a postprocessing step that can map these numeric IDs back to the original information.</p>
|
|
<h2><a class="anchor" id="ex1a_cf_cli"></a>
|
|
Saving mlpack_cf models</h2>
|
|
<p>The <code>mlpack_cf</code> program is able to save a particular model for later loading. Saving a model can be done with the <code>–output_model_file</code> or <code>-M</code> option. The example below builds a CF model on the <code>MovieLens-100k.csv</code> dataset, and then saves the model to the file <code>cf-model.xml</code> for later usage.</p>
|
|
<div class="fragment"><div class="line">$ mlpack_cf -t MovieLens-100k.csv -M cf-model.xml -v</div></div><!-- fragment --><p>The models can also be saved as <code></code>.bin or <code></code>.txt; the <code></code>.xml format provides a human-inspectable format (though the models tend to be quite complex and may be difficult to read). These models can then be re-used to provide specific recommendations for certain users, or other tasks.</p>
|
|
<h2><a class="anchor" id="ex1b_cf_cli"></a>
|
|
Loading mlpack_cf models</h2>
|
|
<p>Instead of training a model, the <code>mlpack_cf</code> model can also load a model to provide recommendations, using the <code>–input_model_file</code> or <code>-m</code> option. For instance, the example below will load the model from <code>cf-model.xml</code> and then generate 3 recommendations for each user in the dataset, saving the results to <code>recommendations.csv</code>.</p>
|
|
<div class="fragment"><div class="line">$ mlpack_cf -m cf-model.xml -v -o recommendations.csv</div></div><!-- fragment --><h2><a class="anchor" id="ex2_cf_cli"></a>
|
|
Specifying rank of mlpack_cf decomposition</h2>
|
|
<p>By default, the matrix factorizations in the <code>mlpack_cf</code> program decompose the data matrix into two matrices <img class="formulaInl" alt="$W$" src="form_112.png"/> and <img class="formulaInl" alt="$H$" src="form_113.png"/> with rank two. Often, this default parameter is not correct, and it makes sense to use a higher-rank decomposition. The rank can be specified with the <code>–rank</code> or <code>-R</code> parameter:</p>
|
|
<div class="fragment"><div class="line">$ mlpack_cf -t MovieLens-100k.csv -R 10 -v</div></div><!-- fragment --><p>In the example above, the data matrix will be decomposed into two matrices of rank 10. In general, higher-rank decompositions will take longer, but will give more accurate predictions.</p>
|
|
<h2><a class="anchor" id="ex3_cf_cli"></a>
|
|
mlpack_cf with single-user recommendation</h2>
|
|
<p>In the previous two examples, the output file <code>recommendations.csv</code> contains one line for each user in the input dataset. But often, recommendations may only be desired for a few users. In that case, we can assemble a file of query users, with one user per line:</p>
|
|
<div class="fragment"><div class="line">$ cat query.csv</div><div class="line">0</div><div class="line">17</div><div class="line">31</div></div><!-- fragment --><p>Now, if we run the <code>mlpack_cf</code> executable with this query file, we will obtain recommendations for users 0, 17, and 31:</p>
|
|
<div class="fragment"><div class="line">$ mlpack_cf -i MovieLens-100k.csv -R 10 -q query.csv -o recommendations.csv</div><div class="line">$ cat recommendations.csv</div><div class="line">474,356,317,432,473</div><div class="line">510,172,204,483,182</div><div class="line">0,120,236,257,126</div></div><!-- fragment --><h2><a class="anchor" id="ex4_cf_cli"></a>
|
|
mlpack_cf with non-default factorizer</h2>
|
|
<p>The <code>–algorithm</code> (or <code>-a</code> ) parameter controls the factorizer that is used. Several options are available:</p>
|
|
<ul>
|
|
<li><code>'NMF'</code>: non-negative matrix factorization; see mlpack::amf::AMF<></li>
|
|
<li><code>'SVDBatch'</code>: SVD batch factorization</li>
|
|
<li><code>'SVDIncompleteIncremental'</code>: incomplete incremental SVD</li>
|
|
<li><code>'SVDCompleteIncremental'</code>: complete incremental SVD</li>
|
|
<li><code>'RegSVD'</code>: regularized SVD; see <a class="el" href="classmlpack_1_1svd_1_1RegularizedSVD.html" title="Regularized SVD is a matrix factorization technique that seeks to reduce the error on the training se...">mlpack::svd::RegularizedSVD</a></li>
|
|
</ul>
|
|
<p>The default factorizer is <code>'NMF'</code>. The example below uses the 'RegSVD' factorizer:</p>
|
|
<div class="fragment"><div class="line">$ mlpack_cf -i MovieLens-100k.csv -R 10 -q query.csv -a RegSVD -o recommendations.csv</div></div><!-- fragment --><h2><a class="anchor" id="ex5_cf_cli"></a>
|
|
mlpack_cf with non-default neighborhood size</h2>
|
|
<p>The <code>mlpack_cf</code> program produces recommendations using a neighborhood: similar users in the query user's neighborhood will be averaged to produce predictions. The size of this neighborhood is controlled with the <code>–neighborhood</code> (or <code>-n</code> ) option. An example using a neighborhood with 10 similar users is below:</p>
|
|
<div class="fragment"><div class="line">$ mlpack_cf -i MovieLens-100k.csv -R 10 -q query.csv -a RegSVD -n 10</div></div><!-- fragment --><h1><a class="anchor" id="cf_cftut"></a>
|
|
The 'CF' class</h1>
|
|
<p>The <code>CF</code> class in <b>mlpack</b> offers a simple, flexible API for performing collaborative filtering for recommender systems within C++ applications. In the constructor, the <code>CF</code> class takes a coordinate-list dataset and decomposes the matrix according to the specified <code>FactorizerType</code> template parameter.</p>
|
|
<p>Then, the <code>GetRecommendations()</code> function may be called to obtain recommendations for certain users (or all users), and the <code>W()</code> and <code>H()</code> matrices may be accessed to perform other computations.</p>
|
|
<p>The data which the <code>CF</code> constructor takes should be an Armadillo matrix (<code>arma::mat</code> ) with three rows. The first row corresponds to users; the second row corresponds to items; the third column corresponds to the rating. This is a coordinate list format, like the format the <code>cf</code> executable takes. The <a class="el" href="namespacemlpack_1_1data.html#a19805d6585ac8b0be7c4e4b7f081977c" title="Loads a matrix from file, guessing the filetype from the extension. ">data::Load()</a> function can be used to load data.</p>
|
|
<p>The following examples detail a few ways that the <code>CF</code> class can be used.</p>
|
|
<h2><a class="anchor" id="ex1_cf_cpp"></a>
|
|
CF with default parameters</h2>
|
|
<p>This example constructs the <code>CF</code> object with default parameters and obtains recommendations for each user, storing the output in the <code>recommendations</code> matrix.</p>
|
|
<div class="fragment"><div class="line"><span class="preprocessor">#include <<a class="code" href="cf_8hpp.html">mlpack/methods/cf/cf.hpp</a>></span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="namespacemlpack_1_1cf.html">mlpack::cf</a>;</div><div class="line"></div><div class="line"><span class="comment">// The coordinate list of ratings that we have.</span></div><div class="line"><span class="keyword">extern</span> arma::mat data;</div><div class="line"><span class="comment">// The size of the neighborhood to use to get recommendations.</span></div><div class="line"><span class="keyword">extern</span> <span class="keywordtype">size_t</span> neighborhood;</div><div class="line"><span class="comment">// The rank of the decomposition.</span></div><div class="line"><span class="keyword">extern</span> <span class="keywordtype">size_t</span> rank;</div><div class="line"></div><div class="line"><span class="comment">// Build the CF object and perform the decomposition.</span></div><div class="line"><span class="comment">// The constructor takes a default-constructed factorizer, which, by default,</span></div><div class="line"><span class="comment">// is of type amf::NMFALSFactorizer.</span></div><div class="line"><a class="code" href="classmlpack_1_1cf_1_1CF.html">CF</a> cf(data, <a class="code" href="namespacemlpack_1_1amf.html#a28055143d86e284abd74bd150ec38c52">amf::NMFALSFactorizer</a>(), neighborhood, rank);</div><div class="line"></div><div class="line"><span class="comment">// Store the results in this object.</span></div><div class="line">arma::Mat<size_t> recommendations;</div><div class="line"></div><div class="line"><span class="comment">// Get 5 recommendations for all users.</span></div><div class="line">cf.GetRecommendations(5, recommendations);</div></div><!-- fragment --><h2><a class="anchor" id="ex2_cf_cpp"></a>
|
|
CF with other factorizers</h2>
|
|
<p><b>mlpack</b> provides a number of existing factorizers which can be used in place of the default <a class="el" href="namespacemlpack_1_1amf.html#a28055143d86e284abd74bd150ec38c52">mlpack::amf::NMFALSFactorizer</a> (which is non-negative matrix factorization with alternating least squares update rules). These include:</p>
|
|
<ul>
|
|
<li><a class="el" href="namespacemlpack_1_1amf.html#a0a89e9140ca599a10ead8ba2dacbc38b" title="SparseSVDBatchFactorizer factorizes given matrix V into two matrices W and H by gradient descent...">mlpack::amf::SVDBatchFactorizer</a></li>
|
|
<li><a class="el" href="namespacemlpack_1_1amf.html#a2cd6a9e37e9e0942a335fcc60f853722" title="SVDCompleteIncrementalFactorizer factorizes given matrix V into two matrices W and H by complete incr...">mlpack::amf::SVDCompleteIncrementalFactorizer</a></li>
|
|
<li><a class="el" href="namespacemlpack_1_1amf.html#adbb3b940b48c9f2996a825d66ef1a302" title="SVDIncompleteIncrementalFactorizer factorizes given matrix V into two matrices W and H by incomplete ...">mlpack::amf::SVDIncompleteIncrementalFactorizer</a></li>
|
|
<li><a class="el" href="namespacemlpack_1_1amf.html#a28055143d86e284abd74bd150ec38c52">mlpack::amf::NMFALSFactorizer</a></li>
|
|
<li><a class="el" href="classmlpack_1_1svd_1_1RegularizedSVD.html" title="Regularized SVD is a matrix factorization technique that seeks to reduce the error on the training se...">mlpack::svd::RegularizedSVD</a></li>
|
|
<li><a class="el" href="classmlpack_1_1svd_1_1QUIC__SVD.html" title="QUIC-SVD is a matrix factorization technique, which operates in a subspace such that A's approximatio...">mlpack::svd::QUIC_SVD</a></li>
|
|
</ul>
|
|
<p>The amf::AMF<> class has many other possibilities than those listed here; it is a framework for alternating matrix factorization techniques. See the class documentation or <a class="el" href="amftutorial.html">tutorial on AMF</a> for more information.</p>
|
|
<p>The use of another factorizer is straightforward; the example from the previous section is adapted below to use svd::RegularizedSVD:</p>
|
|
<div class="fragment"><div class="line"><span class="preprocessor">#include <<a class="code" href="cf_8hpp.html">mlpack/methods/cf/cf.hpp</a>></span></div><div class="line"><span class="preprocessor">#include <<a class="code" href="regularized__svd_8hpp.html">mlpack/methods/regularized_svd/regularized_svd.hpp</a>></span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="namespacemlpack_1_1cf.html">mlpack::cf</a>;</div><div class="line"></div><div class="line"><span class="comment">// The coordinate list of ratings that we have.</span></div><div class="line"><span class="keyword">extern</span> arma::mat data;</div><div class="line"><span class="comment">// The size of the neighborhood to use to get recommendations.</span></div><div class="line"><span class="keyword">extern</span> <span class="keywordtype">size_t</span> neighborhood;</div><div class="line"><span class="comment">// The rank of the decomposition.</span></div><div class="line"><span class="keyword">extern</span> <span class="keywordtype">size_t</span> rank;</div><div class="line"></div><div class="line"><span class="comment">// Build the CF object and perform the decomposition.</span></div><div class="line"><a class="code" href="classmlpack_1_1cf_1_1CF.html">CF</a> cf(data, svd::RegularizedSVD(), neighborhood, rank);</div><div class="line"></div><div class="line"><span class="comment">// Store the results in this object.</span></div><div class="line">arma::Mat<size_t> recommendations;</div><div class="line"></div><div class="line"><span class="comment">// Get 5 recommendations for all users.</span></div><div class="line">cf.GetRecommendations(5, recommendations);</div></div><!-- fragment --><h2><a class="anchor" id="ex3_cf_cpp"></a>
|
|
Predicting individual user/item ratings</h2>
|
|
<p>The <code>Predict()</code> method can be used to predict the rating of an item by a certain user, using the same neighborhood-based approach as the <code>GetRecommendations()</code> function or the <code>cf</code> executable. Below is an example of the use of that function.</p>
|
|
<p>The example below will obtain the predicted rating for item 50 by user 12.</p>
|
|
<div class="fragment"><div class="line"><span class="preprocessor">#include <<a class="code" href="cf_8hpp.html">mlpack/methods/cf/cf.hpp</a>></span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="namespacemlpack_1_1cf.html">mlpack::cf</a>;</div><div class="line"></div><div class="line"><span class="comment">// The coordinate list of ratings that we have.</span></div><div class="line"><span class="keyword">extern</span> arma::mat data;</div><div class="line"><span class="comment">// The size of the neighborhood to use to get recommendations.</span></div><div class="line"><span class="keyword">extern</span> <span class="keywordtype">size_t</span> neighborhood;</div><div class="line"><span class="comment">// The rank of the decomposition.</span></div><div class="line"><span class="keyword">extern</span> <span class="keywordtype">size_t</span> rank;</div><div class="line"></div><div class="line"><span class="comment">// Build the CF object and perform the decomposition.</span></div><div class="line"><span class="comment">// The constructor takes a default-constructed factorizer, which, by default,</span></div><div class="line"><span class="comment">// is of type amf::NMFALSFactorizer.</span></div><div class="line"><a class="code" href="classmlpack_1_1cf_1_1CF.html">CF</a> cf(data, <a class="code" href="namespacemlpack_1_1amf.html#a28055143d86e284abd74bd150ec38c52">amf::NMFALSFactorizer</a>(), neighborhood, rank);</div><div class="line"></div><div class="line"><span class="keyword">const</span> <span class="keywordtype">double</span> prediction = cf.Predict(12, 50); <span class="comment">// User 12, item 50.</span></div></div><!-- fragment --><h2><a class="anchor" id="ex4_cf_cpp"></a>
|
|
Other operations with the W and H matrices</h2>
|
|
<p>Sometimes, the raw decomposed W and H matrices can be useful. The example below obtains these matrices, and multiplies them against each other to obtain a reconstructed data matrix with no missing values.</p>
|
|
<div class="fragment"><div class="line"><span class="preprocessor">#include <<a class="code" href="cf_8hpp.html">mlpack/methods/cf/cf.hpp</a>></span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="namespacemlpack_1_1cf.html">mlpack::cf</a>;</div><div class="line"></div><div class="line"><span class="comment">// The coordinate list of ratings that we have.</span></div><div class="line"><span class="keyword">extern</span> arma::mat data;</div><div class="line"><span class="comment">// The size of the neighborhood to use to get recommendations.</span></div><div class="line"><span class="keyword">extern</span> <span class="keywordtype">size_t</span> neighborhood;</div><div class="line"><span class="comment">// The rank of the decomposition.</span></div><div class="line"><span class="keyword">extern</span> <span class="keywordtype">size_t</span> rank;</div><div class="line"></div><div class="line"><span class="comment">// Build the CF object and perform the decomposition.</span></div><div class="line"><span class="comment">// The constructor takes a default-constructed factorizer, which, by default,</span></div><div class="line"><span class="comment">// is of type amf::NMFALSFactorizer.</span></div><div class="line"><a class="code" href="classmlpack_1_1cf_1_1CF.html">CF</a> cf(data, <a class="code" href="namespacemlpack_1_1amf.html#a28055143d86e284abd74bd150ec38c52">amf::NMFALSFactorizer</a>(), neighborhood, rank);</div><div class="line"></div><div class="line"><span class="comment">// References to W and H matrices.</span></div><div class="line"><span class="keyword">const</span> arma::mat& W = cf.W();</div><div class="line"><span class="keyword">const</span> arma::mat& H = cf.H();</div><div class="line"></div><div class="line"><span class="comment">// Multiply the matrices together.</span></div><div class="line">arma::mat reconstructed = W * H;</div></div><!-- fragment --><h1><a class="anchor" id="cpp_cftut"></a>
|
|
Template parameters for the 'CF' class</h1>
|
|
<p>The <code>CF</code> class takes the <code>FactorizerType</code> as a template parameter to some of its constructors and to the <code>Train()</code> function. The <code>FactorizerType</code> class defines the algorithm used for matrix factorization. There are a number of existing factorizers that can be used in <b>mlpack</b>; these were detailed in the <a class="el" href="cftutorial.html#ex2_cf_cpp">'other factorizers' example</a> of the previous section.</p>
|
|
<p>The <code>FactorizerType</code> class must implement one of the two following methods:</p>
|
|
<ul>
|
|
<li><code>"Apply(arma::mat& data, const size_t rank, arma::mat& W, arma::mat& H);"</code> </li>
|
|
<li><code>"Apply(arma::sp_mat& data, const size_t rank, arma::mat& W, arma::mat& H);"</code> </li>
|
|
</ul>
|
|
<p>The difference between these two methods is whether <code>arma::mat</code> or <code>arma::sp_mat</code> is used as input. If <code>arma::mat</code> is used, then the data matrix is a coordinate list with three columns, as in the constructor to the <code>CF</code> class. If <code>arma::sp_mat</code> is used, then a sparse matrix is passed with the number of rows equal to the number of items and the number of columns equal to the number of users, and each nonzero element in the matrix corresponds to a non-missing rating.</p>
|
|
<p>The method that the factorizer implements is specified via the <code>FactorizerTraits</code> class, which is a template metaprogramming traits class:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">template</span><<span class="keyword">typename</span> FactorizerType></div><div class="line"><span class="keyword">struct </span>FactorizerTraits</div><div class="line">{</div><div class="line"> <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">bool</span> UsesCoordinateList = <span class="keyword">false</span>;</div><div class="line">};</div></div><!-- fragment --><p>If <code>FactorizerTraits<MyFactorizer>::UsesCoordinateList</code> is <code>true</code>, then <code>CF</code> will try to call <code>Apply()</code> with an <code>arma::mat</code> object. Otherwise, <code>CF</code> will try to call <code>Apply()</code> with an <code>arma::sp_mat</code> object. Specifying the value of <code>UsesCoordinateList</code> is straightforward; provide this specialization of the <code>FactorizerTraits</code> class:</p>
|
|
<div class="fragment"><div class="line"><span class="keyword">template</span><></div><div class="line"><span class="keyword">struct </span>FactorizerTraits<MyFactorizer></div><div class="line">{</div><div class="line"> <span class="keyword">static</span> <span class="keyword">const</span> <span class="keywordtype">bool</span> UsesCoordinateList = <span class="keyword">true</span>; <span class="comment">// Set your value here.</span></div><div class="line">};</div></div><!-- fragment --><p>The <code>Apply()</code> function also takes a reference to the matrices <code>W</code> and <code>H</code>. When the <code>Apply()</code> function returns, the input data matrix should be decomposed into these two matrices. <code>W</code> should have number of rows equal to the number of items and number of columns equal to the <code>rank</code> parameter, and <code>H</code> should have number of rows equal to the <code>rank</code> parameter, and number of columns equal to the number of users.</p>
|
|
<p>The <a class="el" href="classmlpack_1_1amf_1_1AMF.html">amf::AMF<> class</a> can be used as a base for factorizers that alternate between updating <code>W</code> and updating <code>H</code>. A useful reference is the <a class="el" href="amftutorial.html">AMF tutorial</a>.</p>
|
|
<h1><a class="anchor" id="further_doc_cftut"></a>
|
|
Further documentation</h1>
|
|
<p>Further documentation for the <code>CF</code> class may be found in the <a class="el" href="classmlpack_1_1cf_1_1CF.html">complete API documentation</a>. In addition, more information on the <code>AMF</code> class of factorizers may be found in its <a class="el" href="classmlpack_1_1amf_1_1AMF.html">complete API documentation</a>. </p>
|
|
</div></div><!-- contents -->
|
|
<!-- start footer part -->
|
|
<hr class="footer"/><address class="footer"><small>
|
|
Generated by  <a href="http://www.doxygen.org/index.html">
|
|
<img class="footer" src="doxygen.png" alt="doxygen"/>
|
|
</a> 1.8.11
|
|
</small></address>
|
|
</body>
|
|
<script type="text/javascript">
|
|
var x = document.getElementsByClassName("formulaDsp");
|
|
var i;
|
|
for (i = 0; i < x.length; i++)
|
|
{
|
|
x[i].width /= 4;
|
|
}
|
|
</script>
|
|
</html>
|