- We will create a new Python file and import the following packages:
import json import numpy as np
- To calculate the Pearson correlation score between two users, we will define a new function. Let's check the presence of the users in the database:
# Returns the Pearson correlation score between user1 and user2 def pearson _dist_score(dataset, FirstUser, SecondUser): if FirstUser not in dataset: raise TypeError('User ' + FirstUser + ' not present in the dataset') if SecondUser not in dataset: raise TypeError('User ' + SecondUser + ' not present in the dataset')
- We will now extract the movies that have been rated by both users:
# Movies rated by both FirstUser and SecondUser Both_User_rated = {} for item in dataset[FirstUser]: ...