I am working on how to use KNN to predict a rating for a movie. I use a video and a book to teach myself how to go about it

I tried to run the code I found in the book but it gave me error message. I googled the error message so as to understand it and fix my problem but I don't think I know how to adapt the solutions to my problem. The code is given below:

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))

import operator

def getNeighbors(movieID, K):

distances = []

for movie in movieDict:

if (movie != movieID):

dist = ComputeDistance(movieDict[movieID], movieDict[movie])

distances.append((movie, dist))

distances.sort(key=operator.itemgetter(1))

neighbors = []

for x in range(K):

neighbors.append(distance[x])

return neighbors

K = 10
avgRating = 0

neighbors = getNeighbors(1, K)

I got this error message from PowerShell:

Traceback(most recent call last):

neighbors = getNeighbors(1, K)

dist = ComputeDistance(movieDict[movieID], movieDict[movie])

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

return correlation(u, v, w=w, centered=False)

uv = np.average(u*v, weights=w)

ValueError: operands could not be broadcast together with shape (19,)(0,)

I got this error message when I tried to debug the problem from ipython terminal:

c:\programdata\anaconda3\lib\site-packages\scipy\spatial\distance.py(695)correlation()

693        u = u - umu

694        v = v - vmu

--->  695        uv = np.average(u*v, weights=w)

696        uu = np.average(np.square(u), weights=w)

697        vv = np.average(np.square(v), weights=w) Nov 9 in Python 24 views

## Python Error ""ValueError: could not broadcast input array from shape (4) into shape (1000)""

There are better ways of achieving the ...READ MORE

## Python error: No handlers could be found for logger “xhtml2pdf”

The only solution is to define a ...READ MORE

## print(instance(0).id) TypeError: 'list' object is not callable

Hey @Suraj, For me its working fine. ...READ MORE

## Python pandas: TypeError: tuple indices must be integers or slices, not str

In fact, I get a perfectly good ...READ MORE

## Python Selenium: ConnectionRefusedError: No connection could be made because the target machine actively refused it

When you use driver.quit(), it closes the ...READ MORE

## How to remove elements from a list that may or may not be present?

In case I want to remove some ...READ MORE

+1 vote