Let’s say we have two users, User P and User Q, and they have rated 7 different cell phone brands on a scale of 1 to 10.




Problem Definition:


Let’s say we have two users, User P and User Q, and they have rated 7 different cell phone brands on a scale of 1 to 10.


Item Ratings of User P Ratings of User Q
Apple 1 7
Samsung 5 1
Nokia 7 4
Motorola 8
LG 5 4
Sony 1 6
Blackberry 7 3


In Python, we can depict these ratings as two dictionaries:

UserPRatings = {‘Apple’:1, ‘Samsung’:5, ‘Nokia’:7, ‘Motorola’:8, ‘LG’:5, ‘Sony’:1, ‘Blackberry’:7}

UserQRatings = {‘Apple’:7, ‘Samsung’:1, ‘Nokia’:4, ‘LG’:4, ‘Sony’:6, ‘Blackberry’:3}


We would like to find the Pearson Correlation between these two lists.

Requirement for this Assignment:


This assignment requires you to define and call a Person Correlation function as follows. A framework to get you started has been provided on the next page.


(1) Define a function called pearsonD.

  • The function must take two (and only two) dictionary parameters – user1ratings and user2ratings – and return the Pearson Correlation between the two user ratings.
  • The function must use a single for loop to calculate the Pearson Correlation using the computationally efficient form (similar to Slides 16 and 17 of Knowledge Check 3… but using dictionaries instead of lists):
  • The Pearson Correlation calculation must consider an item only if it was rated by both users (similar to how we handled this for Minkowski on Slides 14 and 15 of Knowledge Check 4)


(2) Then call this function for UserPRatings and UserQRatings defined above, and print the person correlation value.






Some things to keep in mind as you code:

  • Make your code readable – for instance, use meaningful variable names and comments.
  • Make your code elegant – for instance, balance the number of variables you introduce – too many or too few make your code difficult to debug, read, and maintain.
  • Make your output readable and user-friendly

Framework for coding:




Once you have written up the script, save it as follows: <FirstName><LastName>Assignment1.py.

[Example: HinaAroraAssignment1.py]


Submit the script by uploading the above python script. Note: upload the actual script – DO NOT attach a screenshot of the script!


The submitted script will be run as-is for grading. Points will be deducted for scripts that:

  • do not adhere to all of the requirements called out under the requirements section above
  • are difficult to read/follow
  • don’t compile/run
  • don’t have all the various pieces of code required
  • have hard-code values instead of using variables
  • have logical errors
  • don’t result in the expected output
  • don’t have user-friendly output



If you have coded up this assignment correctly, you should end up with a Pearson Correlation of -0.7307.