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 10, 2019 in Python 633 views

+1 vote

## ValueError: operands could not be broadcast together with shapes (3,) (1000,)

This is the part of my code, why ...READ MORE

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

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

+1 vote

## ValueError: could not broadcast input array from shape (2) into shape (5)

I was implementing gillespie algorithm and when ...READ MORE

## ValueError: could not broadcast input array from shape (4,1) into shape (4)

Hey @Giorgio, You can try this hope this ...READ MORE

## ValueError: could not broadcast input array from shape (360,270,3) into shape (360,280,3)

Hi@akhtar, In the above error it shows could not ...READ MORE

+1 vote

## ImportError: DLL load failed: The specified module could not be found.

Hi@akhtar, You may get this error if you ...READ MORE

+1 vote

+1 vote

## Can anyone help me with this error: TypeError: list indices must be integers or slices, not str

Hi, @Varshap  It’s a TypeError, which tells us ...READ MORE