我读了 Programming.Collective.Intelligence 一书中的例子,不知道为什么会出现keyError ...
u.item
movie_id | title | ...
1|Toy Story (1995)|...
2|GoldenEye (1995)|...
...
u.data
user movieid rating timestamp
196 242 3 881250949
186 302 3 891717742
...
码
def loadMovieLens(path='E:/project/datamining/collective/chapter2/ml-100k'):
# Get movie titles
movies = {}
for line in open(path+'/u.item', encoding='utf-8'):
(id, title) = line.split('|')[0:2]
movies[id] = title
# load data
prefs = {}
for line in open(path+'/u.data',encoding='utf-8'):
(user,movieid,rating,ts)=line.split('\t')
prefs.setdefault(user,{})
prefs[user][movies[movieid]] = float(rating)
return prefs
prefs = loadMovieLens()
Traceback (most recent call last):
File "E:\project\datamining\collective\chapter2\recommendations.py", line 181, in <module>
prefs = loadMovieLens()
File "E:\project\datamining\collective\chapter2\recommendations.py", line 179, in loadMovieLens
prefs[user][movies[movieid]] = rating
KeyError: '1'
答案 0 :(得分:0)
尝试错误处理。 类似的东西:
errors = 0
for line in open(path + '/ratings.txt'):
try:
(user, movieid, rating, ts) = line.split('\t')
prefs.setdefault(user, {})
prefs[user][movies[movieid]] = float(rating)
except KeyError:
errors+=1
return prefs