<jupyter_start><jupyter_text>https://github.com/massquantity/LibRecommender<jupyter_code>import implicit
from scipy.sparse import coo_matrix

import pandas as pd
import ml_metrics as metrics
user_hist_df = pd.read_csv('../data/raw/movies_dataset_10 months.csv')

user_hist_df['ts'] = pd.to_datetime(user_hist_df['ts'])
user_hist_df['month'] = user_hist_df['ts'].dt.month

test_1 = user_hist_df[user_hist_df['month'] == 6]
test_2 = user_hist_df[user_hist_df['month'] == 7]
user_hist_df = user_hist_df[user_hist_df['episode_id'] == 0]
train = user_hist_df[~user_hist_df['month'].isin([6, 7])]
correct_1 = test_1.drop_duplicates(['user_id', 'movie_id']).groupby('user_id')['movie_id'].apply(list).to_dict()
correct_2 = test_2.drop_duplicates(['user_id', 'movie_id']).groupby('user_id')['movie_id'].apply(list).to_dict()

train['event'] = 1
train = train[['user_id', 'movie_id', 'event']].drop_duplicates()
user_encode = {u: i for i, u in enumerate(train['user_id'].unique())}
item_encode = {u: i for i, u in enumerate(train['movie_id'].unique())}

user_decode = {v: k for k, v in user_encode.items()}
item_decode = {v: k for k, v in item_encode.items()}

n_users, n_items = len(user_encode), len(item_encode)

train['user_id'] = train['user_id'].apply(lambda x: user_encode[x])
train['movie_id'] = train['movie_id'].apply(lambda x: item_encode[x])
item_user = coo_matrix(
    (train['event'], (train['user_id'], train['movie_id'])), shape=(n_users, n_items))

user_item = coo_matrix(
    (train['event'], (train['movie_id'], train['user_id'])), shape=(n_items, n_users))
user_item.shape
# initialize a model
model = implicit.als.AlternatingLeastSquares(factors=5, iterations= 15)

# train the model on a sparse matrix of item/user/confidence weights
model.fit(user_item, show_progress=False)
model = implicit.lmf.LogisticMatrixFactorization(factors = 5, learning_rate = 1, random_state = 42)

model.fit(user_item, show_progress=False)
model = implicit.bpr.BayesianPersonalizedRanking(factors = 10)

model.fit(user_item, show_progress=False)
model = implicit.nearest_neighbours.TFIDFRecommender(K=10)

model.fit(user_item, show_progress=False)
movies_database = pd.read_csv('../data/raw/movies.csv')
useless_movies = movies_database[(movies_database['year'] < 2010) & 
                                 (~movies_database['id'].isin(list(item_encode.keys()))) &
                                 (movies_database['imdb_rating'] < 6)]['id'].values

useless_movies = [m for m in useless_movies if m in item_decode]
# als
res = model.recommend_all(item_user, N = 5, filter_items = useless_movies, show_progress=False)
reco
# als
res = model.similar_items(19908)
res
recom = {}

for ind, rec in enumerate(res):
    recom[user_decode[ind]] = [item_decode[x] for x in rec]
# als
reco = []
corr = []

num_to_recom = 5
for user in correct_1:
    try:
        reco.append(recom[user])
        corr.append(correct_1[user])
    except:
        print(user)
        pass

metrics.mapk(reco, corr, 5)
# als
reco = []
corr = []

num_to_recom = 5
for user in correct_1:
    reco.append(recom[user])
    corr.append(correct_1[user])

metrics.mapk(reco, corr, 5)<jupyter_output><empty_output><jupyter_text>inference<jupyter_code>user_hist_df = pd.read_csv('../data/raw/movies_dataset_10 months.csv')
user_hist_df = user_hist_df[user_hist_df['episode_id'] == 0]

user_hist_df['event'] = 1
train = user_hist_df[['user_id', 'movie_id', 'event']].drop_duplicates()
user_encode = {u: i for i, u in enumerate(train['user_id'].unique())}
item_encode = {u: i for i, u in enumerate(train['movie_id'].unique())}

user_decode = {v: k for k, v in user_encode.items()}
item_decode = {v: k for k, v in item_encode.items()}

n_users, n_items = len(user_encode), len(item_encode)

train['user_id'] = train['user_id'].apply(lambda x: user_encode[x])
train['movie_id'] = train['movie_id'].apply(lambda x: item_encode[x])

item_user = coo_matrix(
    (train['event'], (train['user_id'], train['movie_id'])), shape=(n_users, n_items))

user_item = coo_matrix(
    (train['event'], (train['movie_id'], train['user_id'])), shape=(n_items, n_users))
# initialize a model
model = implicit.als.AlternatingLeastSquares(factors=5)

# train the model on a sparse matrix of item/user/confidence weights
model.fit(user_item, show_progress=False)
model = implicit.lmf.LogisticMatrixFactorization(factors = 5, random_state = 42)

model.fit(user_item, show_progress=False)
res = model.recommend_all(item_user, N = 10, filter_items = useless_movies, show_progress=False)
res = model.recommend_all(item_user, N = 5, show_progress=False)
recom = {}

for ind, rec in enumerate(res):
    recom[user_decode[ind]] = [item_decode[x] for x in rec]
import sys

PATH = '/Users/danil/Documents/github/sweet_RS/'
sys.path.append(str(PATH))

from src.utils import save_to_pickle
save_to_pickle(recom, '../data/processed/als_2_10.pickle')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1560776302349336, 'fraction_numerical': 0.011848825331971399, 'mean_word_length': 5.934844192634561, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '421673', 'license': 'MIT', 'n_tokens_mistral': 1895, 'n_tokens_neox': 1803, 'n_words': 344, 'path': 'notebooks/implicit.ipynb', 'repo_name': 'danilpolkov/sweet_RS', 'size': '4895'}