Learn practical skills, build real-world projects, and advance your career

Collaborative Filtering

Collaborative Filtering simply put uses the "wisdom of the crowd" to recommend items. Item based collaborative filtering uses the patterns of users who liked the same movie as me to recommend me a movie (users who liked the movie that I like, also liked these other movies). Recommendation based on user's input of any movie present in the dataset is done.

%%html
<main class="container">
   <h1 id="title"><a href = "https://cambridgespark.com/content/tutorials/implementing-your-own-recommender-systems-in-Python/index.html">Implementing your own recommender systems in Python</a></h1> 
  <div id="subsbox">

<!--&lt;!&ndash;[if lte IE 8]>-->
<!--<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2-legacy.js"></script>-->
<!--<![endif]&ndash;&gt;-->
<!--<script charset="utf-8" type="text/javascript" src="//js.hsforms.net/forms/v2.js"></script>-->
<!--<script>-->
  <!--hbspt.forms.create({-->
	<!--portalId: "4132242",-->
	<!--formId: "cf642961-7bff-4e58-b97b-d05a825ebe8a"-->
<!--});-->
<!--</script>-->
</div>
  <div id="title-separator"><hr></div>
  <h1 id="implementing-your-own-recommender-systems-in-python">Implementing your own recommender systems in Python</h1>
<p>Nowadays, <em>recommender systems</em> are used to personalize your experience on the web, telling you what to buy, where to eat or even who you should be friends with. People's tastes vary, but generally follow patterns. People tend to like things that are similar to other things they like, and they tend to have similar taste as other people they are close with. Recommender systems try to capture these patterns to help predict what else you might like. E-commerce, social media, video and online news platforms have been actively deploying their own recommender systems to help their customers to choose products more efficiently, which serves win-win strategy.</p>
<p>Two most ubiquitous types of recommender systems are <em>Content-Based</em> and <em>Collaborative Filtering (CF)</em>. Collaborative filtering produces recommendations based on the knowledge of users’ attitude to items, that is it uses the “wisdom of the crowd” to recommend items. In contrast, content-based recommender systems focus on the attributes of the items and give you recommendations based on the similarity between them.</p>
<p>In general, Collaborative filtering (CF) is the workhorse of recommender engines. The algorithm has a very interesting property of being able to do feature learning on its own, which means that it can start to learn for itself what features to use. CF can be divided into <em>Memory-Based Collaborative Filtering</em> and <em>Model-Based Collaborative filtering</em>. In this tutorial, you will implement Model-Based CF by using singular value decomposition (SVD) and Memory-Based CF by computing cosine similarity.</p>
<p>You will use MovieLens dataset, which is one of the most common datasets used when implementing and testing recommender engines. It contains 100k movie ratings from 943 users and a selection of 1682 movies. You should add unzipped movielens dataset folder to your notebook directory. You can download the dataset <a href="http://files.grouplens.org/datasets/movielens/ml-100k.zip">here</a>.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> numpy <span class="im">as</span> np
<span class="im">import</span> pandas <span class="im">as</span> pd</code></pre></div>
<p>You read in the <code>u.data</code> file, which contains the full dataset. You can read a brief description of the dataset <a href="http://files.grouplens.org/datasets/movielens/ml-100k-README.txt">here</a>.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">header <span class="op">=</span> [<span class="st">'user_id'</span>, <span class="st">'item_id'</span>, <span class="st">'rating'</span>, <span class="st">'timestamp'</span>]
df <span class="op">=</span> pd.read_csv(<span class="st">'ml-100k/u.data'</span>, sep<span class="op">=</span><span class="st">'</span><span class="ch">\t</span><span class="st">'</span>, names<span class="op">=</span>header)</code></pre></div>
<p>Get a sneak peek of the first two rows in the dataset. Next, let's count the number of unique users and movies.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">n_users <span class="op">=</span> df.user_id.unique().shape[<span class="dv">0</span>]
n_items <span class="op">=</span> df.item_id.unique().shape[<span class="dv">0</span>]
<span class="bu">print</span> <span class="st">'Number of users = '</span> <span class="op">+</span> <span class="bu">str</span>(n_users) <span class="op">+</span> <span class="st">' | Number of movies = '</span> <span class="op">+</span> <span class="bu">str</span>(n_items)</code></pre></div>
<pre><code>Number of users = 943 | Number of movies = 1682</code></pre>
<p>You can use the <a href="http://scikit-learn.org/stable/"><code>scikit-learn</code></a> library to split the dataset into testing and training. <a href="http://scikit-learn.org/stable/modules/generated/sklearn.cross_validation.train_test_split.html"><code>Cross_validation.train_test_split</code></a> shuffles and splits the data into two datasets according to the percentage of test examples (<code>test_size</code>), which in this case is 0.25.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">from</span> sklearn <span class="im">import</span> cross_validation <span class="im">as</span> cv
train_data, test_data <span class="op">=</span> cv.train_test_split(df, test_size<span class="op">=</span><span class="fl">0.25</span>)</code></pre></div>
<h2 id="memory-based-collaborative-filtering">Memory-Based Collaborative Filtering</h2>
<p>Memory-Based Collaborative Filtering approaches can be divided into two main sections: <em>user-item filtering</em> and <em>item-item filtering</em>. A <em>user-item filtering</em> takes a particular user, find users that are similar to that user based on similarity of ratings, and recommend items that those similar users liked. In contrast, <em>item-item filtering</em> will take an item, find users who liked that item, and find other items that those users or similar users also liked. It takes items and outputs other items as recommendations.</p>
<ul>
<li><em>Item-Item Collaborative Filtering</em>: “Users who liked this item also liked …”</li>
<li><em>User-Item Collaborative Filtering</em>: “Users who are similar to you also liked …”</li>
</ul>
<p>In both cases, you create a user-item matrix which you build from the entire dataset. Since you have split the data into testing and training you will need to create two 943 <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-1-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mo>&amp;#x00D7;</mo></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-1" style="width: 0.828em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.779em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.653em, 1000.63em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-2"><span class="mo" id="MathJax-Span-3" style="font-family: MathJax_Main;">×</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.603em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mo>×</mo></math></span></span> rated both items.<script type="math/tex" id="MathJax-Element-1">\times</script></span> 1682 matrices. The training matrix contains 75% of the ratings and the testing matrix contains 25% of the ratings.</p>
<p>Example of user-item matrix:</p>
<div class="figure">
<img src="https://cambridgespark.com/content/tutorials/implementing-your-own-recommender-systems-in-Python/figures/BLOG_CCA_8.png">

</div>
<p>After you have built the user-item matrix you calculate the similarity and create a similarity matrix.</p>
<p>The similarity values between items in <em>Item-Item Collaborative Filtering</em> are measured by observing all the users who have rated both items.</p>
<div class="figure">
<img src="https://cambridgespark.com/content/tutorials/implementing-your-own-recommender-systems-in-Python/figures/BLOG_CCA_10.png">

</div>
<p>For <em>User-Item Collaborative Filtering</em> the similarity values between users are measured by observing all the items that are rated by both users.</p>
<div class="figure">
<img src="https://cambridgespark.com/content/tutorials/implementing-your-own-recommender-systems-in-Python/figures/BLOG_CCA_11.png">

