How to do it...

  1. We will create a new Python file and import the following packages into it:
import json 
import numpy as np  
  1. To calculate the Euclidean score between two users, we will define a new function. Let's check the presence of the users in the database:
# The following code will return the Euclidean distance score between user1 and user2: 
 
def euclidean_dist_score(dataset, FirstUser, SecondUser): 
  if FirstUser not in dataset: 
    raiseTypeError('User ' + FirstUser + ' not present in the dataset') 
  if SecondUser not in dataset: 
    raiseTypeError('User ' + SecondUser + ' not present in the dataset') 
  1. We will now extract the movies that have been rated by both users. Then we will compute the score:
  # Movies rated by both FirstUser and SecondUser 
  Both_User_rated = {} 
  for element in dataset[FirstUser]: 
    if element in dataset[SecondUser]: 
      Both_User_rated[element] = 1
  1. No movies in common indicates no similarities between the first and second user. (otherwise unable to compute the ratings in database):
  # Score 0 indicate no common movies 
  if len(Both_User_rated) == 0: 
    return 0
  1. If the ratings are common, calculate the sum of the squared differences, compute the square root of the result obtained, and then normalize it. The score will now be between zero and one:
  SquareDifference = [] 
  for element in dataset[FirstUser]: 
    if element in dataset[SecondUser]: 
      SquareDifference.append(np.square(dataset[FirstUser][element] - 
dataset[SecondUser][element]))    
  return 1 / (1 + np.sqrt(np.sum(SquareDifference))) 

If both user ratings are same, then sum of squared differences will be a small value. Therefore, the score will be high. This the aim here.

  1. We will name our data file movie_rates.json. We will now load it:
if __name__=='__main__': 
  data_file = 'movie_rates.json' 
  with open(data_file, 's') as m: 
    data = json.loads(m.read()) 
  1. Let's calculate the Euclidean distance score for two random users:
FirstUser = 'Steven Ferndndes' 
SecondUser = 'Ramesh Nayak' 
print "nEuclidean score:" 
print euclidean_dist_score(data, FirstUser, SecondUser) 
  1. The preceding code will print the Euclidean distance score in the Terminal:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.179.225