Question
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
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.
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.
(1) Define a function calledpearsonD.
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
The Pearson Correlation calculation must consider an item only if it was rated by both users
(2) Then call this function for UserPRatings and UserQRatings defined above, and print the person correlation value.
The Framework for the code should look like this:
import math
def pearsonD(userratings1, userratings2):
# use the computationally efficient form of Pearson correlation
# use a single for loop
# consider an item only if it was rated by both users
# return calculated Pearson correlation value
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}
# Call the function
# print returned value
# If correct the Pearson Correlation is -0.7307
# do not use numpy
Here is what I have so far but it is incorrect and I cannot use numpy to find the correlation
def pearsonD(userratings1, userratings2):
import numpy as np
import math
user1_keys = list(userratings1.keys())
user2_keys = list(userratings2.keys())
userratings1_mean = np.mean(list((userratings1.values())))
userratings2_mean = np.mean(list((userratings2.values())))
unique_keys = np.unique(user1_keys + user2_keys)
sum_num = 0
xm = 0
ym = 0
for key in unique_keys:
if key in user1_keys and key in user2_keys:
sum_num = sum_num + ((userratings1.get(key)-userratings1_mean) * (userratings2.get(key)-userratings2_mean))
xm = xm + (userratings1.get(key)-userratings1_mean)**2
ym = ym + (userratings2.get(key)-userratings2_mean)**2
return sum_num / math.sqrt((xm * ym))
UserPRatings = {'Apple':1, 'Samsung':6, 'Nokia':7, 'Motorola':8, 'LG':6, 'Sony':1, 'Blackberry':9}
UserQRatings = {'Apple':7, 'Samsung':1, 'Nokia':9, 'LG':9, 'Sony':8, 'Blackberry':3}
print(pearsonD(UserPRatings, UserQRatings))
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started