<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>code-diesel &#187; collaborative filtering</title>
	<atom:link href="http://www.codediesel.com/tag/collaborative-filtering/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codediesel.com</link>
	<description>/* PHP &#38; MySQL Journal */</description>
	<lastBuildDate>Thu, 02 Feb 2012 13:19:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<xhtml:meta xmlns:xhtml="http://www.w3.org/1999/xhtml" name="robots" content="noindex" />
		<item>
		<title>Item based collaborative filtering in PHP</title>
		<link>http://www.codediesel.com/php/item-based-collaborative-filtering-php/</link>
		<comments>http://www.codediesel.com/php/item-based-collaborative-filtering-php/#comments</comments>
		<pubDate>Fri, 25 Apr 2008 04:05:19 +0000</pubDate>
		<dc:creator>sameer</dc:creator>
				<category><![CDATA[data]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[collaborative filtering]]></category>

		<guid isPermaLink="false">http://www.codediesel.com/?p=27</guid>
		<description><![CDATA[Most people are familiar with recommendation systems on websites, wherein after you select an item you are presented with a list of similar items other people purchased . Amazon being the popular one and also one of the first to use it. Below is shown a snapshot from Amazon. Collaborative filtering algorithms work by searching [...]]]></description>
			<content:encoded><![CDATA[<p>Most people are familiar with recommendation systems on websites, wherein after you select an item you are presented with a list of similar items other people purchased . Amazon being the popular one and also one of the first to use it. Below is shown a snapshot from Amazon.</p>
<p>Collaborative filtering algorithms work by searching a large group of users or items and finding a smaller llist from it with tastes similar to yours.<span id="more-27"></span></p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2008/04/recommend_amazon.gif"><img class="alignnone size-full wp-image-28" title="recommend_amazon" src="http://www.codediesel.com/wp-content/uploads/2008/04/recommend_amazon.gif" alt="" width="500" height="143" /></a></p>
<p>In this post I&#8217;ll show you how to integrate a simple recommendation system. You can download the class file <a href="http://www.codediesel.com/data/code/sample_recommend.zip" target="_blank">here</a>.</p>
<p>We will work with the following book data as an example.</p>
<p><a href="http://www.codediesel.com/wp-content/uploads/2008/04/recommend1.gif"><img class="aligncenter size-full wp-image-29" title="recommend1" src="http://www.codediesel.com/wp-content/uploads/2008/04/recommend1.gif" alt="sample data" width="297" height="349" /></a></p>
<p>It shows a list of books purchased by various people and rated on a scale of 5 by them. The list can be any thing &#8211; books, jewelry, movies, blog posts; any items where ratings are possible. You have to create this array at runtime form the database.</p>
<p>Our job here will be to use this list to recommend books. The above list is converted into an array here.</p>

<div class="wp_codebox"><table><tr id="p2713"><td class="code" id="p27code13"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$books</span> <span style="color: #339933;">=</span>  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
&nbsp;
    <span style="color: #0000ff;">&quot;phil&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;my girl&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.5</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;the god delusion&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">&quot;tweak&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;the shack&quot;</span> <span style="color: #339933;">=&gt;;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">&quot;the birds in my life&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.5</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">&quot;new moon&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #0000ff;">&quot;sameer&quot;</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;the last lecture&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.5</span><span style="color: #339933;">,</span>
                      <span style="color: #0000ff;">&quot;the god delusion&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span><span style="color: #339933;">,</span>
                      <span style="color: #0000ff;">&quot;the noble wilds&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;the shack&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span><span style="color: #339933;">,</span>
                      <span style="color: #0000ff;">&quot;the birds in my life&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.5</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;new moon&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #0000ff;">&quot;john&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;a thousand splendid suns&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;the secret&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">&quot;tweak&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #0000ff;">&quot;peter&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chaos&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;php in action&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #0000ff;">&quot;jill&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;the last lecture&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">1.5</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;the secret&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.5</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">&quot;the noble wilds&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;the host: a novel&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span><span style="color: #339933;">,</span>
                    <span style="color: #0000ff;">&quot;the world without end&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.5</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;new moon&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #0000ff;">&quot;bruce&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;the last lecture&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;the hollow&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">1.5</span><span style="color: #339933;">,</span>
                     <span style="color: #0000ff;">&quot;the noble wilds&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;the shack&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span><span style="color: #339933;">,</span>
                     <span style="color: #0000ff;">&quot;the appeal&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;new moon&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
&nbsp;
    <span style="color: #0000ff;">&quot;tom&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;chaos&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.5</span><span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Lets start by including the class file first.</p>

<div class="wp_codebox"><table><tr id="p2714"><td class="code" id="p27code14"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">require_once</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;recommend.php&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Lets say you are &#8216;John&#8217; and the books you have purchased till now are &#8216;a thousand splendid suns&#8217;, &#8216;the secret&#8217; and &#8216;tweak&#8217; with appropriate rating given. If you come to the website again what books would the site recommend. Lets try!</p>

<div class="wp_codebox"><table><tr id="p2715"><td class="code" id="p27code15"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$re</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Recommend<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$re</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRecommendations</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$books</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;john&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>It will output the following with appropriate ratings and sorted in descending order.</p>

<div class="wp_codebox"><table><tr id="p2716"><td class="code" id="p27code16"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>the noble wilds<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;;</span> <span style="color: #cc66cc;">4</span>
    <span style="color: #009900;">&#91;</span>the shack<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span>
    <span style="color: #009900;">&#91;</span>the host<span style="color: #339933;">:</span> a novel<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span>
    <span style="color: #009900;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> moon<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span>
    <span style="color: #009900;">&#91;</span>the god delusion<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span>
    <span style="color: #009900;">&#91;</span>the world without <span style="color: #990000;">end</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;;</span> <span style="color:#800080;">2.5</span>
    <span style="color: #009900;">&#91;</span>the birds in my life<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;;</span> <span style="color:#800080;">2.5</span>
    <span style="color: #009900;">&#91;</span>my girl<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.5</span>
    <span style="color: #009900;">&#91;</span>the last lecture<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">1.5</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Now lets try with &#8216;tom&#8217;.</p>

<div class="wp_codebox"><table><tr id="p2717"><td class="code" id="p27code17"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$re</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Recommend<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$re</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getRecommendations</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$books</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;tom&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The recommended books will be&#8230;</p>

<div class="wp_codebox"><table><tr id="p2718"><td class="code" id="p27code18"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
<span style="color: #009900;">&#91;</span>php in action<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>Now how about recommending items based on book name rather then on the person. For that what we have to do is invert the array using the &#8216;transformPreferences&#8217; function and then use the &#8216;matchItems&#8217; function.</p>
<p>Inverting the array like this</p>

<div class="wp_codebox"><table><tr id="p2719"><td class="code" id="p27code19"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$re</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">transformPreferences</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$books</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>will return an array as show below.</p>

<div class="wp_codebox"><table><tr id="p2720"><td class="code" id="p27code20"><pre class="php" style="font-family:monospace;">rray
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>my girl<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>phil<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.5</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span>the god delusion<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>phil<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span>
            <span style="color: #009900;">&#91;</span>sameer<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span>tweak<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>phil<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
            <span style="color: #009900;">&#91;</span>john<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span>the shack<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>phil<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">4</span>
            <span style="color: #009900;">&#91;</span>sameer<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span>
            <span style="color: #009900;">&#91;</span>bruce<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span>the birds in my life<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>phil<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.5</span>
            <span style="color: #009900;">&#91;</span>sameer<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;;</span> <span style="color:#800080;">2.5</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span><span style="color: #000000; font-weight: bold;">new</span> moon<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>phil<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span>
            <span style="color: #009900;">&#91;</span>sameer<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">1</span>
            <span style="color: #009900;">&#91;</span>jill<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">3.5</span>
            <span style="color: #009900;">&#91;</span>bruce<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
    <span style="color: #009900;">&#91;</span>the last lecture<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span>sameer<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">2.5</span>
            <span style="color: #009900;">&#91;</span>jill<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">1.5</span>
            <span style="color: #009900;">&#91;</span>bruce<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">3</span>
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span>
<span style="color: #339933;">.</span></pre></td></tr></table></div>

<p>Lets try with the book &#8216;chaos&#8217; and see what the system will recommend us.</p>

<div class="wp_codebox"><table><tr id="p2721"><td class="code" id="p27code21"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$re</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Recommend<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$re</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">transformPreferences</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$books</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$re</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">matchItems</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;chaos&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The out will be.</p>

<div class="wp_codebox"><table><tr id="p2722"><td class="code" id="p27code22"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">Array</span>
<span style="color: #009900;">&#40;</span>
<span style="color: #009900;">&#91;</span>php in action<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color:#800080;">0.4</span>
<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>But now the value on the right is not a rating but a match probability from a scale of 0 &#8211; 1, 1 being a perfect match. If you change the rating of the &#8216;php in action book&#8217; in the above array to 5 and then run the above code again you will get the match probability as &#8217;1&#8242;.</p>
<p>You can also find how similar the choice of two persons is on a scale of 0-1.</p>

<div class="wp_codebox"><table><tr id="p2723"><td class="code" id="p27code23"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$similarity</span> <span style="color: #339933;">=</span>  <span style="color: #000088;">$re</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">similarityDistance</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$books</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;tom&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;peter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009933; font-style: italic;">//Converted to percent.</span>
<span style="color: #000000; font-weight: bold;">echo</span> <span style="color: #990000;">sprintf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%.2f</span>&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$similarity</span> <span style="color: #339933;">*</span> <span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;%&quot;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Will return</p>

<div class="wp_codebox"><table><tr id="p2724"><td class="code" id="p27code24"><pre class="php" style="font-family:monospace;"><span style="color:#800080;">28.57</span><span style="color: #339933;">%</span></pre></td></tr></table></div>

<p>This simplified filtering system can be used on a site with a few thousand items and members. But as the number of items grows it will be time consuming to calculate the recommendations every time someone purchases or browses an item. In that case it will be helpful to precalculate the array and store it in a database maybe once a day. But that is a different story altogether. For more information on the algorithms and variations you can refer to the wonderful <a title="Programming collective intelligence" href="http://www.amazon.com/Programming-Collective-Intelligence-Building-Applications/dp/0596529325/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1209100464&amp;sr=1-1" target="_blank">Programming Collective Intelligence: Building Smart Web 2.0 Applications</a>. The code examples in the book are in Python, though.</p>
<p>The above code may not work in each and every instance but you can tweak the code according to your requirements.</p>
<p><a class="download" href="http://www.codediesel.com/data/code/sample_recommend.zip">Download code</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.codediesel.com/php/item-based-collaborative-filtering-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>

