# Making Sense of this Error Message

I am using a book and a video to learn how to use KNN method to classify movies according to their genres.This is my code:

import numpy as np
import pandas as pd

r_cols = ['user_id', 'movie_id', 'rating']

movieProperties = ratings.groupby('movie_id').agg({'rating': [np.size, np.mean]})

movieNumRatings = pd.DataFrame(movieProperties['rating']['size'])
movieNormalizedNumRatings = movieNumRatings.apply(lambda x: (x - np.min(x)) / (np.max(x) - np.min(x)))

movieDict = {}
temp = ''
for line in f:
fields = line.rstrip('\n').split('|')
movieID = int(fields)
name = fields
genres = fields[5:25]
genres = map(int, genres)
movieDict[movieID] = (name, genres,
movieNormalizedNumRatings.loc[movieID].get('size'),           movieProperties.loc[movieID].rating.get('mean'))

print(movieDict)

from scipy import spatial
def ComputeDistance(a, b):
genresA = a
genresB = b
genreDistance = spatial.distance.cosine(genresA, genresB)
popularityA = a
popularityB = b
popularityDistance = abs(popularityA - popularityB)
return genreDistance + popularityDistance        # Everything seems fine up to this point. But the problem seems to be the next line of code.

print(ComputeDistance(movieDict, movieDict))


Note: This code can be found here: https://hendra-herviawan.github.io/Movie-Recommendation-based-on-KNN-K-Nearest-Neighbors.html

Note: This code can also be found on pg 245-250, chp7, Hands-On Data Science and Python Machine Learning by Frank Kane. I have an e-copy and I can send it on request.

Note: I don't know how to share the csv files here. I will appreciate it if somebody can tell me how I can do that here.

• What are the types of movieDict and movieDict (it is the objets which are passed to spatial.distance.cosine)? – Stanislas Morbieu Nov 3 '19 at 12:55
• Please avoid giving code as an image, see meta.stackoverflow.com/a/285557/891919 – Erwan Nov 3 '19 at 23:48
• The return genreDistance([...]) line is strange. genreDistance is a double precision float, it cannot be called as a function. Not sure what is the point of this instruction. – Romain Reboulleau Nov 4 '19 at 6:01
• Ok, now what's the error message and the line raising it? – Romain Reboulleau Nov 4 '19 at 22:02
• @MrProf please edit the question to include add the error, not only in comments. – Itamar Mushkin Nov 5 '19 at 6:22

I think I have bumped into what works. I am posting the correct code since somebody may need this someday:

import numpy as np

import pandas as pd

r_cols = ['user_id', 'movie_id', 'rating']

movieProperties = ratings.groupby('movie_id').agg({'rating': [np.size, np.mean]})

movieNumRatings = pd.DataFrame(movieProperties['rating']['size'])

movieNormalizedNumRatings = movieNumRatings.apply(lambda x: (x - np.min(x)) / (np.max(x) - np.min(x)))

movieDict = {}

temp = ''

for line in f:

fields = line.rstrip('\n').split('|')

movieID = int(fields)

name = fields

genres = fields[5:25]

genres = map(int, genres)

movieDict[movieID] = (name, genres, movieNormalizedNumRatings.loc[movieID].get('size'), movieProperties.loc[movieID].rating.get('mean'))

print(movieDict)

from scipy import spatial

def ComputeDistance(a, b):

genresA = np.array(list(a))

genresB = np.array(list(b))

genreDistance = spatial.distance.cosine(genresA, genresB)

popularityA = np.array(a)

popularityB = np.array(b)

popularityDistance = abs(popularityA - popularityB)

return genreDistance + popularityDistance

print(ComputeDistance(movieDict, movieDict))


I am using this opportunity to thank those who gave me one or two suggestions.