</div>
<p>A distance metric commonly used in recommender systems is <em>cosine similarity</em>, where the ratings are seen as vectors in <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-2-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>n</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-4" style="width: 0.633em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.585em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.701em, 1000.59em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-5"><span class="mi" id="MathJax-Span-6" style="font-family: MathJax_Math-italic;">n</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.552em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>n</mi></math></span></span><script type="math/tex" id="MathJax-Element-2">n</script></span>-dimensional space and the similarity is calculated based on the angle between these vectors. Cosine similiarity for users <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-3-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>a</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-7" style="width: 0.585em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.536em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.701em, 1000.54em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-8"><span class="mi" id="MathJax-Span-9" style="font-family: MathJax_Math-italic;">a</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.552em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi></math></span></span><script type="math/tex" id="MathJax-Element-3">a</script></span> and <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-4-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>m</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-10" style="width: 0.925em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.876em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.701em, 1000.88em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-11"><span class="mi" id="MathJax-Span-12" style="font-family: MathJax_Math-italic;">m</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.552em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi></math></span></span><script type="math/tex" id="MathJax-Element-4">m</script></span> can be calculated using the formula below, where you take dot product of the user vector <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-5-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><msub><mi>u</mi><mi>k</mi></msub></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-13" style="width: 1.07em; display: inline-block;"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.701em, 1001.02em, 2.575em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-14"><span class="msubsup" id="MathJax-Span-15"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-16" style="font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-17" style="font-size: 70.7%; font-family: MathJax_Math-italic;">k</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.198em; border-left: 0px solid; width: 0px; height: 0.703em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>u</mi><mi>k</mi></msub></math></span></span><script type="math/tex" id="MathJax-Element-5">u_k</script></span> and the user vector <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-6-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><msub><mi>u</mi><mi>a</mi></msub></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-18" style="width: 1.07em; display: inline-block;"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.701em, 1001.02em, 2.575em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-19"><span class="msubsup" id="MathJax-Span-20"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-21" style="font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-22" style="font-size: 70.7%; font-family: MathJax_Math-italic;">a</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.198em; border-left: 0px solid; width: 0px; height: 0.703em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msub><mi>u</mi><mi>a</mi></msub></math></span></span><script type="math/tex" id="MathJax-Element-6">u_a</script></span> and divide it by multiplication of the Euclidean lengths of the vectors.</p>
<p><span class="math display"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><div class="MathJax_Display" style="text-align: center;"><span class="MathJax" id="MathJax-Element-7-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;><msubsup><mi>s</mi><mi>u</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>c</mi><mi>o</mi><mi>s</mi></mrow></msubsup><mo stretchy=&quot;false&quot;>(</mo><msub><mi>u</mi><mi>k</mi></msub><mo>,</mo><msub><mi>u</mi><mi>a</mi></msub><mo stretchy=&quot;false&quot;>)</mo><mo>=</mo><mfrac><mrow><msub><mi>u</mi><mi>k</mi></msub><mo>&amp;#x22C5;</mo><msub><mi>u</mi><mi>a</mi></msub></mrow><mrow><mrow><mo symmetric=&quot;true&quot;>&amp;#x2016;</mo><msub><mi>u</mi><mi>k</mi></msub><mo symmetric=&quot;true&quot;>&amp;#x2016;</mo></mrow><mrow><mo symmetric=&quot;true&quot;>&amp;#x2016;</mo><msub><mi>u</mi><mi>a</mi></msub><mo symmetric=&quot;true&quot;>&amp;#x2016;</mo></mrow></mrow></mfrac><mo>=</mo><mfrac><mrow><mo>&amp;#x2211;</mo><msub><mi>x</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>k</mi><mo>,</mo><mi>m</mi></mrow></msub><msub><mi>x</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>a</mi><mo>,</mo><mi>m</mi></mrow></msub></mrow><msqrt><mo>&amp;#x2211;</mo><msubsup><mi>x</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>k</mi><mo>,</mo><mi>m</mi></mrow><mn>2</mn></msubsup><mo>&amp;#x2211;</mo><msubsup><mi>x</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>a</mi><mo>,</mo><mi>m</mi></mrow><mn>2</mn></msubsup></msqrt></mfrac></math>" role="presentation" style="text-align: center; position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-23" style="width: 20.148em; display: inline-block;"><span style="display: inline-block; position: relative; width: 19.566em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(0.633em, 1019.57em, 4.177em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-24"><span class="msubsup" id="MathJax-Span-25"><span style="display: inline-block; position: relative; width: 1.507em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.44em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-26" style="font-family: MathJax_Math-italic;">s</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.546em, 1001.07em, 4.129em, -999.998em); top: -4.318em; left: 0.488em;"><span class="texatom" id="MathJax-Span-27"><span class="mrow" id="MathJax-Span-28"><span class="mi" id="MathJax-Span-29" style="font-size: 70.7%; font-family: MathJax_Math-italic;">c</span><span class="mi" id="MathJax-Span-30" style="font-size: 70.7%; font-family: MathJax_Math-italic;">o</span><span class="mi" id="MathJax-Span-31" style="font-size: 70.7%; font-family: MathJax_Math-italic;">s</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.546em, 1000.49em, 4.129em, -999.998em); top: -3.833em; left: 0.488em;"><span class="mi" id="MathJax-Span-32" style="font-size: 70.7%; font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-33" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-34"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-35" style="font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-36" style="font-size: 70.7%; font-family: MathJax_Math-italic;">k</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-37" style="font-family: MathJax_Main;">,</span><span class="msubsup" id="MathJax-Span-38" style="padding-left: 0.148em;"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-39" style="font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-40" style="font-size: 70.7%; font-family: MathJax_Math-italic;">a</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-41" style="font-family: MathJax_Main;">)</span><span class="mo" id="MathJax-Span-42" style="font-family: MathJax_Main; padding-left: 0.294em;">=</span><span class="mfrac" id="MathJax-Span-43" style="padding-left: 0.294em;"><span style="display: inline-block; position: relative; width: 4.323em; height: 0px; margin-right: 0.1em; margin-left: 0.1em;"><span style="position: absolute; clip: rect(3.4em, 1002.82em, 4.274em, -999.998em); top: -4.658em; left: 50%; margin-left: -1.405em;"><span class="mrow" id="MathJax-Span-44"><span class="msubsup" id="MathJax-Span-45"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-46" style="font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-47" style="font-size: 70.7%; font-family: MathJax_Math-italic;">k</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-48" style="font-family: MathJax_Main; padding-left: 0.245em;">⋅</span><span class="msubsup" id="MathJax-Span-49" style="padding-left: 0.245em;"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-50" style="font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-51" style="font-size: 70.7%; font-family: MathJax_Math-italic;">a</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.109em, 1004.03em, 4.371em, -999.998em); top: -3.25em; left: 50%; margin-left: -2.085em;"><span class="mrow" id="MathJax-Span-52"><span class="mrow" id="MathJax-Span-53"><span class="mo" id="MathJax-Span-54" style="font-family: MathJax_Main;">∥</span><span class="msubsup" id="MathJax-Span-55"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-56" style="font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-57" style="font-size: 70.7%; font-family: MathJax_Math-italic;">k</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-58" style="font-family: MathJax_Main;">∥</span></span><span class="mrow" id="MathJax-Span-59" style="padding-left: 0.148em;"><span class="mo" id="MathJax-Span-60" style="font-family: MathJax_Main;">∥</span><span class="msubsup" id="MathJax-Span-61"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-62" style="font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-63" style="font-size: 70.7%; font-family: MathJax_Math-italic;">a</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-64" style="font-family: MathJax_Main;">∥</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(0.876em, 1004.32em, 1.216em, -999.998em); top: -1.308em; left: 0em;"><span style="display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 4.323em; height: 0px;"></span><span style="display: inline-block; width: 0px; height: 1.07em;"></span></span></span></span><span class="mo" id="MathJax-Span-65" style="font-family: MathJax_Main; padding-left: 0.294em;">=</span><span class="mfrac" id="MathJax-Span-66" style="padding-left: 0.294em;"><span style="display: inline-block; position: relative; width: 7.381em; height: 0px; margin-right: 0.1em; margin-left: 0.1em;"><span style="position: absolute; clip: rect(3.109em, 1004.86em, 4.42em, -999.998em); top: -4.706em; left: 50%; margin-left: -2.425em;"><span class="mrow" id="MathJax-Span-67"><span class="mo" id="MathJax-Span-68" style="font-family: MathJax_Size1; vertical-align: 0em;">∑</span><span class="msubsup" id="MathJax-Span-69" style="padding-left: 0.148em;"><span style="display: inline-block; position: relative; width: 1.847em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-70" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="texatom" id="MathJax-Span-71"><span class="mrow" id="MathJax-Span-72"><span class="mi" id="MathJax-Span-73" style="font-size: 70.7%; font-family: MathJax_Math-italic;">k</span><span class="mo" id="MathJax-Span-74" style="font-size: 70.7%; font-family: MathJax_Main;">,</span><span class="mi" id="MathJax-Span-75" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="msubsup" id="MathJax-Span-76"><span style="display: inline-block; position: relative; width: 1.847em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-77" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="texatom" id="MathJax-Span-78"><span class="mrow" id="MathJax-Span-79"><span class="mi" id="MathJax-Span-80" style="font-size: 70.7%; font-family: MathJax_Math-italic;">a</span><span class="mo" id="MathJax-Span-81" style="font-size: 70.7%; font-family: MathJax_Main;">,</span><span class="mi" id="MathJax-Span-82" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(2.769em, 1007.24em, 4.857em, -999.998em); top: -2.91em; left: 50%; margin-left: -3.638em;"><span class="msqrt" id="MathJax-Span-83"><span style="display: inline-block; position: relative; width: 7.235em; height: 0px;"><span style="position: absolute; clip: rect(3.012em, 1006.22em, 4.614em, -999.998em); top: -3.978em; left: 1.022em;"><span class="mrow" id="MathJax-Span-84"><span class="mo" id="MathJax-Span-85" style="font-family: MathJax_Size1; vertical-align: 0em;">∑</span><span class="msubsup" id="MathJax-Span-86" style="padding-left: 0.148em;"><span style="display: inline-block; position: relative; width: 1.847em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-87" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.352em, 1000.44em, 4.129em, -999.998em); top: -4.318em; left: 0.585em;"><span class="mn" id="MathJax-Span-88" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.352em, 1001.26em, 4.274em, -999.998em); top: -3.638em; left: 0.585em;"><span class="texatom" id="MathJax-Span-89"><span class="mrow" id="MathJax-Span-90"><span class="mi" id="MathJax-Span-91" style="font-size: 70.7%; font-family: MathJax_Math-italic;">k</span><span class="mo" id="MathJax-Span-92" style="font-size: 70.7%; font-family: MathJax_Main;">,</span><span class="mi" id="MathJax-Span-93" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-94" style="font-family: MathJax_Size1; vertical-align: 0em; padding-left: 0.148em;">∑</span><span class="msubsup" id="MathJax-Span-95" style="padding-left: 0.148em;"><span style="display: inline-block; position: relative; width: 1.847em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-96" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.352em, 1000.44em, 4.129em, -999.998em); top: -4.318em; left: 0.585em;"><span class="mn" id="MathJax-Span-97" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.546em, 1001.26em, 4.274em, -999.998em); top: -3.833em; left: 0.585em;"><span class="texatom" id="MathJax-Span-98"><span class="mrow" id="MathJax-Span-99"><span class="mi" id="MathJax-Span-100" style="font-size: 70.7%; font-family: MathJax_Math-italic;">a</span><span class="mo" id="MathJax-Span-101" style="font-size: 70.7%; font-family: MathJax_Main;">,</span><span class="mi" id="MathJax-Span-102" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.546em, 1006.26em, 3.886em, -999.998em); top: -4.755em; left: 1.022em;"><span style="display: inline-block; position: relative; width: 6.265em; height: 0px;"><span style="position: absolute; font-family: MathJax_Main; top: -3.978em; left: -0.095em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; font-family: MathJax_Main; top: -3.978em; left: 5.585em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 0.391em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 0.925em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 1.459em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 1.944em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 2.478em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 2.964em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 3.498em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 4.032em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 4.517em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 5.051em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(2.672em, 1001.02em, 4.76em, -999.998em); top: -3.93em; left: 0em;"><span style="font-family: MathJax_Size2;">√</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(0.876em, 1007.38em, 1.216em, -999.998em); top: -1.308em; left: 0em;"><span style="display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 7.381em; height: 0px;"></span><span style="display: inline-block; width: 0px; height: 1.07em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -1.848em; border-left: 0px solid; width: 0px; height: 3.452em;"></span></span></nobr><span class="MJX_Assistive_MathML MJX_Assistive_MathML_Block" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><msubsup><mi>s</mi><mi>u</mi><mrow class="MJX-TeXAtom-ORD"><mi>c</mi><mi>o</mi><mi>s</mi></mrow></msubsup><mo stretchy="false">(</mo><msub><mi>u</mi><mi>k</mi></msub><mo>,</mo><msub><mi>u</mi><mi>a</mi></msub><mo stretchy="false">)</mo><mo>=</mo><mfrac><mrow><msub><mi>u</mi><mi>k</mi></msub><mo>⋅</mo><msub><mi>u</mi><mi>a</mi></msub></mrow><mrow><mrow><mo symmetric="true">‖</mo><msub><mi>u</mi><mi>k</mi></msub><mo symmetric="true">‖</mo></mrow><mrow><mo symmetric="true">‖</mo><msub><mi>u</mi><mi>a</mi></msub><mo symmetric="true">‖</mo></mrow></mrow></mfrac><mo>=</mo><mfrac><mrow><mo>∑</mo><msub><mi>x</mi><mrow class="MJX-TeXAtom-ORD"><mi>k</mi><mo>,</mo><mi>m</mi></mrow></msub><msub><mi>x</mi><mrow class="MJX-TeXAtom-ORD"><mi>a</mi><mo>,</mo><mi>m</mi></mrow></msub></mrow><msqrt><mo>∑</mo><msubsup><mi>x</mi><mrow class="MJX-TeXAtom-ORD"><mi>k</mi><mo>,</mo><mi>m</mi></mrow><mn>2</mn></msubsup><mo>∑</mo><msubsup><mi>x</mi><mrow class="MJX-TeXAtom-ORD"><mi>a</mi><mo>,</mo><mi>m</mi></mrow><mn>2</mn></msubsup></msqrt></mfrac></math></span></span></div><script type="math/tex; mode=display" id="MathJax-Element-7">
s_u^{cos}(u_k,u_a)
 = \frac{ u_k \cdot u_a }{ \left \| u_k \right \| \left \| u_a \right \| }
 = \frac{ \sum x_{k,m}x_{a,m} }{ \sqrt{\sum x_{k,m}^2\sum x_{a,m}^2} }
