REK-proj-2/project_2_recommender_and_evaluation-Copy1.ipynb
2021-06-28 20:18:14 +02:00

64 KiB
Raw Blame History

%matplotlib inline
%load_ext autoreload
%autoreload 2

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from IPython.display import Markdown, display, HTML
from collections import defaultdict

import torch
import torch.nn as nn
import torch.optim as optim
from livelossplot import PlotLosses

# Fix the dying kernel problem (only a problem in some installations - you can remove it, if it works without it)
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload

Load the dataset for recommenders

data_path = os.path.join("data", "hotel_data")

interactions_df = pd.read_csv(os.path.join(data_path, "hotel_data_interactions_df.csv"), index_col=0)

base_item_features = ['term', 'length_of_stay_bucket', 'rate_plan', 'room_segment', 'n_people_bucket', 'weekend_stay']

column_values_dict = {
    'term': ['WinterVacation', 'Easter', 'OffSeason', 'HighSeason', 'LowSeason', 'MayLongWeekend', 'NewYear', 'Christmas'],
    'length_of_stay_bucket': ['[0-1]', '[2-3]', '[4-7]', '[8-inf]'],
    'rate_plan': ['Standard', 'Nonref'],
    'room_segment': ['[0-160]', '[160-260]', '[260-360]', '[360-500]', '[500-900]'],
    'n_people_bucket': ['[1-1]', '[2-2]', '[3-4]', '[5-inf]'],
    'weekend_stay': ['True', 'False']
}

interactions_df.loc[:, 'term'] = pd.Categorical(
    interactions_df['term'], categories=column_values_dict['term'])
interactions_df.loc[:, 'length_of_stay_bucket'] = pd.Categorical(
    interactions_df['length_of_stay_bucket'], categories=column_values_dict['length_of_stay_bucket'])
interactions_df.loc[:, 'rate_plan'] = pd.Categorical(
    interactions_df['rate_plan'], categories=column_values_dict['rate_plan'])
interactions_df.loc[:, 'room_segment'] = pd.Categorical(
    interactions_df['room_segment'], categories=column_values_dict['room_segment'])
interactions_df.loc[:, 'n_people_bucket'] = pd.Categorical(
    interactions_df['n_people_bucket'], categories=column_values_dict['n_people_bucket'])
interactions_df.loc[:, 'weekend_stay'] = interactions_df['weekend_stay'].astype('str')
interactions_df.loc[:, 'weekend_stay'] = pd.Categorical(
    interactions_df['weekend_stay'], categories=column_values_dict['weekend_stay'])

display(HTML(interactions_df.head(15).to_html()))
user_id item_id term length_of_stay_bucket rate_plan room_segment n_people_bucket weekend_stay
0 1 0 WinterVacation [2-3] Standard [260-360] [5-inf] True
1 2 1 WinterVacation [2-3] Standard [160-260] [3-4] True
2 3 2 WinterVacation [2-3] Standard [160-260] [2-2] False
3 4 3 WinterVacation [4-7] Standard [160-260] [3-4] True
4 5 4 WinterVacation [4-7] Standard [0-160] [2-2] True
5 6 5 Easter [4-7] Standard [260-360] [5-inf] True
6 7 6 OffSeason [2-3] Standard [260-360] [5-inf] True
7 8 7 HighSeason [2-3] Standard [160-260] [1-1] True
8 9 8 HighSeason [2-3] Standard [0-160] [1-1] True
9 8 7 HighSeason [2-3] Standard [160-260] [1-1] True
10 8 7 HighSeason [2-3] Standard [160-260] [1-1] True
11 10 9 HighSeason [2-3] Standard [160-260] [3-4] True
12 11 9 HighSeason [2-3] Standard [160-260] [3-4] True
13 12 10 HighSeason [8-inf] Standard [160-260] [3-4] True
14 14 11 HighSeason [2-3] Standard [0-160] [3-4] True

(Optional) Prepare numerical user features

The method below is left here for convenience if you want to experiment with content-based user features as an input for your neural network.

def n_to_p(l):
    n = sum(l)
    return [x / n for x in l] if n > 0 else l

def calculate_p(x, values):
    counts = [0]*len(values)
    for v in x:
        counts[values.index(v)] += 1

    return n_to_p(counts)

