<jupyter_start><jupyter_text>---<jupyter_code>!pip install -q pyreclab
import pandas as pd
import pyreclab
import seaborn as sns
import numpy as np
import scipy.sparse as sparse
import matplotlib.pyplot as plt
columns = ['movieid', 'title', 'release_date', 'video_release_date', \
           'IMDb_URL', 'unknown', 'Action', 'Adventure', 'Animation', \
           'Children', 'Comedy', 'Crime', 'Documentary', 'Drama', 'Fantasy', \
           'Film-Noir', 'Horror', 'Musical', 'Mystery', 'Romance', 'Sci-Fi', \
           'Thriller', 'War', 'Western']
df_train = pd.read_csv('./data/bronze/u2.base',
                         sep='\t',
                         names=['userid', 'itemid', 'rating', 'timestamp'],
                         header=None)

# rating> = 3 is relevant (1) and rating less than 3 is not relevant (0)
df_train.rating = [1 if x >=3 else 0 for x in df_train.rating ]

df_train.head()
df_items = pd.read_csv('./data/bronze/u.item',
                        sep='|',
                        index_col=0,
                        names = columns,
                        header=None, 
                        encoding='latin-1')

df_items.head()
user_id = 2
user_df = df_train[df_train['userid'] == user_id]
df_items.loc[user_df['itemid'].values,['title','release_date']]<jupyter_output><empty_output><jupyter_text>MostPop<jupyter_code>most_popular = pyreclab.MostPopular(dataset='./data/bronze/u2.base',
                   dlmchar=b'\t',
                   header=False,
                   usercol=0,
                   itemcol=1,
                   ratingcol=2)

most_popular.train()
top_n = 10

recommendList, maprec, ndcg = most_popular.testrec(input_file='./data/bronze/u2.test',
                                          dlmchar=b'\t',
                                          header=False,
                                          usercol=0,
                                          itemcol=1,
                                          ratingcol=2,
                                          topn=top_n,
                                          relevance_threshold=2,
                                          includeRated=False)

print('MAP: {}\nNDCG@{}: {}'.format(maprec, top_n, ndcg))
ranking = [int(r) for r in most_popular.recommend(str(user_id), top_n, includeRated=False)]
print('Recommendation for user {}: {}'.format(user_id, ranking))
df_items.loc[ranking,['title','release_date']]<jupyter_output><empty_output><jupyter_text>ItemAvg<jupyter_code>item_avg = pyreclab.ItemAvg(dataset='./data/bronze/u2.base',
                   dlmchar=b'\t',
                   header=False,
                   usercol=0,
                   itemcol=1,
                   ratingcol=2)

item_avg.train()
top_n = 10

recommendList, maprec, ndcg = item_avg.testrec(input_file='./data/bronze/u2.test',
                                          dlmchar=b'\t',
                                          header=False,
                                          usercol=0,
                                          itemcol=1,
                                          ratingcol=2,
                                          topn=top_n,
                                          relevance_threshold=2,
                                          includeRated=False)

print('MAP: {}\nNDCG@{}: {}'.format(maprec, top_n, ndcg))
ranking = [int(r) for r in item_avg.recommend(str(user_id), top_n, includeRated=False)]
print('Recommendation for user {}: {}'.format(user_id, ranking))
df_items.loc[ranking,['title','release_date']]<jupyter_output><empty_output><jupyter_text>UserKNN<jupyter_code>myUserKnn = pyreclab.UserKnn(dataset='./data/bronze/u1.base',
                             dlmchar=b'\t',
                             header=False,
                             usercol=0,
                             itemcol=1,
                             ratingcol=2)

myUserKnn.train(k=7, similarity='pearson')
predlist, mae, rmse = myUserKnn.test(input_file='./data/bronze/u2.test',
                               dlmchar=b'\t',
                               header=False,
                               usercol=0,
                               itemcol=1,
                               ratingcol=2)