</script></span></p>
<p>To calculate similarity between items <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-8-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>m</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-103" style="width: 0.925em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.876em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.701em, 1000.88em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-104"><span class="mi" id="MathJax-Span-105" style="font-family: MathJax_Math-italic;">m</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.552em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi></math></span></span><script type="math/tex" id="MathJax-Element-8">m</script></span> and <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-9-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>b</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-106" style="width: 0.488em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.439em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.459em, 1000.44em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-107"><span class="mi" id="MathJax-Span-108" style="font-family: MathJax_Math-italic;">b</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.853em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>b</mi></math></span></span><script type="math/tex" id="MathJax-Element-9">b</script></span> you use the formula:</p>
<p><span class="math display"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><div class="MathJax_Display" style="text-align: center;"><span class="MathJax" id="MathJax-Element-10-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;><msubsup><mi>s</mi><mi>u</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>c</mi><mi>o</mi><mi>s</mi></mrow></msubsup><mo stretchy=&quot;false&quot;>(</mo><msub><mi>i</mi><mi>m</mi></msub><mo>,</mo><msub><mi>i</mi><mi>b</mi></msub><mo stretchy=&quot;false&quot;>)</mo><mo>=</mo><mfrac><mrow><msub><mi>i</mi><mi>m</mi></msub><mo>&amp;#x22C5;</mo><msub><mi>i</mi><mi>b</mi></msub></mrow><mrow><mrow><mo symmetric=&quot;true&quot;>&amp;#x2016;</mo><msub><mi>i</mi><mi>m</mi></msub><mo symmetric=&quot;true&quot;>&amp;#x2016;</mo></mrow><mrow><mo symmetric=&quot;true&quot;>&amp;#x2016;</mo><msub><mi>i</mi><mi>b</mi></msub><mo symmetric=&quot;true&quot;>&amp;#x2016;</mo></mrow></mrow></mfrac><mo>=</mo><mfrac><mrow><mo>&amp;#x2211;</mo><msub><mi>x</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>a</mi><mo>,</mo><mi>m</mi></mrow></msub><msub><mi>x</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>a</mi><mo>,</mo><mi>b</mi></mrow></msub></mrow><msqrt><mo>&amp;#x2211;</mo><msubsup><mi>x</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>a</mi><mo>,</mo><mi>m</mi></mrow><mn>2</mn></msubsup><mo>&amp;#x2211;</mo><msubsup><mi>x</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>a</mi><mo>,</mo><mi>b</mi></mrow><mn>2</mn></msubsup></msqrt></mfrac></math>" role="presentation" style="text-align: center; position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-109" style="width: 19.226em; display: inline-block;"><span style="display: inline-block; position: relative; width: 18.643em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(0.633em, 1018.64em, 4.177em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-110"><span class="msubsup" id="MathJax-Span-111"><span style="display: inline-block; position: relative; width: 1.507em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.44em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-112" style="font-family: MathJax_Math-italic;">s</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.546em, 1001.07em, 4.129em, -999.998em); top: -4.318em; left: 0.488em;"><span class="texatom" id="MathJax-Span-113"><span class="mrow" id="MathJax-Span-114"><span class="mi" id="MathJax-Span-115" style="font-size: 70.7%; font-family: MathJax_Math-italic;">c</span><span class="mi" id="MathJax-Span-116" style="font-size: 70.7%; font-family: MathJax_Math-italic;">o</span><span class="mi" id="MathJax-Span-117" style="font-size: 70.7%; font-family: MathJax_Math-italic;">s</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.546em, 1000.49em, 4.129em, -999.998em); top: -3.833em; left: 0.488em;"><span class="mi" id="MathJax-Span-118" style="font-size: 70.7%; font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-119" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-120"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.158em, 1000.29em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-121" style="font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.342em;"><span class="mi" id="MathJax-Span-122" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-123" style="font-family: MathJax_Main;">,</span><span class="msubsup" id="MathJax-Span-124" style="padding-left: 0.148em;"><span style="display: inline-block; position: relative; width: 0.731em; height: 0px;"><span style="position: absolute; clip: rect(3.158em, 1000.29em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-125" style="font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.342em;"><span class="mi" id="MathJax-Span-126" style="font-size: 70.7%; font-family: MathJax_Math-italic;">b</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-127" style="font-family: MathJax_Main;">)</span><span class="mo" id="MathJax-Span-128" style="font-family: MathJax_Main; padding-left: 0.294em;">=</span><span class="mfrac" id="MathJax-Span-129" style="padding-left: 0.294em;"><span style="display: inline-block; position: relative; width: 4.032em; height: 0px; margin-right: 0.1em; margin-left: 0.1em;"><span style="position: absolute; clip: rect(3.158em, 1002.53em, 4.274em, -999.998em); top: -4.658em; left: 50%; margin-left: -1.26em;"><span class="mrow" id="MathJax-Span-130"><span class="msubsup" id="MathJax-Span-131"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.158em, 1000.29em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-132" style="font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.342em;"><span class="mi" id="MathJax-Span-133" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-134" style="font-family: MathJax_Main; padding-left: 0.245em;">⋅</span><span class="msubsup" id="MathJax-Span-135" style="padding-left: 0.245em;"><span style="display: inline-block; position: relative; width: 0.731em; height: 0px;"><span style="position: absolute; clip: rect(3.158em, 1000.29em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-136" style="font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.342em;"><span class="mi" id="MathJax-Span-137" style="font-size: 70.7%; font-family: MathJax_Math-italic;">b</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.109em, 1003.79em, 4.371em, -999.998em); top: -3.25em; left: 50%; margin-left: -1.939em;"><span class="mrow" id="MathJax-Span-138"><span class="mrow" id="MathJax-Span-139"><span class="mo" id="MathJax-Span-140" style="font-family: MathJax_Main;">∥</span><span class="msubsup" id="MathJax-Span-141"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.158em, 1000.29em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-142" style="font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.342em;"><span class="mi" id="MathJax-Span-143" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-144" style="font-family: MathJax_Main;">∥</span></span><span class="mrow" id="MathJax-Span-145" style="padding-left: 0.148em;"><span class="mo" id="MathJax-Span-146" style="font-family: MathJax_Main;">∥</span><span class="msubsup" id="MathJax-Span-147"><span style="display: inline-block; position: relative; width: 0.731em; height: 0px;"><span style="position: absolute; clip: rect(3.158em, 1000.29em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-148" style="font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.342em;"><span class="mi" id="MathJax-Span-149" style="font-size: 70.7%; font-family: MathJax_Math-italic;">b</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-150" style="font-family: MathJax_Main;">∥</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(0.876em, 1004.03em, 1.216em, -999.998em); top: -1.308em; left: 0em;"><span style="display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 4.032em; height: 0px;"></span><span style="display: inline-block; width: 0px; height: 1.07em;"></span></span></span></span><span class="mo" id="MathJax-Span-151" style="font-family: MathJax_Main; padding-left: 0.294em;">=</span><span class="mfrac" id="MathJax-Span-152" style="padding-left: 0.294em;"><span style="display: inline-block; position: relative; width: 7.041em; height: 0px; margin-right: 0.1em; margin-left: 0.1em;"><span style="position: absolute; clip: rect(3.109em, 1004.57em, 4.42em, -999.998em); top: -4.706em; left: 50%; margin-left: -2.279em;"><span class="mrow" id="MathJax-Span-153"><span class="mo" id="MathJax-Span-154" style="font-family: MathJax_Size1; vertical-align: 0em;">∑</span><span class="msubsup" id="MathJax-Span-155" style="padding-left: 0.148em;"><span style="display: inline-block; position: relative; width: 1.847em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-156" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="texatom" id="MathJax-Span-157"><span class="mrow" id="MathJax-Span-158"><span class="mi" id="MathJax-Span-159" style="font-size: 70.7%; font-family: MathJax_Math-italic;">a</span><span class="mo" id="MathJax-Span-160" style="font-size: 70.7%; font-family: MathJax_Main;">,</span><span class="mi" id="MathJax-Span-161" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="msubsup" id="MathJax-Span-162"><span style="display: inline-block; position: relative; width: 1.507em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-163" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="texatom" id="MathJax-Span-164"><span class="mrow" id="MathJax-Span-165"><span class="mi" id="MathJax-Span-166" style="font-size: 70.7%; font-family: MathJax_Math-italic;">a</span><span class="mo" id="MathJax-Span-167" style="font-size: 70.7%; font-family: MathJax_Main;">,</span><span class="mi" id="MathJax-Span-168" style="font-size: 70.7%; font-family: MathJax_Math-italic;">b</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(2.769em, 1006.94em, 4.857em, -999.998em); top: -2.91em; left: 50%; margin-left: -3.444em;"><span class="msqrt" id="MathJax-Span-169"><span style="display: inline-block; position: relative; width: 6.944em; height: 0px;"><span style="position: absolute; clip: rect(3.012em, 1005.92em, 4.614em, -999.998em); top: -3.978em; left: 1.022em;"><span class="mrow" id="MathJax-Span-170"><span class="mo" id="MathJax-Span-171" style="font-family: MathJax_Size1; vertical-align: 0em;">∑</span><span class="msubsup" id="MathJax-Span-172" style="padding-left: 0.148em;"><span style="display: inline-block; position: relative; width: 1.847em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-173" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.352em, 1000.44em, 4.129em, -999.998em); top: -4.318em; left: 0.585em;"><span class="mn" id="MathJax-Span-174" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.546em, 1001.26em, 4.274em, -999.998em); top: -3.833em; left: 0.585em;"><span class="texatom" id="MathJax-Span-175"><span class="mrow" id="MathJax-Span-176"><span class="mi" id="MathJax-Span-177" style="font-size: 70.7%; font-family: MathJax_Math-italic;">a</span><span class="mo" id="MathJax-Span-178" style="font-size: 70.7%; font-family: MathJax_Main;">,</span><span class="mi" id="MathJax-Span-179" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-180" style="font-family: MathJax_Size1; vertical-align: 0em; padding-left: 0.148em;">∑</span><span class="msubsup" id="MathJax-Span-181" style="padding-left: 0.148em;"><span style="display: inline-block; position: relative; width: 1.507em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-182" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.352em, 1000.44em, 4.129em, -999.998em); top: -4.318em; left: 0.585em;"><span class="mn" id="MathJax-Span-183" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.352em, 1000.97em, 4.274em, -999.998em); top: -3.638em; left: 0.585em;"><span class="texatom" id="MathJax-Span-184"><span class="mrow" id="MathJax-Span-185"><span class="mi" id="MathJax-Span-186" style="font-size: 70.7%; font-family: MathJax_Math-italic;">a</span><span class="mo" id="MathJax-Span-187" style="font-size: 70.7%; font-family: MathJax_Main;">,</span><span class="mi" id="MathJax-Span-188" style="font-size: 70.7%; font-family: MathJax_Math-italic;">b</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.546em, 1005.92em, 3.886em, -999.998em); top: -4.755em; left: 1.022em;"><span style="display: inline-block; position: relative; width: 5.925em; height: 0px;"><span style="position: absolute; font-family: MathJax_Main; top: -3.978em; left: -0.095em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; font-family: MathJax_Main; top: -3.978em; left: 5.245em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 0.439em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 0.973em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 1.507em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 2.041em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 2.575em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 3.109em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 3.643em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 4.177em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 4.711em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(2.672em, 1001.02em, 4.76em, -999.998em); top: -3.93em; left: 0em;"><span style="font-family: MathJax_Size2;">√</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(0.876em, 1007.04em, 1.216em, -999.998em); top: -1.308em; left: 0em;"><span style="display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 7.041em; height: 0px;"></span><span style="display: inline-block; width: 0px; height: 1.07em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -1.848em; border-left: 0px solid; width: 0px; height: 3.452em;"></span></span></nobr><span class="MJX_Assistive_MathML MJX_Assistive_MathML_Block" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><msubsup><mi>s</mi><mi>u</mi><mrow class="MJX-TeXAtom-ORD"><mi>c</mi><mi>o</mi><mi>s</mi></mrow></msubsup><mo stretchy="false">(</mo><msub><mi>i</mi><mi>m</mi></msub><mo>,</mo><msub><mi>i</mi><mi>b</mi></msub><mo stretchy="false">)</mo><mo>=</mo><mfrac><mrow><msub><mi>i</mi><mi>m</mi></msub><mo>⋅</mo><msub><mi>i</mi><mi>b</mi></msub></mrow><mrow><mrow><mo symmetric="true">‖</mo><msub><mi>i</mi><mi>m</mi></msub><mo symmetric="true">‖</mo></mrow><mrow><mo symmetric="true">‖</mo><msub><mi>i</mi><mi>b</mi></msub><mo symmetric="true">‖</mo></mrow></mrow></mfrac><mo>=</mo><mfrac><mrow><mo>∑</mo><msub><mi>x</mi><mrow class="MJX-TeXAtom-ORD"><mi>a</mi><mo>,</mo><mi>m</mi></mrow></msub><msub><mi>x</mi><mrow class="MJX-TeXAtom-ORD"><mi>a</mi><mo>,</mo><mi>b</mi></mrow></msub></mrow><msqrt><mo>∑</mo><msubsup><mi>x</mi><mrow class="MJX-TeXAtom-ORD"><mi>a</mi><mo>,</mo><mi>m</mi></mrow><mn>2</mn></msubsup><mo>∑</mo><msubsup><mi>x</mi><mrow class="MJX-TeXAtom-ORD"><mi>a</mi><mo>,</mo><mi>b</mi></mrow><mn>2</mn></msubsup></msqrt></mfrac></math></span></span></div><script type="math/tex; mode=display" id="MathJax-Element-10">
s_u^{cos}(i_m,i_b)
 = \frac{ i_m \cdot i_b }{ \left \| i_m \right \| \left \| i_b \right \| }
 = \frac{ \sum x_{a,m} x_{a,b} }{ \sqrt{ \sum x_{a,m}^2 \sum x_{a,b}^2 } }