def prepare_users_df(interactions_df):

    users_df = interactions_df.loc[:, ["user_id"]]
    users_df = users_df.groupby("user_id").first().reset_index(drop=False)
    
    user_features = []

    for column in base_item_features:

        column_values = column_values_dict[column]
        df = interactions_df.loc[:, ['user_id', column]]
        df = df.groupby('user_id').aggregate(lambda x: list(x)).reset_index(drop=False)

        def calc_p(x):
            return calculate_p(x, column_values)

        df.loc[:, column] = df[column].apply(lambda x: calc_p(x))

        p_columns = []
        for i in range(len(column_values)):
            p_columns.append("user_" + column + "_" + column_values[i])
            df.loc[:, p_columns[i]] = df[column].apply(lambda x: x[i])
            user_features.append(p_columns[i])

        users_df = pd.merge(users_df, df.loc[:, ['user_id'] + p_columns], on=["user_id"])
    
    return users_df, user_features
    

users_df, user_features = prepare_users_df(interactions_df)

print(user_features)

display(HTML(users_df.loc[users_df['user_id'].isin([706, 1736, 7779, 96, 1, 50, 115])].head(15).to_html()))
['user_term_WinterVacation', 'user_term_Easter', 'user_term_OffSeason', 'user_term_HighSeason', 'user_term_LowSeason', 'user_term_MayLongWeekend', 'user_term_NewYear', 'user_term_Christmas', 'user_length_of_stay_bucket_[0-1]', 'user_length_of_stay_bucket_[2-3]', 'user_length_of_stay_bucket_[4-7]', 'user_length_of_stay_bucket_[8-inf]', 'user_rate_plan_Standard', 'user_rate_plan_Nonref', 'user_room_segment_[0-160]', 'user_room_segment_[160-260]', 'user_room_segment_[260-360]', 'user_room_segment_[360-500]', 'user_room_segment_[500-900]', 'user_n_people_bucket_[1-1]', 'user_n_people_bucket_[2-2]', 'user_n_people_bucket_[3-4]', 'user_n_people_bucket_[5-inf]', 'user_weekend_stay_True', 'user_weekend_stay_False']
user_id user_term_WinterVacation user_term_Easter user_term_OffSeason user_term_HighSeason user_term_LowSeason user_term_MayLongWeekend user_term_NewYear user_term_Christmas user_length_of_stay_bucket_[0-1] user_length_of_stay_bucket_[2-3] user_length_of_stay_bucket_[4-7] user_length_of_stay_bucket_[8-inf] user_rate_plan_Standard user_rate_plan_Nonref user_room_segment_[0-160] user_room_segment_[160-260] user_room_segment_[260-360] user_room_segment_[360-500] user_room_segment_[500-900] user_n_people_bucket_[1-1] user_n_people_bucket_[2-2] user_n_people_bucket_[3-4] user_n_people_bucket_[5-inf] user_weekend_stay_True user_weekend_stay_False
0 1 0.130435 0.0 0.652174 0.086957 0.130435 0.000000 0.000000 0.000000 0.000000 0.608696 0.391304 0.000000 0.521739 0.478261 0.000000 0.869565 0.130435 0.000000 0.0 0.000000 0.739130 0.173913 0.086957 0.782609 0.217391
47 50 0.043478 0.0 0.434783 0.304348 0.217391 0.000000 0.000000 0.000000 0.000000 0.913043 0.086957 0.000000 0.260870 0.739130 0.000000 0.565217 0.434783 0.000000 0.0 0.000000 0.173913 0.521739 0.304348 0.782609 0.217391
92 96 0.083333 0.0 0.708333 0.125000 0.041667 0.041667 0.000000 0.000000 0.250000 0.666667 0.041667 0.041667 0.291667 0.708333 0.125000 0.791667 0.083333 0.000000 0.0 0.041667 0.333333 0.541667 0.083333 0.750000 0.250000
111 115 0.727273 0.0 0.272727 0.000000 0.000000 0.000000 0.000000 0.000000 0.500000 0.363636 0.136364 0.000000 1.000000 0.000000 0.000000 0.818182 0.181818 0.000000 0.0 0.818182 0.090909 0.045455 0.045455 0.363636 0.636364
675 706 0.091988 0.0 0.451039 0.189911 0.207715 0.038576 0.011869 0.008902 0.169139 0.459941 0.272997 0.097923 0.994065 0.005935 0.020772 0.839763 0.130564 0.008902 0.0 0.041543 0.094955 0.738872 0.124629 0.676558 0.323442
1699 1736 0.034483 0.0 0.482759 0.206897 0.275862 0.000000 0.000000 0.000000 0.241379 0.551724 0.206897 0.000000 0.172414 0.827586 0.000000 0.931034 0.068966 0.000000 0.0 0.379310 0.413793 0.206897 0.000000 0.448276 0.551724
7639 7779 0.037037 0.0 0.296296 0.259259 0.370370 0.000000 0.000000 0.037037 0.111111 0.296296 0.481481 0.111111 1.000000 0.000000 0.000000 0.814815 0.185185 0.000000 0.0 0.000000 0.037037 0.740741 0.222222 0.814815 0.185185

