How to do it...

  1. We will create a new Python file and import the following packages:
import json 
import numpy as np
  1. 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') 
  1. 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]: ...

Get Raspberry Pi 3 Cookbook for Python Programmers - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.