Recommendation (SVD) Inference

# https://github.com/NicolasHug/Surprise
from surprise import SVD, Dataset
from surprise.dump import load
from collections import defaultdict
import pandas as pd

def get_top_n(predictions, n=10):
    """Return the top-N recommendation for each user from a set of predictions.
    Args:
        predictions(list of Prediction objects): The list of predictions, as
            returned by the test method of an algorithm.
        n(int): The number of recommendation to output for each user. Default
            is 10.
    Returns:
    A dict where keys are user (raw) ids and values are lists of tuples:
        [(raw item id, rating estimation), ...] of size n.
    """

    # First map the predictions to each user.
    top_n = defaultdict(list)
    for uid, iid, true_r, est, _ in predictions:
        top_n[uid].append((iid, est))

    # Then sort the predictions for each user and retrieve the k highest ones.
    for uid, user_ratings in top_n.items():
        user_ratings.sort(key=lambda x: x[1], reverse=True)
        top_n[uid] = user_ratings[:n]

    return top_n
df = pd.read_csv('./movies.dat',sep="::",header=None,engine='python')
df.columns = ['iid','name','genre']
df.set_index('iid',inplace=True)
predictions, algo = load('./surprise_model')

Output:

   0                                   1                             2
0  1                    Toy Story (1995)   Animation|Children's|Comedy
1  2                      Jumanji (1995)  Adventure|Children's|Fantasy
2  3             Grumpier Old Men (1995)                Comedy|Romance
3  4            Waiting to Exhale (1995)                  Comedy|Drama
4  5  Father of the Bride Part II (1995)                        Comedy
algo: SVD
top_n = get_top_n(predictions, n=5)
# Print the recommended items for each user
limit = 0
for uid, user_ratings in top_n.items():
    print('\nUser:',uid)
    seen = [df.loc[int(iid),'name'] for (iid, _) in algo.trainset.ur[int(uid)]]
    if len(seen) > 10: seen = seen[:10]
    print('\tSeen:',seen)
    print('\tRecommendations:',[df.loc[int(iid),'name'] for (iid, _) in user_ratings])
    limit+=1
    if limit>3:
        break

Output:

User: 196
    Seen: ['Richie Rich (1994)', 'Getaway, The (1994)', 'Batman Forever (1995)', 'Feast of July (1995)', 'Heidi Fleiss: Hollywood Madam (1995)', 'Shadows (Cienie) (1988)', 'Terminator 2: Judgment Day (1991)', "Nobody's Fool (1994)", "Breakfast at Tiffany's (1961)", 'Basic Instinct (1992)']
    Recommendations: ['Age of Innocence, The (1993)', 'Bio-Dome (1996)', 'Strawberry and Chocolate (Fresa y chocolate) (1993)', 'Guardian Angel (1994)', "Carlito's Way (1993)"]

User: 186
    Seen: ['Double Happiness (1994)', 'Mr. Jones (1993)', 'War Room, The (1993)', 'Bloodsport 2 (1995)', 'Usual Suspects, The (1995)', 'Big Green, The (1995)', 'Mighty Morphin Power Rangers: The Movie (1995)', 'Boys on the Side (1995)', 'Cold Fever (� k�ldum klaka) (1994)', 'Sum of Us, The (1994)']
    Recommendations: ['Lightning Jack (1994)', 'Robocop 3 (1993)', 'Walk in the Clouds, A (1995)', 'Living in Oblivion (1995)', 'Strawberry and Chocolate (Fresa y chocolate) (1993)']

User: 22
    Seen: ['Assassins (1995)', 'Nico Icon (1995)', 'From the Journals of Jean Seberg (1995)', 'Last Summer in the Hamptons (1995)', 'Down Periscope (1996)', 'Bushwhacked (1995)', 'Beyond Bedlam (1993)', 'Client, The (1994)', 'Hoop Dreams (1994)', 'Ladybird Ladybird (1994)']
    Recommendations: ['Home for the Holidays (1995)', 'Age of Innocence, The (1993)', 'Balto (1995)', 'City Hall (1996)', 'Ready to Wear (Pret-A-Porter) (1994)']

User: 244
    Seen: ['When Night Is Falling (1995)', 'Birdcage, The (1996)', '8 Seconds (1994)', 'Foreign Student (1994)', 'Mighty Aphrodite (1995)', 'Before Sunrise (1995)', 'Lion King, The (1994)', 'Clockers (1995)', 'Underneath, The (1995)', 'Manny & Lo (1996)']
    Recommendations: ['Century (1993)', 'Balto (1995)', 'Age of Innocence, The (1993)', 'Remains of the Day, The (1993)', 'Jimmy Hollywood (1994)']