<jupyter_start><jupyter_text>Model - Baseline<jupyter_code>import sys
sys.path.append("../")
import numpy as np
import pandas as pd
from IPython.display import SVG
import matplotlib.pyplot as plt
import seaborn as sns

from reco.preprocess import encode_user_item, random_split, user_split
%matplotlib inline<jupyter_output><empty_output><jupyter_text>Prepare Data<jupyter_code>df_ratings = pd.read_csv("data/ratings.csv")
df_ratings.head()
DATA = encode_user_item(df_ratings, "user_id", "movie_id", "rating", "unix_timestamp")
DATA.head()
# Data Splitting
#train, test = user_split(DATA, "TIMESTAMP", "USER", [0.75, 0.25])
train, test = random_split(DATA, [0.75, 0.25])
train.shape, test.shape<jupyter_output><empty_output><jupyter_text>Build & Train Model - Baseline (Rating): We will use each user's **mean rating** as the baseline prediction.- Baseline (Ranking): We will use the **top-k popular items** as the baseline ranking<jupyter_code>def average_rating_model(train):
    users_ratings = train.groupby(["USER"])["RATING"].mean()
    users_ratings = users_ratings.reset_index()
    users_ratings.rename(columns = {'RATING': 'RATING_PRED'}, inplace = True)

    return users_ratings
def popular_item_model(train):
    item_counts = (train.groupby("ITEM")
                   .count()
                   .reset_index()
                   .sort_values(ascending = False, by = "USER"))
    item_counts = item_counts[["ITEM", "USER"]]
    item_counts.columns = ['ITEM', 'RATING_PRED']
        
    return item_counts<jupyter_output><empty_output><jupyter_text>Generate Prediction: Rating & Ranking<jupyter_code>predictions_ratings = average_rating_model(train)
predictions_ratings.head()
predictions_ranking = popular_item_model(train)
predictions_ranking.head()<jupyter_output><empty_output><jupyter_text>Evaluate Rating & Ranking<jupyter_code>rating_evaluate_df = pd.merge(test, predictions_ratings, on=['USER'], how='inner')
rating_evaluate_df.head()
from sklearn.metrics import mean_absolute_error, mean_squared_error
mean_absolute_error(rating_evaluate_df.RATING, rating_evaluate_df.RATING_PRED)<jupyter_output><empty_output><jupyter_text>For Ranking<jupyter_code>from reco.evaluate import user_item_crossjoin, filter_by, get_top_k_items
from reco.evaluate import precision_at_k, recall_at_k, ndcg_at_k
def recommend_topk(data, train, k=5):
    
    """
    Params:
        data (pandas.DataFrame): DataFrame of entire rating data
        train (pandas.DataFrame): DataFrame of train rating data
        k (int): number of items for each user

    Returns:
        pd.DataFrame: DataFrame of top k items for each user, sorted by `col_user` and `rank`
    
    """
    
    # Create the crossjoin for user-item
    user_item = user_item_crossjoin(data)
    
    # Get ranking prediction for each user-item,
    all_predictions = pd.merge(user_item, predictions_ranking, on="ITEM", how="left")
    
    # Handle Missing Values
    all_predictions.fillna(0, inplace=True)
    
    # Filter already seen items
    all_predictions_unseen = filter_by(all_predictions, train, ["USER", "ITEM"])
    
    
    recommend_topk_df = get_top_k_items(all_predictions_unseen, "USER", "RATING_PRED", k=5)
    
    return recommend_topk_df

ranking_topk = recommend_topk(DATA, train, 10)
ranking_topk.head()
eval_precision = precision_at_k(test, ranking_topk, k=10)
eval_recall = recall_at_k(test, ranking_topk, k=10)
eval_ndcg = ndcg_at_k(test, ranking_topk, k=10)

print("NDCG@K:\t%f" % eval_ndcg,
      "Precision@K:\t%f" % eval_precision,
      "Recall@K:\t%f" % eval_recall, sep='\n')<jupyter_output>NDCG@K:	0.170889
Precision@K:	0.117024
Recall@K:	0.069829
{'fraction_non_alphanumeric': 0.11415275116342732, 'fraction_numerical': 0.012044894607172188, 'mean_word_length': 5.049668874172186, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '600095', 'license': 'MIT', 'n_tokens_mistral': 1335, 'n_tokens_neox': 1272, 'n_words': 260, 'path': '06-Model-Baseline.ipynb', 'repo_name': 'Abhishek-Aditya-bs/Recommender-System-MovieLens', 'size': '3653'}