print('MAE: {}\nRMSE: {}'.format(mae, rmse))
ranking = [int(r) for r in myUserKnn.recommend(str(user_id), top_n, includeRated=False)]
print('Recommendation for user {}: {}'.format(user_id, ranking))
df_items.loc[ranking,['title','release_date']]<jupyter_output><empty_output><jupyter_text>ItemKNN<jupyter_code>myItemKnn = pyreclab.ItemKnn(dataset='./data/bronze/u1.base',
                             dlmchar=b'\t',
                             header=False,
                             usercol=0,
                             itemcol=1,
                             ratingcol=2)

myItemKnn.train(k=7, similarity='cosine')
predlist, mae, rmse = myItemKnn.test(input_file='./data/bronze/u2.test',
                               dlmchar=b'\t',
                               header=False,
                               usercol=0,
                               itemcol=1,
                               ratingcol=2)

print('MAE: {}\nRMSE: {}'.format(mae, rmse))
ranking = [int(r) for r in myItemKnn.recommend(str(user_id), top_n, includeRated=False)]
print('Recommendation for user {}: {}'.format(user_id, ranking))
df_items.loc[ranking,['title','release_date']]<jupyter_output><empty_output><jupyter_text>FunkSVD<jupyter_code>svd = pyreclab.SVD(dataset='./data/bronze/u2.base',
                   dlmchar=b'\t',
                   header=False,
                   usercol=0,
                   itemcol=1,
                   ratingcol=2)

svd.train(factors=100, maxiter=100, lr=0.01, lamb=0.1)
predlist, mae, rmse = svd.test(input_file='./data/bronze/u2.test',
                               dlmchar=b'\t',
                               header=False,
                               usercol=0,
                               itemcol=1,
                               ratingcol=2)

print('MAE: {}\nRMSE: {}'.format(mae, rmse))
top_n = 20

recommendList, maprec, ndcg = svd.testrec(input_file='./data/bronze/u2.test',
                                          dlmchar=b'\t',
                                          header=False,
                                          usercol=0,
                                          itemcol=1,
                                          ratingcol=2,
                                          topn=top_n,
                                          relevance_threshold=2,
                                          includeRated=False)

print('MAP: {}\nNDCG@{}: {}'.format(maprec, top_n, ndcg))
ranking = [int(r) for r in svd.recommend(str(user_id), top_n, includeRated=False)]
print('Recommendation for user {}: {}'.format(user_id, ranking))
df_items.loc[ranking,['title','release_date']]<jupyter_output><empty_output><jupyter_text>SlopeOne<jupyter_code>slopeone = pyreclab.SlopeOne(dataset='./data/bronze/u2.base',
                   dlmchar=b'\t',
                   header=False,
                   usercol=0,
                   itemcol=1,
                   ratingcol=2)

slopeone.train()
predlist, mae, rmse = slopeone.test(input_file='./data/bronze/u2.test',
                               dlmchar=b'\t',
                               header=False,
                               usercol=0,
                               itemcol=1,
                               ratingcol=2)

print('MAE: {}\nRMSE: {}'.format(mae, rmse))
top_n = 20

recommendList, maprec, ndcg = slopeone.testrec(input_file='./data/bronze/u2.test',
                                          dlmchar=b'\t',
                                          header=False,
                                          usercol=0,
                                          itemcol=1,
                                          ratingcol=2,
                                          topn=top_n,
                                          relevance_threshold=2,
                                          includeRated=False)

print('MAP: {}\nNDCG@{}: {}'.format(maprec, top_n, ndcg))
ranking = [int(r) for r in slopeone.recommend(str(user_id), top_n, includeRated=False)]
print('Recommendation for user {}: {}'.format(user_id, ranking))
df_items.loc[ranking,['title','release_date']]<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12522576760987358, 'fraction_numerical': 0.011077664057796508, 'mean_word_length': 1.3929703255545953, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 64, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '390155', 'license': 'Apache-2.0', 'n_tokens_mistral': 2488, 'n_tokens_neox': 2382, 'n_words': 359, 'path': '_docs/nbs/reco-tut-mlh-t2-pyreclab-library.ipynb', 'repo_name': 'sparsh-ai/recohut', 'size': '8305'}