(Optional) Prepare numerical item features

The method below is left here for convenience if you want to experiment with content-based item features as an input for your neural network.

def map_items_to_onehot(df):
    one_hot = pd.get_dummies(df.loc[:, base_item_features])
    df = df.drop(base_item_features, axis = 1)
    df = df.join(one_hot)
    
    return df, list(one_hot.columns)

def prepare_items_df(interactions_df):
    items_df = interactions_df.loc[:, ["item_id"] + base_item_features].drop_duplicates()
    
    items_df, item_features = map_items_to_onehot(items_df)
    
    return items_df, item_features


items_df, item_features = prepare_items_df(interactions_df)

print(item_features)

display(HTML(items_df.loc[items_df['item_id'].isin([0, 1, 2, 3, 4, 5, 6])].head(15).to_html()))
['term_WinterVacation', 'term_Easter', 'term_OffSeason', 'term_HighSeason', 'term_LowSeason', 'term_MayLongWeekend', 'term_NewYear', 'term_Christmas', 'length_of_stay_bucket_[0-1]', 'length_of_stay_bucket_[2-3]', 'length_of_stay_bucket_[4-7]', 'length_of_stay_bucket_[8-inf]', 'rate_plan_Standard', 'rate_plan_Nonref', 'room_segment_[0-160]', 'room_segment_[160-260]', 'room_segment_[260-360]', 'room_segment_[360-500]', 'room_segment_[500-900]', 'n_people_bucket_[1-1]', 'n_people_bucket_[2-2]', 'n_people_bucket_[3-4]', 'n_people_bucket_[5-inf]', 'weekend_stay_True', 'weekend_stay_False']
item_id term_WinterVacation term_Easter term_OffSeason term_HighSeason term_LowSeason term_MayLongWeekend term_NewYear term_Christmas length_of_stay_bucket_[0-1] length_of_stay_bucket_[2-3] length_of_stay_bucket_[4-7] length_of_stay_bucket_[8-inf] rate_plan_Standard rate_plan_Nonref room_segment_[0-160] room_segment_[160-260] room_segment_[260-360] room_segment_[360-500] room_segment_[500-900] n_people_bucket_[1-1] n_people_bucket_[2-2] n_people_bucket_[3-4] n_people_bucket_[5-inf] weekend_stay_True weekend_stay_False
0 0 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0
1 1 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 0 1 0 1 0
2 2 1 0 0 0 0 0 0 0 0 1 0 0 1 0 0 1 0 0 0 0 1 0 0 0 1
3 3 1 0 0 0 0 0 0 0 0 0 1 0 1 0 0 1 0 0 0 0 0 1 0 1 0
4 4 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 0 1 0
5 5 0 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 1 1 0
6 6 0 0 1 0 0 0 0 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 1 1 0

Neural network recommender

Task:
Code a recommender based on a neural network model. You are free to choose any network architecture you find appropriate. The network can use the interaction vectors for users and items, embeddings of users and items, as well as user and item features (you can use the features you developed in the first project).

Remember to keep control over randomness - in the init method add the seed as a parameter and initialize the random seed generator with that seed (both for numpy and pytorch):

self.seed = seed
self.rng = np.random.RandomState(seed=seed)

in the network model:

self.seed = torch.manual_seed(seed)

You are encouraged to experiment with:

  • the number of layers in the network, the number of neurons and different activation functions,
  • different optimizers and their parameters,
  • batch size and the number of epochs,
  • embedding layers,
  • content-based features of both users and items.
from recommenders.recommender import Recommender


class Net(nn.Module):
    def __init__(self, features_len, output_len):
        super(Net, self).__init__()
        
        print("IN:", features_len, "OUT:", output_len)
        
        self.fc1 = nn.Linear(features_len, 150)
        self.fc2 = nn.Linear(150, 50)
        self.fc3 = nn.Linear(50, 25)
        self.fc4 = nn.Linear(25, output_len+500)
        
    def forward(self, x):
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = F.relu(self.fc3(x))
        return self.fc4(x)

# class Net(nn.Module):
#     def __init__(self, features_len):
#         super(Net, self).__init__()
#         self.hid1 = nn.Linear(features_len, 10)
#         self.hid2 = nn.Linear(10, 10)
#         self.oupt = nn.Linear(10, 1)

#         nn.init.xavier_uniform_(self.hid1.weight)
#         nn.init.zeros_(self.hid1.bias)
#         nn.init.xavier_uniform_(self.hid2.weight)
#         nn.init.zeros_(self.hid2.bias)
#         nn.init.xavier_uniform_(self.oupt.weight)
#         nn.init.zeros_(self.oupt.bias)

#     def forward(self, x):
#         z = torch.tanh(self.hid1(x))
#         z = torch.tanh(self.hid2(z))
#         z = torch.sigmoid(self.oupt(z))
#         return z
    
    
class NNRecommender(Recommender):
    """
    Linear recommender class based on user and item features.
    """
 
    def generate_negative_interaction(self):
        user_ids = interactions_df['user_id']
        item_ids = interactions_df['item_id']
 
        user_id = user_ids.sample().item()
        item_id = item_ids.sample().item()
        positive_interactions = interactions_df.loc[
            (interactions_df['item_id'] == item_id) & (interactions_df['user_id'] == user_id)]
 
        while not positive_interactions.empty:
            user_id = user_ids.sample().item()
            item_id = item_ids.sample().item()
            positive_interactions = interactions_df.loc[
                (interactions_df['item_id'] == item_id) & (interactions_df['user_id'] == user_id)]
 
        return (user_id, item_id, 0)
 
    def generate_negative_interactions(self, n, interactions_df, cross_df):
        combined_dfs = pd.concat([cross_df, interactions_df[['user_id', 'item_id']]])
        return combined_dfs.drop_duplicates(keep=False).sample(n=n)
 
 
    def __init__(self, seed=6789, n_neg_per_pos=5):
        """
        Initialize base recommender params and variables.
        """
        self.model = None
        self.n_neg_per_pos = n_neg_per_pos
 
        self.recommender_df = pd.DataFrame(columns=['user_id', 'item_id', 'score'])
        self.users_df = None
        self.user_features = None
 
        self.seed = seed
        self.rng = np.random.RandomState(seed=seed)
 
    def calculate_accuracy(self, y_true, y_pred):
        predicted = y_pred.ge(.5).view(-1)
        return (y_true == predicted).sum().float() / len(y_true)
 
    def round_tensor(self, t, decimal_places=3):
        return round(t.item(), decimal_places)
 
    def fit(self, interactions_df, users_df, items_df):
        """
        Training of the recommender.
 
        :param pd.DataFrame interactions_df: DataFrame with recorded interactions between users and items 
            defined by user_id, item_id and features of the interaction.
        :param pd.DataFrame users_df: DataFrame with users and their features defined by user_id and the user feature columns.
        :param pd.DataFrame items_df: DataFrame with items and their features defined by item_id and the item feature columns.
        """
 
        interactions_df = interactions_df.copy()
        # Prepare users_df and items_df 
        # (optional - use only if you want to train a hybrid model with content-based features)
 
        users_df, user_features = prepare_users_df(interactions_df)
 
        self.users_df = users_df
        self.user_features = user_features
 
        items_df, item_features = prepare_items_df(interactions_df)
        items_df = items_df.loc[:, ['item_id'] + item_features]
 
        n_epochs = 51

        X = items_df[['term_WinterVacation', 'term_Easter', 'term_OffSeason', 'term_HighSeason', 'term_LowSeason', 'term_MayLongWeekend', 'term_NewYear', 'term_Christmas', 'rate_plan_Standard', 'rate_plan_Nonref', 'room_segment_[0-160]', 'room_segment_[160-260]', 'room_segment_[260-360]', 'room_segment_[360-500]', 'room_segment_[500-900]', 'n_people_bucket_[1-1]', 'n_people_bucket_[2-2]', 'n_people_bucket_[3-4]', 'n_people_bucket_[5-inf]', 'weekend_stay_True', 'weekend_stay_False']]
        y = items_df[['item_id']]
        X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=self.seed)
 
        X_train = torch.from_numpy(X_train.to_numpy()).float()
        y_train = torch.squeeze(torch.from_numpy(y_train.to_numpy()).long())
        X_test = torch.from_numpy(X_test.to_numpy()).float()
        y_test = torch.squeeze(torch.from_numpy(y_test.to_numpy()).long())
 
        self.net = Net(X_train.shape[1], items_df['item_id'].unique().size)
 
        optimizer = optim.Adam(self.net.parameters(), lr=0.05)
        criterion = nn.CrossEntropyLoss()
 
        for epoch in range(n_epochs):
            y_pred = self.net(X_train)
            y_pred = torch.squeeze(y_pred)
            train_loss = criterion(y_pred, y_train)
 
#             if epoch % 5000 == 0:
#                 train_acc = self.calculate_accuracy(y_train, y_pred)
#                 y_test_pred = self.net(X_test)
#                 y_test_pred = torch.squeeze(y_test_pred)
#                 test_loss = criterion(y_test_pred, y_test)
#                 test_acc = self.calculate_accuracy(y_test, y_test_pred)
#                 print(
#         f'''epoch {epoch}
#         Train set - loss: {self.round_tensor(train_loss)}, accuracy: {self.round_tensor(train_acc)}
#         Test  set - loss: {self.round_tensor(test_loss)}, accuracy: {self.round_tensor(test_acc)}
#         ''')
 
            optimizer.zero_grad()
            train_loss.backward()
            optimizer.step()
 
    def recommend(self, users_df, items_df, n_recommendations=1):
        """
        Serving of recommendations. Scores items in items_df for each user in users_df and returns 
        top n_recommendations for each user.
 
        :param pd.DataFrame users_df: DataFrame with users and their features for which recommendations should be generated.
        :param pd.DataFrame items_df: DataFrame with items and their features which should be scored.
        :param int n_recommendations: Number of recommendations to be returned for each user.
        :return: DataFrame with user_id, item_id and score as columns returning n_recommendations top recommendations 
            for each user.
        :rtype: pd.DataFrame
        """
 
        # Clean previous recommendations (iloc could be used alternatively)
        self.recommender_df = self.recommender_df[:0]
 
        # Prepare users_df and items_df
        # (optional - use only if you want to train a hybrid model with content-based features)
 
        users_df = users_df.loc[:, 'user_id']
        users_df = pd.merge(users_df, self.users_df, on=['user_id'], how='left').fillna(0)
 
        #         items_df, item_features = prepare_items_df(items_df)
        #         items_df = items_df.loc[:, ['item_id'] + item_features]
 
        # Score the items
 
        recommendations = pd.DataFrame(columns=['user_id', 'item_id', 'score'])
 
        for ix, user in users_df.iterrows():
            prep_user = torch.from_numpy(user[['user_term_WinterVacation', 'user_term_Easter', 'user_term_OffSeason', 'user_term_HighSeason', 'user_term_LowSeason', 'user_term_MayLongWeekend', 'user_term_NewYear', 'user_term_Christmas', 'user_rate_plan_Standard', 'user_rate_plan_Nonref', 'user_room_segment_[0-160]', 'user_room_segment_[160-260]', 'user_room_segment_[260-360]', 'user_room_segment_[360-500]', 'user_room_segment_[500-900]', 'user_n_people_bucket_[1-1]', 'user_n_people_bucket_[2-2]', 'user_n_people_bucket_[3-4]', 'user_n_people_bucket_[5-inf]', 'user_weekend_stay_True', 'user_weekend_stay_False']].to_numpy()).float()
            
            scores = self.net(prep_user).detach().numpy()
 
            chosen_ids = np.argsort(-scores)[:n_recommendations]
 
            recommendations = []
            for item_id in chosen_ids:
                recommendations.append(
                    {
                        'user_id': user['user_id'],
                        'item_id': item_id,
                        'score': scores[item_id]
                    }
                )
 
            user_recommendations = pd.DataFrame(recommendations)
 
            self.recommender_df = pd.concat([self.recommender_df, user_recommendations])
 
        return self.recommender_df

Quick test of the recommender

items_df = interactions_df.loc[:, ['item_id'] + base_item_features].drop_duplicates()
# Fit method
nn_recommender = NNRecommender()
nn_recommender.fit(interactions_df.head(1000), None, None)
# nn_recommender.fit(interactions_df, None, None)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-23-851d3aa5378e> in <module>
      1 # Fit method
      2 nn_recommender = NNRecommender()
----> 3 nn_recommender.fit(interactions_df.head(1000), None, None)
      4 # nn_recommender.fit(interactions_df, None, None)

<ipython-input-21-ca3049874457> in fit(self, interactions_df, users_df, items_df)
    114         X = items_df[['term_WinterVacation', 'term_Easter', 'term_OffSeason', 'term_HighSeason', 'term_LowSeason', 'term_MayLongWeekend', 'term_NewYear', 'term_Christmas', 'rate_plan_Standard', 'rate_plan_Nonref', 'room_segment_[0-160]', 'room_segment_[160-260]', 'room_segment_[260-360]', 'room_segment_[360-500]', 'room_segment_[500-900]', 'n_people_bucket_[1-1]', 'n_people_bucket_[2-2]', 'n_people_bucket_[3-4]', 'n_people_bucket_[5-inf]', 'weekend_stay_True', 'weekend_stay_False']]
    115         y = items_df[['item_id']]
--> 116         X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=self.seed)
    117 
    118         X_train = torch.from_numpy(X_train.to_numpy()).float()

NameError: name 'train_test_split' is not defined
# Recommender method

recommendations = nn_recommender.recommend(pd.DataFrame([[1]], columns=['user_id']), items_df, 3)

recommendations = pd.merge(recommendations, items_df, on='item_id', how='left')
display(HTML(recommendations.to_html()))
user_id item_id score term length_of_stay_bucket rate_plan room_segment n_people_bucket weekend_stay
0 1.0 88 37.715969 WinterVacation [0-1] Standard [160-260] [2-2] True
1 1.0 57 36.182877 WinterVacation [2-3] Standard [160-260] [2-2] True
2 1.0 69 35.771114 WinterVacation [4-7] Standard [160-260] [2-2] True

Tuning method

from evaluation_and_testing.testing import evaluate_train_test_split_implicit

seed = 6789
from hyperopt import hp, fmin, tpe, Trials
import traceback

def tune_recommender(recommender_class, interactions_df, items_df, 
                     param_space, max_evals=1, show_progressbar=True, seed=6789):
    # Split into train_validation and test sets

    shuffle = np.arange(len(interactions_df))
    rng = np.random.RandomState(seed=seed)
    rng.shuffle(shuffle)
    shuffle = list(shuffle)

    train_test_split = 0.8
    split_index = int(len(interactions_df) * train_test_split)

    train_validation = interactions_df.iloc[shuffle[:split_index]]
    test = interactions_df.iloc[shuffle[split_index:]]

    # Tune

    def loss(tuned_params):
        recommender = recommender_class(seed=seed, **tuned_params)
        hr1, hr3, hr5, hr10, ndcg1, ndcg3, ndcg5, ndcg10 = evaluate_train_test_split_implicit(
            recommender, train_validation, items_df, seed=seed)
        return -hr10

    n_tries = 1
    succeded = False
    try_id = 0
    while not succeded and try_id < n_tries:
        try:
            trials = Trials()
            best_param_set = fmin(loss, space=param_space, algo=tpe.suggest, 
                                  max_evals=max_evals, show_progressbar=show_progressbar, trials=trials, verbose=True)
            succeded = True
        except:
            traceback.print_exc()
            try_id += 1
            
    if not succeded:
        return None
        
    # Validate
    
    recommender = recommender_class(seed=seed, **best_param_set)

    results = [[recommender_class.__name__] + list(evaluate_train_test_split_implicit(
        recommender, {'train': train_validation, 'test': test}, items_df, seed=seed))]

    results = pd.DataFrame(results, 
                           columns=['Recommender', 'HR@1', 'HR@3', 'HR@5', 'HR@10', 'NDCG@1', 'NDCG@3', 'NDCG@5', 'NDCG@10'])

    display(HTML(results.to_html()))
    
    return best_param_set

Tuning of the recommender

Task:
Tune your model using the code below. You only need to put the class name of your recommender and choose an appropriate parameter space.

param_space = {
    'n_neg_per_pos': hp.quniform('n_neg_per_pos', 1, 10, 1)
}
items_df['item_id'].unique().size

best_param_set = tune_recommender(NNRecommender, interactions_df, items_df,
                                  param_space, max_evals=10, show_progressbar=True, seed=seed)

print("Best parameters:")
print(best_param_set)
IN:                                                   
21                                                    
OUT:                                                  
691                                                   
IN:                                                                                
21                                                                                 
OUT:                                                                               
691                                                                                
IN:                                                                                
21                                                                                 
OUT:                                                                               
691                                                                                
IN:                                                                                
21                                                                                 
OUT:                                                                               
691                                                                                
IN:                                                                                
21                                                                                 
OUT:                                                                               
691                                                                                
IN:                                                                                
21                                                                                 
OUT:                                                                               
691                                                                                
IN:                                                                                
21                                                                                 
OUT:                                                                               
691                                                                                
IN:                                                                                
21                                                                                 
OUT:                                                                               
691                                                                                
IN:                                                                                
21                                                                                 
OUT:                                                                               
691                                                                                
IN:                                                                                
21                                                                                 
OUT:                                                                               
691                                                                                
100%|██████████| 10/10 [18:34<00:00, 111.50s/trial, best loss: -0.04424416222859484]
IN: 21 OUT: 736
Recommender HR@1 HR@3 HR@5 HR@10 NDCG@1 NDCG@3 NDCG@5 NDCG@10
0 NNRecommender 0.010201 0.020072 0.026324 0.035538 0.010201 0.01574 0.018216 0.021141
Best parameters:
{'n_neg_per_pos': 9.0}

Final evaluation

Task:
Run the final evaluation of your recommender and present its results against the Amazon and Netflix recommenders' results. You just need to give the class name of your recommender and its tuned parameters below.

nn_recommender = NNRecommender(n_neg_per_pos=9)  # Initialize your recommender here

# Give the name of your recommender in the line below
nn_tts_results = [['NNRecommender'] + list(evaluate_train_test_split_implicit(
    nn_recommender, interactions_df, items_df))]

nn_tts_results = pd.DataFrame(
    nn_tts_results, columns=['Recommender', 'HR@1', 'HR@3', 'HR@5', 'HR@10', 'NDCG@1', 'NDCG@3', 'NDCG@5', 'NDCG@10'])

display(HTML(nn_tts_results.to_html()))
IN: 21 OUT: 736
Recommender HR@1 HR@3 HR@5 HR@10 NDCG@1 NDCG@3 NDCG@5 NDCG@10
0 NNRecommender 0.003949 0.015137 0.019743 0.026654 0.003949 0.010361 0.01223 0.014409
from recommenders.amazon_recommender import AmazonRecommender

amazon_recommender = AmazonRecommender()

amazon_tts_results = [['AmazonRecommender'] + list(evaluate_train_test_split_implicit(
    amazon_recommender, interactions_df, items_df))]

amazon_tts_results = pd.DataFrame(
    amazon_tts_results, columns=['Recommender', 'HR@1', 'HR@3', 'HR@5', 'HR@10', 'NDCG@1', 'NDCG@3', 'NDCG@5', 'NDCG@10'])

display(HTML(amazon_tts_results.to_html()))
Recommender HR@1 HR@3 HR@5 HR@10 NDCG@1 NDCG@3 NDCG@5 NDCG@10
0 AmazonRecommender 0.042119 0.10464 0.140507 0.199408 0.042119 0.076826 0.091797 0.110705
from recommenders.netflix_recommender import NetflixRecommender

netflix_recommender = NetflixRecommender(n_epochs=30, print_type='live')

netflix_tts_results = [['NetflixRecommender'] + list(evaluate_train_test_split_implicit(
    netflix_recommender, interactions_df, items_df))]

netflix_tts_results = pd.DataFrame(
    netflix_tts_results, columns=['Recommender', 'HR@1', 'HR@3', 'HR@5', 'HR@10', 'NDCG@1', 'NDCG@3', 'NDCG@5', 'NDCG@10'])

display(HTML(netflix_tts_results.to_html()))
tts_results = pd.concat([nn_tts_results, amazon_tts_results, netflix_tts_results]).reset_index(drop=True)
display(HTML(tts_results.to_html()))

Summary

Task:
Write a summary of your experiments. What worked well and what did not? What are your thoughts how could you possibly further improve the model?