Skip to content

Instantly share code, notes, and snippets.

View GabsGear's full-sized avatar
🐢
Baby steps

Gabriel Ghellere GabsGear

🐢
Baby steps
View GitHub Profile
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
from surprise import Dataset, evaluate
from surprise import KNNBasic
data = Dataset.load_builtin("ml-100k")
trainingSet = data.build_full_trainset()
knn = KNNBasic(sim_options={
'name': 'cosine',
'user_based': False })
knn.train(trainingSet)
testSet = trainingSet.build_anti_testset()
predictions = knn.test(testSet)
from collections import defaultdict
def get_top3_recommendations(predictions, topN = 3):
top_recs = defaultdict(list)
for uid, iid, true_r, est, _ in predictions:
top_recs[uid].append((iid, est))
import os, io
def read_item_names():
"""Read the u.item file from MovieLens 100-k dataset and returns a
mapping to convert raw ids into movie names.
"""
file_name = (os.path.expanduser('~') +
'/.surprise_data/ml-100k/ml-100k/u.item')
rid_to_name = {}
import os, io
def read_item_names():
"""Read the u.item file from MovieLens 100-k dataset and returns a
mapping to convert raw ids into movie names.
"""
file_name = (os.path.expanduser('~') +
'/.surprise_data/ml-100k/ml-100k/u.item')
rid_to_name = {}
196 ['Very Natural Thing, A (1974)', 'Walk in the Sun, A (1945)', 'War at Home, The (1996)']
186 ['Mamma Roma (1962)', 'Conspiracy Theory (1997)', 'Toy Story (1995)']
22 ['Entertaining Angels: The Dorothy Day Story (1996)', 'King of New York (1990)', 'Usual Suspects, The (1995)']
244 ['Other Voices, Other Rooms (1997)', 'Big Bang Theory, The (1994)', 'Godfather, The (1972)']
166 ['Mamma Roma (1962)', 'Delta of Venus (1994)', 'Carmen Miranda: Bananas Is My Business (1994)']
298 ['North by Northwest (1959)', 'Pinocchio (1940)', 'Amadeus (1984)']
115 ['2001: A Space Odyssey (1968)', 'Clockwork Orange, A (1971)', 'Three Colors: White (1994)']
253 ['Entertaining Angels: The Dorothy Day Story (1996)', 'Michael (1996)', 'Empire Strikes Back, The (1980)']
305 ['Lone Star (1996)', 'African Queen, The (1951)', 'My Left Foot (1989)']
6 ['Bullets Over Broadway (1994)', 'Rosewood (1997)', 'Rear Window (1954)']