I looked online and cant seem to understand much of it. im new to python and was wondering how i can fix this.
when running:
results = getRecommendations(userCompare[0], userCompare[0]['1'], sim_distance)
i get error:
TypeError Traceback (most recent call last)
<ipython-input-147-4d74cac55074> in <module>()
----> 1 results = getRecommendations(userCompare[0], userCompare[0]['1'], sim_distance)
<ipython-input-54-5f2d7e0dd3ba> in getRecommendations(data, person, similarity)
5 for other in data:
6 if other==person: continue #dont compare self
----> 7 sim=similarity(data, person, other)
8 if sim<=0: continue #ignore scores of 0 or lower
9 for item in data[other]:
<ipython-input-146-b30288308fee> in sim_distance(data, c1, c2)
2 def sim_distance(data, c1, c2):
3 si = {} #get the list of shared items
----> 4 for item in data[c1]:
5 if item in data[c2]:
6 si[item] = 1
TypeError: unhashable type: 'dict'
to create userCompare i did the following:
movies = {}
prefsList = []
def loadMovieLens(path = directory):
# Get movie titles
for line in open(path + 'u.item'):
(id, title) = line.split('|')[0:2]
movies[id] = title
# Load data
for k in range(len(centroidsM)):
prefs ={}
for rows in range(len(centroidsM[k])):
for columns in range(len(centroidsM[k][0,:])):
user = str(rows+1)
movieid =str(columns+1)
prefs.setdefault(user,{})
prefs[user][movies[movieid]] = float(centroidsM[k][rows,columns])
prefsList.append(prefs)
return prefsList
I basically had an array of centroids with different K values, each K value has is a kx1682 matrix (k meaning number of clusters) so i loaded that into a list of dicts. i hope this makes sense. im starting to hate python or atleast dicts.