Ayushi Jain
Ayushi Jain

Reputation: 43

Python Code from Programming Collective intelligence

Heres a code of recommendations.py Programming Collective Intelligence Book Chapter 2.

The code below is to returns the best matches for person from the preference dictionary and get recommendations for a person by using a weighted average of every other user's rankings

from math import sqrt
critics={'Lisa Rose': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.5,'Just My Luck': 3.0, 'Superman Returns': 3.5, 'You, Me and Dupree': 2.5,'The Night Listener': 3.0},'Gene Seymour': {'Lady in the Water': 3.0, 'Snakes on a Plane': 3.5,'Just My Luck': 1.5, 'Superman Returns': 5.0, 'The Night Listener': 3.0,'You, Me and Dupree': 3.5},'Michael Phillips': {'Lady in the Water': 2.5, 'Snakes on a Plane': 3.0,'Superman Returns': 3.5, 'The Night Listener': 4.0},'Claudia Puig': {'Snakes on a Plane': 3.5, 'Just My Luck': 3.0,'The Night Listener': 4.5, 'Superman Returns': 4.0,'You, Me and Dupree': 2.5},'Mick LaSalle': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,'Just My Luck': 2.0, 'Superman Returns': 3.0, 'The Night Listener': 3.0,'You, Me and Dupree': 2.0},'Jack Matthews': {'Lady in the Water': 3.0, 'Snakes on a Plane': 4.0,'The Night Listener': 3.0, 'Superman Returns': 5.0, 'You, Me and Dupree': 3.5},'Toby': {'Snakes on a Plane':4.5,'You, Me and Dupree':1.0,'Superman Returns':4.0}} 
def sim_distance(prefs,person1,person2):  # Get the list of shared_items 
    si={}  
    for item in prefs[person1]:
        if item in prefs[person2]:
            si[item]=1
# if they have no ratings in common, return 0  
    if len(si)==0: 
        return 0
# Add up the squares of all the differences  
    sum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2)  
                    for item in prefs[person1] if item in prefs[person2]])
    return 1/(1+sum_of_squares) 

I have just started working with python, so I'm confused what is the difference between these two loops :

At the starting,

for item in prefs[person1]:
    if item in prefs[person2]: 

At the end,

for item in prefs[person1] if item in prefs[person2]])

Also, they use the square root in implementation , which in this code, they have not used. So, is this just an example or we dont use in codes? Also, if these both for loops are same, they give different answers when I apply the second for loop like this.

for item in prefs[person1]:
    if item in prefs[person2]:
        sum_of_squares=sum([pow(prefs[person1][item]-prefs[person2][item],2)])

Upvotes: 2

Views: 240

Answers (1)

vaibhavgb
vaibhavgb

Reputation: 26

Also, they use the square root in implementation , which in this code, they have not used. So, is this just an example or we dont use in codes?

Please check this link The 'Unconfirmed' errata for this mistake by O'Reilly Media

The last method should be return 1/(1+*sqrt(sum_of_squares)) and off course you can use this in code. Also, there is a mistake in the example too. The coordinates are wrong. please refer the link

As mentioned in the comment, 1st loop checks how many ratings they have in common.

>>> recommendations.sim_distance(recommendations.critics,'Lisa Rose','Toby')

[FOR] Lisa :  Lady in the Water
[FOR] Lisa :  Snakes on a Plane
[IF] Toby  :  Snakes on a Plane
[FOR] Lisa :  Just My Luck
[FOR] Lisa :  Superman Returns
[IF] Toby  :  Superman Returns
[FOR] Lisa :  You, Me and Dupree
[IF] Toby  :  You, Me and Dupree
[FOR] Lisa :  The Night Listener

0.3483314773547883

The 2nd loop iterates the item-values(ratings)for subtraction.

As per the above list, this loop has to make 3 iterations (FOR == person1, IF == person2).

Upvotes: 1

Related Questions