</script></span></p>
<p>Your first step will be to create the user-item matrix. Since you have both testing and training data you need to create two matrices.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="co">#Create two user-item matrices, one for training and another for testing</span>
train_data_matrix <span class="op">=</span> np.zeros((n_users, n_items))
<span class="cf">for</span> line <span class="op">in</span> train_data.itertuples():
    train_data_matrix[line[<span class="dv">1</span>]<span class="op">-</span><span class="dv">1</span>, line[<span class="dv">2</span>]<span class="op">-</span><span class="dv">1</span>] <span class="op">=</span> line[<span class="dv">3</span>]

test_data_matrix <span class="op">=</span> np.zeros((n_users, n_items))
<span class="cf">for</span> line <span class="op">in</span> test_data.itertuples():
    test_data_matrix[line[<span class="dv">1</span>]<span class="op">-</span><span class="dv">1</span>, line[<span class="dv">2</span>]<span class="op">-</span><span class="dv">1</span>] <span class="op">=</span> line[<span class="dv">3</span>]</code></pre></div>
<p>You can use the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.metrics.pairwise.pairwise_distances.html"><code>pairwise_distances</code></a> function from <code>sklearn</code> to calculate the cosine similarity. Note, the output will range from 0 to 1 since the ratings are all positive.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">from</span> sklearn.metrics.pairwise <span class="im">import</span> pairwise_distances
user_similarity <span class="op">=</span> pairwise_distances(train_data_matrix, metric<span class="op">=</span><span class="st">'cosine'</span>)
item_similarity <span class="op">=</span> pairwise_distances(train_data_matrix.T, metric<span class="op">=</span><span class="st">'cosine'</span>)</code></pre></div>
<p>Next step is to make predictions. You have already created similarity matrices: <code>user_similarity</code> and <code>item_similarity</code> and therefore you can make a prediction by applying following formula for user-based CF:</p>
<p><span class="math display"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><div class="MathJax_Display" style="text-align: center;"><span class="MathJax" id="MathJax-Element-11-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;><msub><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mover><mi>x</mi><mo stretchy=&quot;false&quot;>&amp;#x005E;</mo></mover></mrow><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>k</mi><mo>,</mo><mi>m</mi></mrow></msub><mo>=</mo><msub><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mover><mi>x</mi><mo stretchy=&quot;false&quot;>&amp;#x00AF;</mo></mover></mrow><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>k</mi></mrow></msub><mo>+</mo><mfrac><mrow><munder><mo movablelimits=&quot;false&quot;>&amp;#x2211;</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><msub><mi>u</mi><mi>a</mi></msub></mrow></munder><mi>s</mi><mi>i</mi><msub><mi>m</mi><mi>u</mi></msub><mo stretchy=&quot;false&quot;>(</mo><msub><mi>u</mi><mi>k</mi></msub><mo>,</mo><msub><mi>u</mi><mi>a</mi></msub><mo stretchy=&quot;false&quot;>)</mo><mo stretchy=&quot;false&quot;>(</mo><msub><mi>x</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>a</mi><mo>,</mo><mi>m</mi></mrow></msub><mo>&amp;#x2212;</mo><msub><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mover><mi>x</mi><mo stretchy=&quot;false&quot;>&amp;#x00AF;</mo></mover></mrow><mrow class=&quot;MJX-TeXAtom-ORD&quot;><msub><mi>u</mi><mi>a</mi></msub></mrow></msub><mo stretchy=&quot;false&quot;>)</mo></mrow><mrow><munder><mo movablelimits=&quot;false&quot;>&amp;#x2211;</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><msub><mi>u</mi><mi>a</mi></msub></mrow></munder><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo stretchy=&quot;false&quot;>|</mo></mrow><mi>s</mi><mi>i</mi><msub><mi>m</mi><mi>u</mi></msub><mo stretchy=&quot;false&quot;>(</mo><msub><mi>u</mi><mi>k</mi></msub><mo>,</mo><msub><mi>u</mi><mi>a</mi></msub><mo stretchy=&quot;false&quot;>)</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo stretchy=&quot;false&quot;>|</mo></mrow></mrow></mfrac></math>" role="presentation" style="text-align: center; position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-189" style="width: 18.206em; display: inline-block;"><span style="display: inline-block; position: relative; width: 17.672em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(-0.143em, 1017.67em, 4.226em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-190"><span class="msubsup" id="MathJax-Span-191"><span style="display: inline-block; position: relative; width: 1.847em; height: 0px;"><span style="position: absolute; clip: rect(3.109em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="texatom" id="MathJax-Span-192"><span class="mrow" id="MathJax-Span-193"><span class="munderover" id="MathJax-Span-194"><span style="display: inline-block; position: relative; width: 0.585em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-195" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.158em, 1000.39em, 3.595em, -999.998em); top: -4.027em; left: 0.051em;"><span class="mo" id="MathJax-Span-196" style="font-family: MathJax_Main;">^</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="texatom" id="MathJax-Span-197"><span class="mrow" id="MathJax-Span-198"><span class="mi" id="MathJax-Span-199" style="font-size: 70.7%; font-family: MathJax_Math-italic;">k</span><span class="mo" id="MathJax-Span-200" style="font-size: 70.7%; font-family: MathJax_Main;">,</span><span class="mi" id="MathJax-Span-201" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-202" style="font-family: MathJax_Main; padding-left: 0.294em;">=</span><span class="msubsup" id="MathJax-Span-203" style="padding-left: 0.294em;"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.206em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="texatom" id="MathJax-Span-204"><span class="mrow" id="MathJax-Span-205"><span class="munderover" id="MathJax-Span-206"><span style="display: inline-block; position: relative; width: 0.585em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-207" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.255em, 1000.44em, 3.595em, -999.998em); top: -3.978em; left: 0.051em;"><span class="mo" id="MathJax-Span-208" style="font-family: MathJax_Main;">¯</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="texatom" id="MathJax-Span-209"><span class="mrow" id="MathJax-Span-210"><span class="mi" id="MathJax-Span-211" style="font-size: 70.7%; font-family: MathJax_Math-italic;">k</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-212" style="font-family: MathJax_Main; padding-left: 0.245em;">+</span><span class="mfrac" id="MathJax-Span-213" style="padding-left: 0.245em;"><span style="display: inline-block; position: relative; width: 11.993em; height: 0px; margin-right: 0.1em; margin-left: 0.1em;"><span style="position: absolute; clip: rect(3.109em, 1011.8em, 5.197em, -999.998em); top: -5.483em; left: 50%; margin-left: -5.92em;"><span class="mrow" id="MathJax-Span-214"><span class="munderover" id="MathJax-Span-215"><span style="display: inline-block; position: relative; width: 1.07em; height: 0px;"><span style="position: absolute; clip: rect(3.109em, 1001.02em, 4.371em, -999.998em); top: -3.978em; left: 0em;"><span class="mo" id="MathJax-Span-216" style="font-family: MathJax_Size1; vertical-align: 0em;">∑</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.546em, 1000.73em, 4.323em, -999.998em); top: -3.104em; left: 0.148em;"><span class="texatom" id="MathJax-Span-217"><span class="mrow" id="MathJax-Span-218"><span class="msubsup" id="MathJax-Span-219"><span style="display: inline-block; position: relative; width: 0.731em; height: 0px;"><span style="position: absolute; clip: rect(3.546em, 1000.39em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-220" style="font-size: 70.7%; font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.881em; left: 0.391em;"><span class="mi" id="MathJax-Span-221" style="font-size: 50%; font-family: MathJax_Math-italic;">a</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mi" id="MathJax-Span-222" style="font-family: MathJax_Math-italic; padding-left: 0.148em;">s</span><span class="mi" id="MathJax-Span-223" style="font-family: MathJax_Math-italic;">i</span><span class="msubsup" id="MathJax-Span-224"><span style="display: inline-block; position: relative; width: 1.362em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.88em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-225" style="font-family: MathJax_Math-italic;">m</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.876em;"><span class="mi" id="MathJax-Span-226" style="font-size: 70.7%; font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-227" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-228"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-229" style="font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-230" style="font-size: 70.7%; font-family: MathJax_Math-italic;">k</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-231" style="font-family: MathJax_Main;">,</span><span class="msubsup" id="MathJax-Span-232" style="padding-left: 0.148em;"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-233" style="font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-234" style="font-size: 70.7%; font-family: MathJax_Math-italic;">a</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-235" style="font-family: MathJax_Main;">)</span><span class="mo" id="MathJax-Span-236" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-237"><span style="display: inline-block; position: relative; width: 1.847em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-238" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="texatom" id="MathJax-Span-239"><span class="mrow" id="MathJax-Span-240"><span class="mi" id="MathJax-Span-241" style="font-size: 70.7%; font-family: MathJax_Math-italic;">a</span><span class="mo" id="MathJax-Span-242" style="font-size: 70.7%; font-family: MathJax_Main;">,</span><span class="mi" id="MathJax-Span-243" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-244" style="font-family: MathJax_Main; padding-left: 0.245em;">−</span><span class="msubsup" id="MathJax-Span-245" style="padding-left: 0.245em;"><span style="display: inline-block; position: relative; width: 1.362em; height: 0px;"><span style="position: absolute; clip: rect(3.206em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="texatom" id="MathJax-Span-246"><span class="mrow" id="MathJax-Span-247"><span class="munderover" id="MathJax-Span-248"><span style="display: inline-block; position: relative; width: 0.585em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-249" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.255em, 1000.44em, 3.595em, -999.998em); top: -3.978em; left: 0.051em;"><span class="mo" id="MathJax-Span-250" style="font-family: MathJax_Main;">¯</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="texatom" id="MathJax-Span-251"><span class="mrow" id="MathJax-Span-252"><span class="msubsup" id="MathJax-Span-253"><span style="display: inline-block; position: relative; width: 0.731em; height: 0px;"><span style="position: absolute; clip: rect(3.546em, 1000.39em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-254" style="font-size: 70.7%; font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.881em; left: 0.391em;"><span class="mi" id="MathJax-Span-255" style="font-size: 50%; font-family: MathJax_Math-italic;">a</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-256" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.109em, 1007.04em, 5.197em, -999.998em); top: -3.25em; left: 50%; margin-left: -3.59em;"><span class="mrow" id="MathJax-Span-257"><span class="munderover" id="MathJax-Span-258"><span style="display: inline-block; position: relative; width: 1.07em; height: 0px;"><span style="position: absolute; clip: rect(3.109em, 1001.02em, 4.371em, -999.998em); top: -3.978em; left: 0em;"><span class="mo" id="MathJax-Span-259" style="font-family: MathJax_Size1; vertical-align: 0em;">∑</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.546em, 1000.73em, 4.323em, -999.998em); top: -3.104em; left: 0.148em;"><span class="texatom" id="MathJax-Span-260"><span class="mrow" id="MathJax-Span-261"><span class="msubsup" id="MathJax-Span-262"><span style="display: inline-block; position: relative; width: 0.731em; height: 0px;"><span style="position: absolute; clip: rect(3.546em, 1000.39em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-263" style="font-size: 70.7%; font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.881em; left: 0.391em;"><span class="mi" id="MathJax-Span-264" style="font-size: 50%; font-family: MathJax_Math-italic;">a</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="texatom" id="MathJax-Span-265" style="padding-left: 0.148em;"><span class="mrow" id="MathJax-Span-266"><span class="mo" id="MathJax-Span-267" style="font-family: MathJax_Main;">|</span></span></span><span class="mi" id="MathJax-Span-268" style="font-family: MathJax_Math-italic;">s</span><span class="mi" id="MathJax-Span-269" style="font-family: MathJax_Math-italic;">i</span><span class="msubsup" id="MathJax-Span-270"><span style="display: inline-block; position: relative; width: 1.362em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.88em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-271" style="font-family: MathJax_Math-italic;">m</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.876em;"><span class="mi" id="MathJax-Span-272" style="font-size: 70.7%; font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-273" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-274"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-275" style="font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-276" style="font-size: 70.7%; font-family: MathJax_Math-italic;">k</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-277" style="font-family: MathJax_Main;">,</span><span class="msubsup" id="MathJax-Span-278" style="padding-left: 0.148em;"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-279" style="font-family: MathJax_Math-italic;">u</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-280" style="font-size: 70.7%; font-family: MathJax_Math-italic;">a</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-281" style="font-family: MathJax_Main;">)</span><span class="texatom" id="MathJax-Span-282"><span class="mrow" id="MathJax-Span-283"><span class="mo" id="MathJax-Span-284" style="font-family: MathJax_Main;">|</span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(0.876em, 1011.99em, 1.216em, -999.998em); top: -1.308em; left: 0em;"><span style="display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 11.993em; height: 0px;"></span><span style="display: inline-block; width: 0px; height: 1.07em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -1.898em; border-left: 0px solid; width: 0px; height: 4.252em;"></span></span></nobr><span class="MJX_Assistive_MathML MJX_Assistive_MathML_Block" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><msub><mrow class="MJX-TeXAtom-ORD"><mover><mi>x</mi><mo stretchy="false">^</mo></mover></mrow><mrow class="MJX-TeXAtom-ORD"><mi>k</mi><mo>,</mo><mi>m</mi></mrow></msub><mo>=</mo><msub><mrow class="MJX-TeXAtom-ORD"><mover><mi>x</mi><mo stretchy="false">¯</mo></mover></mrow><mrow class="MJX-TeXAtom-ORD"><mi>k</mi></mrow></msub><mo>+</mo><mfrac><mrow><munder><mo movablelimits="false">∑</mo><mrow class="MJX-TeXAtom-ORD"><msub><mi>u</mi><mi>a</mi></msub></mrow></munder><mi>s</mi><mi>i</mi><msub><mi>m</mi><mi>u</mi></msub><mo stretchy="false">(</mo><msub><mi>u</mi><mi>k</mi></msub><mo>,</mo><msub><mi>u</mi><mi>a</mi></msub><mo stretchy="false">)</mo><mo stretchy="false">(</mo><msub><mi>x</mi><mrow class="MJX-TeXAtom-ORD"><mi>a</mi><mo>,</mo><mi>m</mi></mrow></msub><mo>−</mo><msub><mrow class="MJX-TeXAtom-ORD"><mover><mi>x</mi><mo stretchy="false">¯</mo></mover></mrow><mrow class="MJX-TeXAtom-ORD"><msub><mi>u</mi><mi>a</mi></msub></mrow></msub><mo stretchy="false">)</mo></mrow><mrow><munder><mo movablelimits="false">∑</mo><mrow class="MJX-TeXAtom-ORD"><msub><mi>u</mi><mi>a</mi></msub></mrow></munder><mrow class="MJX-TeXAtom-ORD"><mo stretchy="false">|</mo></mrow><mi>s</mi><mi>i</mi><msub><mi>m</mi><mi>u</mi></msub><mo stretchy="false">(</mo><msub><mi>u</mi><mi>k</mi></msub><mo>,</mo><msub><mi>u</mi><mi>a</mi></msub><mo stretchy="false">)</mo><mrow class="MJX-TeXAtom-ORD"><mo stretchy="false">|</mo></mrow></mrow></mfrac></math></span></span></div><script type="math/tex; mode=display" id="MathJax-Element-11">
\hat{x}_{k,m} = \bar{x}_{k} + \frac{\sum\limits_{u_a} sim_u(u_k, u_a) (x_{a,m} - \bar{x}_{u_a})}{\sum\limits_{u_a}|sim_u(u_k, u_a)|}
</script></span></p>
<p>You can look at the similarity between users <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-12-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>k</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-285" style="width: 0.585em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.536em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.459em, 1000.54em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-286"><span class="mi" id="MathJax-Span-287" style="font-family: MathJax_Math-italic;">k</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.853em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi></math></span></span><script type="math/tex" id="MathJax-Element-12">k</script></span> and <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-13-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>a</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-288" style="width: 0.585em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.536em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.701em, 1000.54em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-289"><span class="mi" id="MathJax-Span-290" style="font-family: MathJax_Math-italic;">a</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.552em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi></math></span></span><script type="math/tex" id="MathJax-Element-13">a</script></span> as weights that are multiplied by the ratings of a similar user <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-14-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>a</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-291" style="width: 0.585em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.536em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.701em, 1000.54em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-292"><span class="mi" id="MathJax-Span-293" style="font-family: MathJax_Math-italic;">a</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.552em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>a</mi></math></span></span><script type="math/tex" id="MathJax-Element-14">a</script></span> (corrected for the average rating of that user). You will need to normalize it so that the ratings stay between 1 and 5 and, as a final step, sum the average ratings for the user that you are trying to predict.</p>
<p>The idea here is that some users may tend always to give high or low ratings to all movies. The relative difference in the ratings that these users give is more important than the absolute values. To give an example: suppose, user <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-15-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>k</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-294" style="width: 0.585em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.536em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.459em, 1000.54em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-295"><span class="mi" id="MathJax-Span-296" style="font-family: MathJax_Math-italic;">k</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.853em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>k</mi></math></span></span><script type="math/tex" id="MathJax-Element-15">k</script></span> gives 4 stars to his favourite movies and 3 stars to all other good movies. Suppose now that another user <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-16-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>t</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-297" style="width: 0.342em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.342em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.507em, 1000.29em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-298"><span class="mi" id="MathJax-Span-299" style="font-family: MathJax_Math-italic;">t</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.753em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>t</mi></math></span></span><script type="math/tex" id="MathJax-Element-16">t</script></span> rates movies that he/she likes with 5 stars, and the movies he/she fell asleep over with 3 stars. These two users could have a very similar taste but treat the rating system differently.</p>
<p>When making a prediction for item-based CF you don't need to correct for users average rating since query user itself is used to do predictions.</p>
<p><span class="math display"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><div class="MathJax_Display" style="text-align: center;"><span class="MathJax" id="MathJax-Element-17-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;><msub><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mover><mi>x</mi><mo stretchy=&quot;false&quot;>&amp;#x005E;</mo></mover></mrow><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>k</mi><mo>,</mo><mi>m</mi></mrow></msub><mo>=</mo><mfrac><mrow><munder><mo movablelimits=&quot;false&quot;>&amp;#x2211;</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><msub><mi>i</mi><mi>b</mi></msub></mrow></munder><mi>s</mi><mi>i</mi><msub><mi>m</mi><mi>i</mi></msub><mo stretchy=&quot;false&quot;>(</mo><msub><mi>i</mi><mi>m</mi></msub><mo>,</mo><msub><mi>i</mi><mi>b</mi></msub><mo stretchy=&quot;false&quot;>)</mo><mo stretchy=&quot;false&quot;>(</mo><msub><mi>x</mi><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi>k</mi><mo>,</mo><mi>b</mi></mrow></msub><mo stretchy=&quot;false&quot;>)</mo></mrow><mrow><munder><mo movablelimits=&quot;false&quot;>&amp;#x2211;</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><msub><mi>i</mi><mi>b</mi></msub></mrow></munder><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo stretchy=&quot;false&quot;>|</mo></mrow><mi>s</mi><mi>i</mi><msub><mi>m</mi><mi>i</mi></msub><mo stretchy=&quot;false&quot;>(</mo><msub><mi>i</mi><mi>m</mi></msub><mo>,</mo><msub><mi>i</mi><mi>b</mi></msub><mo stretchy=&quot;false&quot;>)</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mo stretchy=&quot;false&quot;>|</mo></mrow></mrow></mfrac></math>" role="presentation" style="text-align: center; position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-300" style="width: 12.381em; display: inline-block;"><span style="display: inline-block; position: relative; width: 11.993em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(-0.192em, 1011.99em, 4.226em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-301"><span class="msubsup" id="MathJax-Span-302"><span style="display: inline-block; position: relative; width: 1.847em; height: 0px;"><span style="position: absolute; clip: rect(3.109em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="texatom" id="MathJax-Span-303"><span class="mrow" id="MathJax-Span-304"><span class="munderover" id="MathJax-Span-305"><span style="display: inline-block; position: relative; width: 0.585em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-306" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.158em, 1000.39em, 3.595em, -999.998em); top: -4.027em; left: 0.051em;"><span class="mo" id="MathJax-Span-307" style="font-family: MathJax_Main;">^</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="texatom" id="MathJax-Span-308"><span class="mrow" id="MathJax-Span-309"><span class="mi" id="MathJax-Span-310" style="font-size: 70.7%; font-family: MathJax_Math-italic;">k</span><span class="mo" id="MathJax-Span-311" style="font-size: 70.7%; font-family: MathJax_Main;">,</span><span class="mi" id="MathJax-Span-312" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-313" style="font-family: MathJax_Main; padding-left: 0.294em;">=</span><span class="mfrac" id="MathJax-Span-314" style="padding-left: 0.294em;"><span style="display: inline-block; position: relative; width: 8.595em; height: 0px; margin-right: 0.1em; margin-left: 0.1em;"><span style="position: absolute; clip: rect(3.109em, 1008.4em, 5.245em, -999.998em); top: -5.532em; left: 50%; margin-left: -4.221em;"><span class="mrow" id="MathJax-Span-315"><span class="munderover" id="MathJax-Span-316"><span style="display: inline-block; position: relative; width: 1.07em; height: 0px;"><span style="position: absolute; clip: rect(3.109em, 1001.02em, 4.371em, -999.998em); top: -3.978em; left: 0em;"><span class="mo" id="MathJax-Span-317" style="font-family: MathJax_Size1; vertical-align: 0em;">∑</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.352em, 1000.54em, 4.323em, -999.998em); top: -3.104em; left: 0.294em;"><span class="texatom" id="MathJax-Span-318"><span class="mrow" id="MathJax-Span-319"><span class="msubsup" id="MathJax-Span-320"><span style="display: inline-block; position: relative; width: 0.536em; height: 0px;"><span style="position: absolute; clip: rect(3.352em, 1000.2em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-321" style="font-size: 70.7%; font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.881em; left: 0.245em;"><span class="mi" id="MathJax-Span-322" style="font-size: 50%; font-family: MathJax_Math-italic;">b</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mi" id="MathJax-Span-323" style="font-family: MathJax_Math-italic; padding-left: 0.148em;">s</span><span class="mi" id="MathJax-Span-324" style="font-family: MathJax_Math-italic;">i</span><span class="msubsup" id="MathJax-Span-325"><span style="display: inline-block; position: relative; width: 1.216em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.88em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-326" style="font-family: MathJax_Math-italic;">m</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.876em;"><span class="mi" id="MathJax-Span-327" style="font-size: 70.7%; font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-328" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-329"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.158em, 1000.29em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-330" style="font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.342em;"><span class="mi" id="MathJax-Span-331" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-332" style="font-family: MathJax_Main;">,</span><span class="msubsup" id="MathJax-Span-333" style="padding-left: 0.148em;"><span style="display: inline-block; position: relative; width: 0.731em; height: 0px;"><span style="position: absolute; clip: rect(3.158em, 1000.29em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-334" style="font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.342em;"><span class="mi" id="MathJax-Span-335" style="font-size: 70.7%; font-family: MathJax_Math-italic;">b</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-336" style="font-family: MathJax_Main;">)</span><span class="mo" id="MathJax-Span-337" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-338"><span style="display: inline-block; position: relative; width: 1.507em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-339" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="texatom" id="MathJax-Span-340"><span class="mrow" id="MathJax-Span-341"><span class="mi" id="MathJax-Span-342" style="font-size: 70.7%; font-family: MathJax_Math-italic;">k</span><span class="mo" id="MathJax-Span-343" style="font-size: 70.7%; font-family: MathJax_Main;">,</span><span class="mi" id="MathJax-Span-344" style="font-size: 70.7%; font-family: MathJax_Math-italic;">b</span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-345" style="font-family: MathJax_Main;">)</span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.109em, 1006.6em, 5.245em, -999.998em); top: -3.25em; left: 50%; margin-left: -3.347em;"><span class="mrow" id="MathJax-Span-346"><span class="munderover" id="MathJax-Span-347"><span style="display: inline-block; position: relative; width: 1.07em; height: 0px;"><span style="position: absolute; clip: rect(3.109em, 1001.02em, 4.371em, -999.998em); top: -3.978em; left: 0em;"><span class="mo" id="MathJax-Span-348" style="font-family: MathJax_Size1; vertical-align: 0em;">∑</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.352em, 1000.54em, 4.323em, -999.998em); top: -3.104em; left: 0.294em;"><span class="texatom" id="MathJax-Span-349"><span class="mrow" id="MathJax-Span-350"><span class="msubsup" id="MathJax-Span-351"><span style="display: inline-block; position: relative; width: 0.536em; height: 0px;"><span style="position: absolute; clip: rect(3.352em, 1000.2em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-352" style="font-size: 70.7%; font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.881em; left: 0.245em;"><span class="mi" id="MathJax-Span-353" style="font-size: 50%; font-family: MathJax_Math-italic;">b</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="texatom" id="MathJax-Span-354" style="padding-left: 0.148em;"><span class="mrow" id="MathJax-Span-355"><span class="mo" id="MathJax-Span-356" style="font-family: MathJax_Main;">|</span></span></span><span class="mi" id="MathJax-Span-357" style="font-family: MathJax_Math-italic;">s</span><span class="mi" id="MathJax-Span-358" style="font-family: MathJax_Math-italic;">i</span><span class="msubsup" id="MathJax-Span-359"><span style="display: inline-block; position: relative; width: 1.216em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.88em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-360" style="font-family: MathJax_Math-italic;">m</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.876em;"><span class="mi" id="MathJax-Span-361" style="font-size: 70.7%; font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-362" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-363"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px;"><span style="position: absolute; clip: rect(3.158em, 1000.29em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-364" style="font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.342em;"><span class="mi" id="MathJax-Span-365" style="font-size: 70.7%; font-family: MathJax_Math-italic;">m</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-366" style="font-family: MathJax_Main;">,</span><span class="msubsup" id="MathJax-Span-367" style="padding-left: 0.148em;"><span style="display: inline-block; position: relative; width: 0.731em; height: 0px;"><span style="position: absolute; clip: rect(3.158em, 1000.29em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-368" style="font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.342em;"><span class="mi" id="MathJax-Span-369" style="font-size: 70.7%; font-family: MathJax_Math-italic;">b</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-370" style="font-family: MathJax_Main;">)</span><span class="texatom" id="MathJax-Span-371"><span class="mrow" id="MathJax-Span-372"><span class="mo" id="MathJax-Span-373" style="font-family: MathJax_Main;">|</span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(0.876em, 1008.6em, 1.216em, -999.998em); top: -1.308em; left: 0em;"><span style="display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 8.595em; height: 0px;"></span><span style="display: inline-block; width: 0px; height: 1.07em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -1.898em; border-left: 0px solid; width: 0px; height: 4.353em;"></span></span></nobr><span class="MJX_Assistive_MathML MJX_Assistive_MathML_Block" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><msub><mrow class="MJX-TeXAtom-ORD"><mover><mi>x</mi><mo stretchy="false">^</mo></mover></mrow><mrow class="MJX-TeXAtom-ORD"><mi>k</mi><mo>,</mo><mi>m</mi></mrow></msub><mo>=</mo><mfrac><mrow><munder><mo movablelimits="false">∑</mo><mrow class="MJX-TeXAtom-ORD"><msub><mi>i</mi><mi>b</mi></msub></mrow></munder><mi>s</mi><mi>i</mi><msub><mi>m</mi><mi>i</mi></msub><mo stretchy="false">(</mo><msub><mi>i</mi><mi>m</mi></msub><mo>,</mo><msub><mi>i</mi><mi>b</mi></msub><mo stretchy="false">)</mo><mo stretchy="false">(</mo><msub><mi>x</mi><mrow class="MJX-TeXAtom-ORD"><mi>k</mi><mo>,</mo><mi>b</mi></mrow></msub><mo stretchy="false">)</mo></mrow><mrow><munder><mo movablelimits="false">∑</mo><mrow class="MJX-TeXAtom-ORD"><msub><mi>i</mi><mi>b</mi></msub></mrow></munder><mrow class="MJX-TeXAtom-ORD"><mo stretchy="false">|</mo></mrow><mi>s</mi><mi>i</mi><msub><mi>m</mi><mi>i</mi></msub><mo stretchy="false">(</mo><msub><mi>i</mi><mi>m</mi></msub><mo>,</mo><msub><mi>i</mi><mi>b</mi></msub><mo stretchy="false">)</mo><mrow class="MJX-TeXAtom-ORD"><mo stretchy="false">|</mo></mrow></mrow></mfrac></math></span></span></div><script type="math/tex; mode=display" id="MathJax-Element-17">
\hat{x}_{k,m} = \frac{\sum\limits_{i_b} sim_i(i_m, i_b) (x_{k,b}) }{\sum\limits_{i_b}|sim_i(i_m, i_b)|}
</script></span></p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="kw">def</span> predict(ratings, similarity, <span class="bu">type</span><span class="op">=</span><span class="st">'user'</span>):
    <span class="cf">if</span> <span class="bu">type</span> <span class="op">==</span> <span class="st">'user'</span>:
        mean_user_rating <span class="op">=</span> ratings.mean(axis<span class="op">=</span><span class="dv">1</span>)
        <span class="co">#You use np.newaxis so that mean_user_rating has same format as ratings</span>
        ratings_diff <span class="op">=</span> (ratings <span class="op">-</span> mean_user_rating[:, np.newaxis])
        pred <span class="op">=</span> mean_user_rating[:, np.newaxis] <span class="op">+</span> similarity.dot(ratings_diff) <span class="op">/</span> np.array([np.<span class="bu">abs</span>(similarity).<span class="bu">sum</span>(axis<span class="op">=</span><span class="dv">1</span>)]).T
    <span class="cf">elif</span> <span class="bu">type</span> <span class="op">==</span> <span class="st">'item'</span>:
        pred <span class="op">=</span> ratings.dot(similarity) <span class="op">/</span> np.array([np.<span class="bu">abs</span>(similarity).<span class="bu">sum</span>(axis<span class="op">=</span><span class="dv">1</span>)])
    <span class="cf">return</span> pred</code></pre></div>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">item_prediction <span class="op">=</span> predict(train_data_matrix, item_similarity, <span class="bu">type</span><span class="op">=</span><span class="st">'item'</span>)
user_prediction <span class="op">=</span> predict(train_data_matrix, user_similarity, <span class="bu">type</span><span class="op">=</span><span class="st">'user'</span>)</code></pre></div>
<h3 id="evaluation">Evaluation</h3>
<p>There are many evaluation metrics but one of the most popular metric used to evaluate accuracy of predicted ratings is <em>Root Mean Squared Error (RMSE)</em>.</p>
<p><span class="math display"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><div class="MathJax_Display" style="text-align: center;"><span class="MathJax" id="MathJax-Element-18-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot; display=&quot;block&quot;><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mi class=&quot;MJX-tex-mathit&quot; mathvariant=&quot;italic&quot;>R</mi><mi class=&quot;MJX-tex-mathit&quot; mathvariant=&quot;italic&quot;>M</mi><mi class=&quot;MJX-tex-mathit&quot; mathvariant=&quot;italic&quot;>S</mi><mi class=&quot;MJX-tex-mathit&quot; mathvariant=&quot;italic&quot;>E</mi></mrow><mo>=</mo><msqrt><mfrac><mn>1</mn><mi>N</mi></mfrac><mo>&amp;#x2211;</mo><mo stretchy=&quot;false&quot;>(</mo><msub><mi>x</mi><mi>i</mi></msub><mo>&amp;#x2212;</mo><mrow class=&quot;MJX-TeXAtom-ORD&quot;><mover><msub><mi>x</mi><mi>i</mi></msub><mo stretchy=&quot;false&quot;>&amp;#x005E;</mo></mover></mrow><msup><mo stretchy=&quot;false&quot;>)</mo><mn>2</mn></msup></msqrt></math>" role="presentation" style="text-align: center; position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-374" style="width: 12.721em; display: inline-block;"><span style="display: inline-block; position: relative; width: 12.333em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(0.633em, 1012.33em, 3.303em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-375"><span class="texatom" id="MathJax-Span-376"><span class="mrow" id="MathJax-Span-377"><span class="mi" id="MathJax-Span-378" style="font-family: MathJax_Main-italic;">R</span><span class="mi" id="MathJax-Span-379" style="font-family: MathJax_Main-italic;">M</span><span class="mi" id="MathJax-Span-380" style="font-family: MathJax_Main-italic;">S</span><span class="mi" id="MathJax-Span-381" style="font-family: MathJax_Main-italic;">E</span></span></span><span class="mo" id="MathJax-Span-382" style="font-family: MathJax_Main; padding-left: 0.294em;">=</span><span class="msqrt" id="MathJax-Span-383" style="padding-left: 0.294em;"><span style="display: inline-block; position: relative; width: 8.109em; height: 0px;"><span style="position: absolute; clip: rect(2.478em, 1007.09em, 4.808em, -999.998em); top: -3.978em; left: 1.022em;"><span class="mrow" id="MathJax-Span-384"><span class="mfrac" id="MathJax-Span-385"><span style="display: inline-block; position: relative; width: 1.022em; height: 0px; margin-right: 0.1em; margin-left: 0.1em;"><span style="position: absolute; clip: rect(3.158em, 1000.44em, 4.129em, -999.998em); top: -4.658em; left: 50%; margin-left: -0.24em;"><span class="mn" id="MathJax-Span-386" style="font-family: MathJax_Main;">1</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.158em, 1000.88em, 4.129em, -999.998em); top: -3.299em; left: 50%; margin-left: -0.434em;"><span class="mi" id="MathJax-Span-387" style="font-family: MathJax_Math-italic;">N<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.1em;"></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(0.876em, 1001.02em, 1.216em, -999.998em); top: -1.308em; left: 0em;"><span style="display: inline-block; overflow: hidden; vertical-align: 0em; border-top: 1.3px solid; width: 1.022em; height: 0px;"></span><span style="display: inline-block; width: 0px; height: 1.07em;"></span></span></span></span><span class="mo" id="MathJax-Span-388" style="font-family: MathJax_Size2; vertical-align: 0em; padding-left: 0.148em;">∑</span><span class="mo" id="MathJax-Span-389" style="font-family: MathJax_Main;">(</span><span class="msubsup" id="MathJax-Span-390"><span style="display: inline-block; position: relative; width: 0.876em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-391" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-392" style="font-size: 70.7%; font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span class="mo" id="MathJax-Span-393" style="font-family: MathJax_Main; padding-left: 0.245em;">−</span><span class="texatom" id="MathJax-Span-394" style="padding-left: 0.245em;"><span class="mrow" id="MathJax-Span-395"><span class="munderover" id="MathJax-Span-396"><span style="display: inline-block; position: relative; width: 0.876em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.88em, 4.274em, -999.998em); top: -3.978em; left: 0em;"><span class="msubsup" id="MathJax-Span-397"><span style="display: inline-block; position: relative; width: 0.876em; height: 0px;"><span style="position: absolute; clip: rect(3.4em, 1000.54em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-398" style="font-family: MathJax_Math-italic;">x</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -3.833em; left: 0.585em;"><span class="mi" id="MathJax-Span-399" style="font-size: 70.7%; font-family: MathJax_Math-italic;">i</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.158em, 1000.39em, 3.595em, -999.998em); top: -4.027em; left: 0.197em;"><span class="mo" id="MathJax-Span-400" style="font-family: MathJax_Main;">^</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span></span><span class="msubsup" id="MathJax-Span-401"><span style="display: inline-block; position: relative; width: 0.828em; height: 0px;"><span style="position: absolute; clip: rect(3.109em, 1000.29em, 4.371em, -999.998em); top: -3.978em; left: 0em;"><span class="mo" id="MathJax-Span-402" style="font-family: MathJax_Main;">)</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -4.415em; left: 0.391em;"><span class="mn" id="MathJax-Span-403" style="font-size: 70.7%; font-family: MathJax_Main;">2</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(3.546em, 1007.14em, 3.886em, -999.998em); top: -5.192em; left: 1.022em;"><span style="display: inline-block; position: relative; width: 7.138em; height: 0px;"><span style="position: absolute; font-family: MathJax_Main; top: -3.978em; left: -0.095em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; font-family: MathJax_Main; top: -3.978em; left: 6.41em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 0.439em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 0.973em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 1.507em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 2.09em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 2.624em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 3.158em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 3.692em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 4.274em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 4.808em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 5.342em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="font-family: MathJax_Main; position: absolute; top: -3.978em; left: 5.876em;">−<span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; clip: rect(2.381em, 1001.02em, 5.1em, -999.998em); top: -4.027em; left: 0em;"><span style="font-family: MathJax_Size3;">√</span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.948em; border-left: 0px solid; width: 0px; height: 2.552em;"></span></span></nobr><span class="MJX_Assistive_MathML MJX_Assistive_MathML_Block" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML" display="block"><mrow class="MJX-TeXAtom-ORD"><mi class="MJX-tex-mathit" mathvariant="italic">R</mi><mi class="MJX-tex-mathit" mathvariant="italic">M</mi><mi class="MJX-tex-mathit" mathvariant="italic">S</mi><mi class="MJX-tex-mathit" mathvariant="italic">E</mi></mrow><mo>=</mo><msqrt><mfrac><mn>1</mn><mi>N</mi></mfrac><mo>∑</mo><mo stretchy="false">(</mo><msub><mi>x</mi><mi>i</mi></msub><mo>−</mo><mrow class="MJX-TeXAtom-ORD"><mover><msub><mi>x</mi><mi>i</mi></msub><mo stretchy="false">^</mo></mover></mrow><msup><mo stretchy="false">)</mo><mn>2</mn></msup></msqrt></math></span></span></div><script type="math/tex; mode=display" id="MathJax-Element-18">
\mathit{RMSE} =\sqrt{\frac{1}{N} \sum (x_i -\hat{x_i})^2}
</script></span></p>
<p>You can use the <a href="http://scikit-learn.org/stable/modules/generated/sklearn.metrics.mean_squared_error.html"><code>mean_square_error</code></a> (MSE) function from <code>sklearn</code>, where the RMSE is just the square root of MSE. To read more about different evaluation metrics you can take a look at <a href="http://research.microsoft.com/pubs/115396/EvaluationMetrics.TR.pdf">this article</a>.</p>
<p>Since you only want to consider predicted ratings that are in the test dataset, you filter out all other elements in the prediction matrix with <code>prediction[ground_truth.nonzero()]</code>.</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">from</span> sklearn.metrics <span class="im">import</span> mean_squared_error
<span class="im">from</span> math <span class="im">import</span> sqrt
<span class="kw">def</span> rmse(prediction, ground_truth):
    prediction <span class="op">=</span> prediction[ground_truth.nonzero()].flatten()
    ground_truth <span class="op">=</span> ground_truth[ground_truth.nonzero()].flatten()
    <span class="cf">return</span> sqrt(mean_squared_error(prediction, ground_truth))</code></pre></div>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="bu">print</span> <span class="st">'User-based CF RMSE: '</span> <span class="op">+</span> <span class="bu">str</span>(rmse(user_prediction, test_data_matrix))
