A method for generating item recommendations based on user preference for a particular item

By at August 26, 2009 09:58
Filed Under:

Question : If you happen to like a particular Movie A, what is the probability you will also like Movie B ?

I’ll try to describe a simple rudimentary system that will try to answer that question. Lets make the following assumptions :

  • We have an online community of users that we can collect information on whether they liked a particular movie and store those answers in a database.
  • The end user can can make 3 distinct choices for each movie, they can either like it, dislike it or not care for it.

Setting up the Environment

Our database schema is represented by the following three tables.

sql1

The Users table contain information about our users. The Ratings table contain the actual votes cast for a particular RatingDefition and the RatingDefinition table contains the Movie names we’ll use for this example.

You can use the following scripts to create the database objects and populate with relevant data. For this particular case we have a total of 50 movies with 10000 users and a total of 500000 like/dislike/neutral votes. For this reason the populate script may take a while to complete.

The basic algorithm of calculating the probability of liking a Movie B given that you like Movie A is implemented as a User Defined Function that takes 2 parameters. First the movie that we liked and the second is the movie that we want to test.

CREATE FUNCTION [dbo].[fnGetProbabilityOfLikingItem] 
(
    @ItemAlreadyLiked UNIQUEIDENTIFIER,
    @ItemToBeTested UNIQUEIDENTIFIER
)
RETURNS FLOAT 
AS
BEGIN
RETURN ( 
 
        SELECT  (SUM(CASE WHEN r.Rating >0 THEN 1.0 ELSE 0.0 END)/SUM(CASE WHEN r.Rating !=0 THEN 1.0 ELSE 0.0 END)) * 100
        FROM    Ratings r
        WHERE   r.UserId IN 
                    (
                    -- all the users that liked that item
                    SELECT UserId 
                    FROM dbo.Ratings 
                    WHERE RatingDefinitionId = @ItemAlreadyLiked AND rating > 0
                    )
                    AND r.RatingDefinitionId = @ItemToBeTested
        )
END

This UDF determines the universe of users that had a favorable review of Movie A and then gathers all the other movies that they' have also liked and tabulates them based on the number of votes cast allowing us to do pair-wise comparisons like this :

DECLARE @ItemAlreadyLiked UNIQUEIDENTIFIER
DECLARE @ItemToBeTested UNIQUEIDENTIFIER
    
SELECT  @ItemAlreadyLiked = Id 
FROM    dbo.RatingDefinition
WHERE   Name = 'Titanic'
 
SELECT  @ItemToBeTested = Id
FROM    dbo.RatingDefinition
WHERE   Name = 'The Dark Knight'
 
SELECT  ProbabilityOfLiking = dbo.fnGetProbabilityOfLikingItem(@ItemAlreadyLiked, @ItemToBeTested)
 
RESULT : 
 
ProbabilityOfLiking
----------------------
50.9977
 
(1 row(s) affected)

 

Since the function is written as a scalar UDF we can also to things like :

 

query

This maybe useful to present a list to the user that they may also be interested in viewing the other movies listed. One thing to note here is the calculated probability numbers appear to be constrained within close proximity of each other. This is due to the fact the algorithm used to generate the votes relies on a random number generator which yields a uniform distribution of votes (ie. there a roughly equivalent number of like, dislike and neutral votes) resulting in this tight spread.

In closing the method outlined here is a much simplified version of what actually might be used in a real world application where the business logic will typically require analysis across several dimensions of varying numbers and their effects on each other rather than the single dimension that we have chosen to take a look at here.

Comments

2/19/2010 5:02:00 PM #

Kurt Cobain Quotes

Excellently written article, if only all bloggers offered the same content as you, the internet would be a much better place. Please keep it up! Cheers.

Kurt Cobain Quotes United States | Reply

3/20/2010 11:34:48 PM #

cheap prom Dresses

the internet would be a much better place. Please keep it up! Cheers.

cheap prom Dresses People's Republic of China | Reply

4/5/2010 7:25:46 PM #

Zoom H2 Handy

This is due to the fact the algorithm used to generate the votes relies on a random number generator which yields a uniform distribution of votes (ie. there a roughly equivalent number of like, dislike and neutral votes) resulting in this tight spread.

Zoom H2 Handy United Kingdom | Reply

4/23/2010 5:46:23 PM #

clinton township mi

I've been searching for info like this.
Thanks for sharing it with us.

clinton township mi Kyrgyzstan | Reply

4/27/2010 1:46:00 PM #

How to get ripped

I really appreciate your articles. I'll bookmark this so I can read more later. Thanks again for sharing this free online

How to get ripped United States | Reply

5/1/2010 4:42:03 PM #

Janki

Very nicely illustrated. Bookmarked it for future reference.

Janki United Kingdom | Reply

6/4/2010 8:38:50 AM #

Heriberto Khalifah

This is a very fascinating post, I was looking for this knowledge. Just so you know I found your site when I was researching for blogs like mine, so please check out my site sometime and leave me a comment to let me know what you think.

Heriberto Khalifah United States | Reply

6/6/2010 3:54:25 PM #

Coach Signature

The next time I read a blog, I hope that it doesnt disappoint me as much as this one.  I mean, I know it was my choice to read, but I actually thought youd have something interesting to say.  All I hear is a bunch of whining about something that you could fix if you werent too busy looking for attention.

Coach Signature United States | Reply

6/10/2010 9:39:26 AM #

remax toronto real estate agents

Thanks for this brilliant article. I am delighted after reading this. Thank you!

remax toronto real estate agents United States | Reply

6/13/2010 1:50:09 AM #

Bob Schmidt

Hey - Alpha Girl Here!  I just found your blog and absolutely love it!  I linked to it from my site and will be back often.  Thanks!

Bob Schmidt United States | Reply

6/13/2010 4:07:45 AM #

Cammie Brienen

Hi everyone!  I'm so excited about the latest season of True Blood!

Season 3 has finally arrived and I am in heaven.

True Blood is the sexiest show on television!

Cammie Brienen United States | Reply

6/17/2010 8:59:14 PM #

glass etching cream

Just now I discovered the web for exactly this kind of information. Thank to your post that search has to end right now. You wrote the post in a very understandable way. With that, I added your blogs as one of my favorites! Cheers!

glass etching cream United States | Reply

6/18/2010 11:19:57 PM #

Business Advance

This is the perfect blog for anyone who wants to know about this topic.  You know so much its almost hard to argue with you (not that I really would want...HaHa).  You definitely put a new spin on a subject thats been written about for years.  Great stuff, just great!

Business Advance United States | Reply

7/1/2010 3:32:00 AM #

generic medications

Incredible manual, the best of all I have read so far. I'll send a link to my colleague, he was looking for that sort of info.

generic medications United States | Reply

7/5/2010 11:11:07 AM #

writing essays

This is my first time i visit here. I found so many interesting stuff in your blog, especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the leisure here! Keep up the excellent work.

writing essays United States | Reply

7/8/2010 11:39:44 AM #

Rubin Warhola

I dont know what to say.  This blog is fantastic.  Thats not really a really huge statement, but its all I could come up with after reading this.  You know so much about this subject.

Rubin Warhola Poland | Reply

7/21/2010 4:26:25 PM #

write ebook

Well done! - I looked at the Wiki on this and it did not have as detailed info - thanks!

write ebook United States | Reply

8/4/2010 11:16:42 AM #

amiga

This was just what I was on  looking for. I'll come back to this blog for sure!

amiga Poland | Reply

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading