Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # eval recommendations from MovieLens data
- # tested it on DSSTNE, because it has straightforward steps to getting recommendations
- # to use to test this
- # fills in rec_titles and data_titles - a sorted list of recommended titles and original titles popularity
- # missing_titles is the list of movies not recommended from the original dataset
- RECS_FILE = '[your path to "recs" file from DSSTNE]'
- TITLES_FILE = '[your path to "movies.csv" from the MovieLens .zip file]'
- ORIGINAL_DATA = '[your path to "ml-20m_ratings.csv" from MovieLens .zip file]'
- data = {}
- with open(ORIGINAL_DATA, 'r') as file:
- line = file.readline() # skip header
- line = file.readline()
- while line:
- line = line[line.index(',')+1:]
- index = int(line[:line.index(',')])
- data[index] = data.get(index, 0) + 1
- line = file.readline()
- recs = {}
- with open(RECS_FILE, 'r') as file:
- line = file.readline()
- while line:
- #usr_id = line[:line.index('\t')]
- line = line[line.index('\t')+1:]
- for _ in range(10):
- index = int(line[:line.index(',')])
- recs[index] = recs.get(index, 0) + 1
- line = line[line.index(':')+1:]
- line = file.readline()
- movies = {}
- with open(TITLES_FILE, 'r') as file:
- line = file.readline() # skip header
- line = file.readline()
- while line:
- index = line[:line.index(',')]
- index = int(index)
- line = line[line.index(',')+1:]
- if line[0] == '"':
- line = line[1:]
- title = line[:line.index('"')]
- else:
- title = line[:line.index(',')]
- movies[index] = title
- line = file.readline()
- rec_titles = []
- for k,v in recs.items():
- rec_titles.append( (v, data[k], movies[k]) )
- rec_titles.sort(reverse=True)
- data_titles = []
- for k,v in data.items():
- data_titles.append( (v, movies[k]) )
- data_titles.sort(reverse=True)
- missing_titles = []
- for k,v in data.items():
- if k not in recs:
- missing_titles.append( (v, movies[k]) )
- missing_titles.sort(reverse=True)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement