# Operands Could not be Broadcast with Shapes (19,)(0,)

I have googled and read something similar to the problem I have but I do not seem to know how to fix the error I got from this particular code:

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)

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


The full 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 = 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)
$$$$
`
• Please detail which line is raising the issue and add the full stack trace. Please also note that pure code questions belong to StackOverflow, where you could surely get better answers, and faster. – Romain Reboulleau Nov 8 at 6:22
• @ Romain Reboulleau I just did that. You can check out the question on StackOverflow. It has the title "Operands Could not be Broadcast with Shapes (19,)(0,) — KNN". Thanks – Mr Prof Nov 9 at 13:59