<span class="bu">print</span> <span class="st">'Item-based CF RMSE: '</span> <span class="op">+</span> <span class="bu">str</span>(rmse(item_prediction, test_data_matrix))</code></pre></div>
<pre><code>User-based CF RMSE: 3.1236202241
Item-based CF RMSE: 3.44983070639</code></pre>
<p>Memory-based algorithms are easy to implement and produce reasonable prediction quality. The drawback of memory-based CF is that it doesn't scale to real-world scenarios and doesn't address the well-known cold-start problem, that is when new user or new item enters the system. Model-based CF methods are scalable and can deal with higher sparsity level than memory-based models, but also suffer when new users or items that don't have any ratings enter the system. I would like to thank Ethan Rosenthal for his <a href="http://blog.ethanrosenthal.com/2015/11/02/intro-to-collaborative-filtering/">post</a> about Memory-Based Collaborative Filtering.</p>
<h2 id="model-based-collaborative-filtering">Model-based Collaborative Filtering</h2>
<p>Model-based Collaborative Filtering is based on <em>matrix factorization (MF)</em> which has received greater exposure, mainly as an unsupervised learning method for latent variable decomposition and dimensionality reduction. Matrix factorization is widely used for recommender systems where it can deal better with scalability and sparsity than Memory-based CF. The goal of MF is to learn the latent preferences of users and the latent attributes of items from known ratings (learn features that describe the characteristics of ratings) to then predict the unknown ratings through the dot product of the latent features of users and items. When you have a very sparse matrix, with a lot of dimensions, by doing matrix factorization you can restructure the user-item matrix into low-rank structure, and you can represent the matrix by the multiplication of two low-rank matrices, where the rows contain the latent vector. You fit this matrix to approximate your original matrix, as closely as possible, by multiplying the low-rank matrices together, which fills in the entries missing in the original matrix.</p>
<p>Let's calculate the sparsity level of MovieLens dataset:</p>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python">sparsity<span class="op">=</span><span class="bu">round</span>(<span class="fl">1.0</span><span class="op">-</span><span class="bu">len</span>(df)<span class="op">/</span><span class="bu">float</span>(n_users<span class="op">*</span>n_items),<span class="dv">3</span>)
<span class="bu">print</span> <span class="st">'The sparsity level of MovieLens100K is '</span> <span class="op">+</span>  <span class="bu">str</span>(sparsity<span class="op">*</span><span class="dv">100</span>) <span class="op">+</span> <span class="st">'%'</span></code></pre></div>
<pre><code>The sparsity level of MovieLens100K is 93.7%</code></pre>
<p>To give an example of the learned latent preferences of the users and items: let's say for the MovieLens dataset you have the following information: <em>(user id, age, location, gender, movie id, director, actor, language, year, rating)</em>. By applying matrix factorization the model learns that important user features are <em>age group (under 10, 10-18, 18-30, 30-90)</em>, <em>location</em> and <em>gender</em>, and for movie features it learns that <em>decade</em>, <em>director</em> and <em>actor</em> are most important. Now if you look into the information you have stored, there is no such feature as the <em>decade</em>, but the model can learn on its own. The important aspect is that the CF model only uses data (user_id, movie_id, rating) to learn the latent features. If there is little data available model-based CF model will predict poorly, since it will be more difficult to learn the latent features.</p>
<p>Models that use both ratings and content features are called <em>Hybrid Recommender Systems</em> where both Collaborative Filtering and Content-based Models are combined. Hybrid recommender systems usually show higher accuracy than Collaborative Filtering or Content-based Models on their own: they are capable to address the cold-start problem better since if you don't have any ratings for a user or an item you could use the metadata from the user or item to make a prediction. Hybrid recommender systems will be covered in the next tutorials.</p>
<h3 id="svd">SVD</h3>
<p>A well-known matrix factorization method is <em>Singular value decomposition (SVD)</em>. Collaborative Filtering can be formulated by approximating a matrix <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-19-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>X</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-404" style="width: 0.876em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.828em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.459em, 1000.83em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-405"><span class="mi" id="MathJax-Span-406" style="font-family: MathJax_Math-italic;">X<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.002em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.802em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math></span></span><script type="math/tex" id="MathJax-Element-19">X</script></span> by using singular value decomposition. The winning team at the Netflix Prize competition used SVD matrix factorization models to produce product recommendations, for more information I recommend to read articles: <a href="http://techblog.netflix.com/2012/04/netflix-recommendations-beyond-5-stars.html">Netflix Recommendations: Beyond the 5 stars</a> and <a href="http://buzzard.ups.edu/courses/2014spring/420projects/math420-UPS-spring-2014-gower-netflix-SVD.pdf">Netflix Prize and SVD</a>. The general equation can be expressed as follows: <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-20-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>X</mi><mo>=</mo><mi>U</mi><mo>&amp;#x00D7;</mo><mi>S</mi><mo>&amp;#x00D7;</mo><msup><mi>V</mi><mi>T</mi></msup></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-407" style="width: 7.867em; display: inline-block;"><span style="display: inline-block; position: relative; width: 7.624em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.313em, 1007.62em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-408"><span class="mi" id="MathJax-Span-409" style="font-family: MathJax_Math-italic;">X<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.002em;"></span></span><span class="mo" id="MathJax-Span-410" style="font-family: MathJax_Main; padding-left: 0.294em;">=</span><span class="mi" id="MathJax-Span-411" style="font-family: MathJax_Math-italic; padding-left: 0.294em;">U<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.1em;"></span></span><span class="mo" id="MathJax-Span-412" style="font-family: MathJax_Main; padding-left: 0.245em;">×</span><span class="mi" id="MathJax-Span-413" style="font-family: MathJax_Math-italic; padding-left: 0.245em;">S<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.051em;"></span></span><span class="mo" id="MathJax-Span-414" style="font-family: MathJax_Main; padding-left: 0.245em;">×</span><span class="msubsup" id="MathJax-Span-415" style="padding-left: 0.245em;"><span style="display: inline-block; position: relative; width: 1.459em; height: 0px;"><span style="position: absolute; clip: rect(3.158em, 1000.78em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-416" style="font-family: MathJax_Math-italic;">V<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.197em;"></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -4.318em; left: 0.876em;"><span class="mi" id="MathJax-Span-417" style="font-size: 70.7%; font-family: MathJax_Math-italic;">T<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.1em;"></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 1.002em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi><mo>=</mo><mi>U</mi><mo>×</mo><mi>S</mi><mo>×</mo><msup><mi>V</mi><mi>T</mi></msup></math></span></span><script type="math/tex" id="MathJax-Element-20">X=U \times S \times V^T</script></span></p>
<p>Given an <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-21-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>m</mi><mo>&amp;#x00D7;</mo><mi>n</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-418" style="width: 2.818em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.721em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.653em, 1002.72em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-419"><span class="mi" id="MathJax-Span-420" style="font-family: MathJax_Math-italic;">m</span><span class="mo" id="MathJax-Span-421" style="font-family: MathJax_Main; padding-left: 0.245em;">×</span><span class="mi" id="MathJax-Span-422" style="font-family: MathJax_Math-italic; padding-left: 0.245em;">n</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.603em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mo>×</mo><mi>n</mi></math></span></span><script type="math/tex" id="MathJax-Element-21">m \times n</script></span> matrix <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-22-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>X</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-423" style="width: 0.876em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.828em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.459em, 1000.83em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-424"><span class="mi" id="MathJax-Span-425" style="font-family: MathJax_Math-italic;">X<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.002em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.802em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math></span></span><script type="math/tex" id="MathJax-Element-22">X</script></span>:</p>
<ul>
<li><span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-23-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>U</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-426" style="width: 0.828em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.779em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.459em, 1000.78em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-427"><span class="mi" id="MathJax-Span-428" style="font-family: MathJax_Math-italic;">U<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.1em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.853em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi></math></span></span><script type="math/tex" id="MathJax-Element-23">U</script></span> is an <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-24-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>m</mi><mo>&amp;#x00D7;</mo><mi>r</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-429" style="width: 2.672em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.575em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.653em, 1002.58em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-430"><span class="mi" id="MathJax-Span-431" style="font-family: MathJax_Math-italic;">m</span><span class="mo" id="MathJax-Span-432" style="font-family: MathJax_Main; padding-left: 0.245em;">×</span><span class="mi" id="MathJax-Span-433" style="font-family: MathJax_Math-italic; padding-left: 0.245em;">r</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.603em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>m</mi><mo>×</mo><mi>r</mi></math></span></span><script type="math/tex" id="MathJax-Element-24">m \times r</script></span> orthogonal matrix</li>
<li><span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-25-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>S</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-434" style="width: 0.731em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.682em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.41em, 1000.68em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-435"><span class="mi" id="MathJax-Span-436" style="font-family: MathJax_Math-italic;">S<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.051em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.853em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math></span></span><script type="math/tex" id="MathJax-Element-25">S</script></span> is an <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-26-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>r</mi><mo>&amp;#x00D7;</mo><mi>r</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-437" style="width: 2.284em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.187em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.653em, 1002.19em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-438"><span class="mi" id="MathJax-Span-439" style="font-family: MathJax_Math-italic;">r</span><span class="mo" id="MathJax-Span-440" style="font-family: MathJax_Main; padding-left: 0.245em;">×</span><span class="mi" id="MathJax-Span-441" style="font-family: MathJax_Math-italic; padding-left: 0.245em;">r</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.603em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>r</mi><mo>×</mo><mi>r</mi></math></span></span><script type="math/tex" id="MathJax-Element-26">r \times r</script></span> diagonal matrix with non-negative real numbers on the diagonal</li>
<li><span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-27-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><msup><mi>V</mi><mi>T</mi></msup></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-442" style="width: 1.507em; display: inline-block;"><span style="display: inline-block; position: relative; width: 1.459em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.313em, 1001.46em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-443"><span class="msubsup" id="MathJax-Span-444"><span style="display: inline-block; position: relative; width: 1.459em; height: 0px;"><span style="position: absolute; clip: rect(3.158em, 1000.78em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-445" style="font-family: MathJax_Math-italic;">V<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.197em;"></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -4.318em; left: 0.876em;"><span class="mi" id="MathJax-Span-446" style="font-size: 70.7%; font-family: MathJax_Math-italic;">T<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.1em;"></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 1.002em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>V</mi><mi>T</mi></msup></math></span></span><script type="math/tex" id="MathJax-Element-27">V^T</script></span> is an <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-28-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>r</mi><mo>&amp;#x00D7;</mo><mi>n</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-447" style="width: 2.43em; display: inline-block;"><span style="display: inline-block; position: relative; width: 2.333em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.653em, 1002.33em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-448"><span class="mi" id="MathJax-Span-449" style="font-family: MathJax_Math-italic;">r</span><span class="mo" id="MathJax-Span-450" style="font-family: MathJax_Main; padding-left: 0.245em;">×</span><span class="mi" id="MathJax-Span-451" style="font-family: MathJax_Math-italic; padding-left: 0.245em;">n</span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.603em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>r</mi><mo>×</mo><mi>n</mi></math></span></span><script type="math/tex" id="MathJax-Element-28">r \times n</script></span> orthogonal matrix</li>
</ul>
<p>Elements on the diagnoal in <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-29-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>S</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-452" style="width: 0.731em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.682em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.41em, 1000.68em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-453"><span class="mi" id="MathJax-Span-454" style="font-family: MathJax_Math-italic;">S<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.051em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.853em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math></span></span><script type="math/tex" id="MathJax-Element-29">S</script></span> are known as <em>singular values of <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-30-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>X</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-455" style="width: 0.811em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.811em; height: 0px; font-size: 99%;"><span style="position: absolute; clip: rect(1.366em, 1000.81em, 2.376em, -999.997em); top: -2.22em; left: 0em;"><span class="mrow" id="MathJax-Span-456"><span class="mi" id="MathJax-Span-457" style="font-family: MathJax_Math-italic;">X<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.003em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.225em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.802em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math></span></span><script type="math/tex" id="MathJax-Element-30">X</script></span></em>.</p>
<p>Matrix <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-31-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>X</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-458" style="width: 0.876em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.828em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.459em, 1000.83em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-459"><span class="mi" id="MathJax-Span-460" style="font-family: MathJax_Math-italic;">X<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.002em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.802em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>X</mi></math></span></span><script type="math/tex" id="MathJax-Element-31">X</script></span> can be factorized to <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-32-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>U</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-461" style="width: 0.828em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.779em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.459em, 1000.78em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-462"><span class="mi" id="MathJax-Span-463" style="font-family: MathJax_Math-italic;">U<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.1em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.853em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi></math></span></span><script type="math/tex" id="MathJax-Element-32">U</script></span>, <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-33-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>S</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-464" style="width: 0.731em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.682em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.41em, 1000.68em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-465"><span class="mi" id="MathJax-Span-466" style="font-family: MathJax_Math-italic;">S<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.051em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.853em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math></span></span><script type="math/tex" id="MathJax-Element-33">S</script></span> and <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-34-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>V</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-467" style="width: 0.828em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.779em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.459em, 1000.78em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-468"><span class="mi" id="MathJax-Span-469" style="font-family: MathJax_Math-italic;">V<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.197em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.853em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>V</mi></math></span></span><script type="math/tex" id="MathJax-Element-34">V</script></span>. The <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-35-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>U</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-470" style="width: 0.828em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.779em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.459em, 1000.78em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-471"><span class="mi" id="MathJax-Span-472" style="font-family: MathJax_Math-italic;">U<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.1em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.853em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi></math></span></span><script type="math/tex" id="MathJax-Element-35">U</script></span> matrix represents the feature vectors corresponding to the users in the hidden feature space and the <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-36-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>V</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-473" style="width: 0.828em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.779em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.459em, 1000.78em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-474"><span class="mi" id="MathJax-Span-475" style="font-family: MathJax_Math-italic;">V<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.197em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.853em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>V</mi></math></span></span><script type="math/tex" id="MathJax-Element-36">V</script></span> matrix represents the feature vectors corresponding to the items in the hidden feature space.</p>
<div class="figure">
<img src="https://cambridgespark.com/content/tutorials/implementing-your-own-recommender-systems-in-Python/figures/BLOG_CCA_5.png">

</div>
<p>Now you can make a prediction by taking dot product of <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-37-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>U</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-476" style="width: 0.828em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.779em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.459em, 1000.78em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-477"><span class="mi" id="MathJax-Span-478" style="font-family: MathJax_Math-italic;">U<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.1em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.853em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>U</mi></math></span></span><script type="math/tex" id="MathJax-Element-37">U</script></span>, <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-38-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><mi>S</mi></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-479" style="width: 0.731em; display: inline-block;"><span style="display: inline-block; position: relative; width: 0.682em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.41em, 1000.68em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-480"><span class="mi" id="MathJax-Span-481" style="font-family: MathJax_Math-italic;">S<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.051em;"></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 0.853em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><mi>S</mi></math></span></span><script type="math/tex" id="MathJax-Element-38">S</script></span> and <span class="math inline"><span class="MathJax_Preview" style="color: inherit; display: none;"></span><span class="MathJax" id="MathJax-Element-39-Frame" tabindex="0" data-mathml="<math xmlns=&quot;http://www.w3.org/1998/Math/MathML&quot;><msup><mi>V</mi><mi>T</mi></msup></math>" role="presentation" style="position: relative;"><nobr aria-hidden="true"><span class="math" id="MathJax-Span-482" style="width: 1.507em; display: inline-block;"><span style="display: inline-block; position: relative; width: 1.459em; height: 0px; font-size: 103%;"><span style="position: absolute; clip: rect(1.313em, 1001.46em, 2.43em, -999.998em); top: -2.279em; left: 0em;"><span class="mrow" id="MathJax-Span-483"><span class="msubsup" id="MathJax-Span-484"><span style="display: inline-block; position: relative; width: 1.459em; height: 0px;"><span style="position: absolute; clip: rect(3.158em, 1000.78em, 4.129em, -999.998em); top: -3.978em; left: 0em;"><span class="mi" id="MathJax-Span-485" style="font-family: MathJax_Math-italic;">V<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.197em;"></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span><span style="position: absolute; top: -4.318em; left: 0.876em;"><span class="mi" id="MathJax-Span-486" style="font-size: 70.7%; font-family: MathJax_Math-italic;">T<span style="display: inline-block; overflow: hidden; height: 1px; width: 0.1em;"></span></span><span style="display: inline-block; width: 0px; height: 3.983em;"></span></span></span></span></span><span style="display: inline-block; width: 0px; height: 2.284em;"></span></span></span><span style="display: inline-block; overflow: hidden; vertical-align: -0.048em; border-left: 0px solid; width: 0px; height: 1.002em;"></span></span></nobr><span class="MJX_Assistive_MathML" role="presentation"><math xmlns="http://www.w3.org/1998/Math/MathML"><msup><mi>V</mi><mi>T</mi></msup></math></span></span><script type="math/tex" id="MathJax-Element-39">V^T</script></span>.</p>
<div class="figure">
<img src="https://cambridgespark.com/content/tutorials/implementing-your-own-recommender-systems-in-Python/figures/BLOG_CCA_4.png">

</div>
<div class="sourceCode"><pre class="sourceCode python"><code class="sourceCode python"><span class="im">import</span> scipy.sparse <span class="im">as</span> sp
<span class="im">from</span> scipy.sparse.linalg <span class="im">import</span> svds

<span class="co">#get SVD components from train matrix. Choose k.</span>
u, s, vt <span class="op">=</span> svds(train_data_matrix, k <span class="op">=</span> <span class="dv">20</span>)
s_diag_matrix<span class="op">=</span>np.diag(s)
X_pred <span class="op">=</span> np.dot(np.dot(u, s_diag_matrix), vt)
<span class="bu">print</span> <span class="st">'User-based CF MSE: '</span> <span class="op">+</span> <span class="bu">str</span>(rmse(X_pred, test_data_matrix))</code></pre></div>
<pre><code>User-based CF MSE: 2.71348045599</code></pre>
<p>Carelessly addressing only the relatively few known entries is highly prone to overfitting. SVD can be very slow and computationally expensive. More recent work minimizes the squared error by applying alternating least square or stochastic gradient descent and uses regularization terms to prevent overfitting.</p>
<p>To wrap it up:</p>
<ul>
<li>In this post we have covered how to implement simple <em>Collaborative Filtering</em> methods, both memory-based CF and model-based CF.</li>
<li><em>Memory-based models</em> are based on similarity between items or users, where we use cosine-similarity.</li>
<li><em>Model-based CF</em> is based on matrix factorization where we use SVD to factorize the matrix.</li>
<li>Building recommender systems that perform well in cold-start scenarios (where litle data is availabe on new users and items) remains a challenge. The standard collaborative filtering method performs poorly is such settings. In the next tutorials you will have the change to dig deeper into this problem.</li>
</ul>

1.Item base movie Recommendation System

1. Import Required Packages

# This Python 3 environment comes with many helpful analytics libraries installed
# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python
# For example, here's several helpful packages to load in 

import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
%matplotlib inline 
from sklearn.metrics import pairwise_distances
from scipy.spatial.distance import cosine,correlation

# Input data files are available in the "../input/" directory.
# For example, running this (by clicking run or pressing Shift+Enter) will list the files in the input directory

import os
print(os.listdir("../input"))

from subprocess import check_output
print(check_output(["ls", "../input"]).decode("utf8"))

# Any results you write to the current directory are saved as output.
['ml-100k'] ml-100k

2. Read and Merge Dataset