Question: find the Pearson Correlation between these two lists. In Python, we can depict these ratings as two dictionaries: UserPRatings = {'Apple':1, 'Samsung':6, 'Nokia':7, 'Motorola':8, 'LG':6,

find the Pearson Correlation between these two lists.

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

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}

(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 using dictionaries instead of lists

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 Pearson correlation value.

The code should look something like this:

import math

#Pearson Correlation between two vectors

def PearsonD(userratings1, userratings2):

# (1) your code here

# Use only the computational efficient form of Pearson correlation

# You must use a single for loop for this solution

# Consider an item only if it was rated by both users

# Remember to return the calculated Pearson correlation value

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}

# (2) your code here

# Remember to call your function

# Remember to print the returned value

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!