最初,我尝试将我的数据集读入字典
with open("msong.csv") as f:
reader = csv.DictReader(f)
data = [r for r in reader]
字典包含类似这样的数据:
[{'': '0',
'artist_name': 'Jack Johnson',
'listen_count': '1',
'release': 'Thicker Than Water',
'song_id': 'SOAKIMP12A8C130995',
'title': 'The Cove',
'user_id': 'b80344d063b5ccb3212f76538f3d9e43d87dca9e',
'year': '0'},
{'': '1',
'artist_name': 'Paco De Lucia',
'listen_count': '2',
'release': 'Flamenco Para Ni\xc3\xb1os',
'song_id': 'SOBBMDR12A8C13253B',
'title': 'Entre Dos Aguas',
'user_id': 'b80344d063b5ccb3212f76538f3d9e43d87dca9e',
'year': '1976'},
我正在使用该功能根据用户口味推荐歌曲
def recommend(person, bound, similarity=pearson_similarity):
scores = [(similarity(person, other), other) for other in data if other != person]
scores.sort()
scores.reverse()
scores = scores[0:bound]
print (scores)
recomms = {}
for sim, other in scores:
ranked = data[other]
for itm in ranked:
if itm not in data[person]:
weight = sim * ranked[itm]
if itm in recomms:
s, weights = recomms[itm]
recomms[itm] = (s + sim, weights + [weight])
else:
recomms[itm] = (sim, [weight])
for r in recomms:
sim, item = recomms[r]
recomms[r] = sum(item) / sim
return recomms
在提供输入时,我收到一个错误
recommend(data[0][user_id],4,euclidean_similarity)
错误:推荐(数据[0][user_id],4,euclidean_similarity)