Friday, January 23, 2009

Portfolio #1: My first real experience with Python.

Before now, I've only looked at other people's python code--being a lab aide, I've had to help some people with their code, but I've never actually written any myself. Regardless, before starting to read the section, I played around with python a bit, to make sure that I could actually make anything; it was successful (I did struggle a little at first though).

I then did everything up to page 9. No hitches or surprises.
>>> from recommendations import critics
>>> critics[
...
... ;
File "", line 3
;
^
SyntaxError: invalid syntax
>>> critics['Lisa Rose']['Lady in the Water']
2.5
>>> critics['Toby']['Snakes on a Plane']=4.5
>>> critics['Toby']
{'Snakes on a Plane': 4.5, 'Superman Returns': 4.0, 'You, Me and Dupree': 1.0}

I accidentally hit 'enter' after 'critics[' the first time--I didn't really know how to undo what I was doing, so I just typed in crap, then continued. Kind've brutish, but it worked, and I got the same result as the book!

Then I did pages 10-11. I got a different answer here than in the book:
>>> from math import sqrt
>>> sqrt(pow(4.5-4,2)+pow(1-2,2))
1.1180339887498949
>>> 1/(1+sqrt(pow(4.5-4,2)+pow(1-2,2)))
0.47213595499957939
>>> import recommendations
>>> reload(recommendations)

>>> recommendations.sim_distance(recommendations.critics,'Lisa Rose','Gene Seymour')
0.29429805508554946
>>> recommendations.sim_distance(recommendations.critics,'Lisa Rose','Gene Seymour')
0.29429805508554946
>>>
The first part matched with the book. However, as the result for 'recommendations.sim_distance(...)', I got the answer as you see; the book got 0.148 repeated. I couldn't figure out why my result was different--perhaps I'll find out later.

Pages 13-14 (beginning of the page) went by without too many hitches... if by without too many hitches you mean a lot of them. I was being stupid while typing the code, and thus got a errors that took me a while to root out (I'm still not quite used to the syntax). Regardless, I got through it.
SyntaxError: invalid syntax
>>> reload(recommendations)

>>> print recommendations.sim_pearson(recommendations.critics, 'Lisa Rose','Gene Seymour')
0.396059017191

End of p. 14 and p. 15 went without any hitches; my results matched the book's.
>>> reload (recommendations)

>>> recommendations.topMatches(recommendations.critics, 'Toby',n=3)
[(0.99124070716192991, 'Lisa Rose'), (0.92447345164190486, 'Mick LaSalle'), (0.89340514744156474, 'Claudia Puig')]
>>>

The code for pages 16 and 17 didn't work out quite well either; here's what I got originally:

>>> recommendations.getRecommendations(recommendations.critics, 'Toby')
[(3.4200483059633244, 'The Night Listener'), (3.0, 'The Night LIstener'), (2.8325499182641614, 'Lady in the Water'), (2.3485551731281777, 'Just My Luck')]
The first issue was here; 'Just My Luck' had a different result than the book (2.3 for me, 2.5 in the book)

>>> recommendations.getRecommendations(recommendatiosn.critics, 'Toby', similarity=recommendations.sim_distance)
Traceback (most recent call last):
File "", line 1, in
NameError: name 'recommendatiosn' is not defined
>>> recommendations.getRecommendations(recommendations.critics, 'Toby', similarity=recommendations.sim_distance)
[(3.5269669502488501, 'The Night Listener'), (3.0, 'The Night LIstener'), (2.8632982149533528, 'Lady in the Water'), (2.5, 'Lady in the Wather'), (2.2177762743461225, 'Just My Luck')]
>>>
The second issue being here; 'The Night Listener' has two wrong results for me (although the first result is not very wrong).

After seeing it in this blog, however, the typos in my recommendations.py file became obvious (yey bigger font?). While this fixed the repeat entries, my numbers are still very slightly off (not enough for me to change anything though--usually only 0.1 off).
>>> reload(recommendations)

>>> recommendations.getRecommendations(recommendations.critics, 'Toby', similarity=recommendations.sim_distance)
[(3.457128694491423, 'The Night Listener'), (2.7785840038149239, 'Lady in the Water'), (2.2177762743461225, 'Just My Luck')]
>>>

Page 18 didn't work out correctly either. The first part was mostly accurate:
>>> recommendations.topMatches(movies,'Superman Returns')
[(0.65795169495976946, 'You, Me and Dupree'), (0.48795003647426888, 'Lady in the Water'), (0.11180339887498941, 'Snakes on a Plane'), (-0.17984719479905439, 'The Night Listener'), (-0.5765566601970562, 'Just My Luck')]

However, I got nothing for the second part:
>>> recommendations.getRecommendations(movies,'Just My Luck')
[]

No clue why it didn't work.