Saturday, January 24, 2009

Assignment 1

My first python function was the sim_distance method

#returns a distance-based similarity score for person1 and person2
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 si])
    return 1/(1+(sum_of_squares))

I found the error on page 11 and changed 

return 1/(1+sqrt(sum_of_squares))

to

return 1/(1+(sum_of_squares))

>>> recommendations.sim_distance(recommendations.critics,'Lisa Rose','Gene Seymour')
0.14814814814814814
_________________________________________________________________
Here is my Manhattan Distance Function:

#returns a distance-based similarity score for person1 and person2
def man_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
    ManDistance = [ abs(prefs[person1][item] - prefs[person2][item]) for item in si ]

    return (1/(1+sum(ManDistance)))

>>> recommendations.man_distance(recommendations.critics,'Lisa Rose','Gene Seymour')
0.18181818181818182
_________________________________________________________________

I found the Collective Intelligence book to be a good way of learning how to program in Python, assuming one has previous programming experience. I liked how the book starts off with explaining the importance of collective intelligence along with real-world examples. Next the book gives a basic example of a recommendation system to program in Python and the code starts to increase in complexity.

I like how the chapters are not too verbose and are easy to understand. I was slightly disappointed how the book did not give the Manhattan Distance formula in the book, but rather gave a link to a wikipedia article. However, I am enjoying using this book and I hope the book continues to be good.

No comments:

Post a Comment