# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

# # ML-Based Collaborative Filtering: Performance Evaluation
# This notebook shows the evaluation of performance the SVD-based machine learning collaborative filtering. 

import pandas as pd
import numpy as np
import sys
sys.path.insert(1, '../resype')
# %load_ext autoreload
# %autoreload 2 

# ## Prepare data

# load transaction list
transaction_train = pd.read_csv("../sample_data/train_set.csv")[['userId', 'movieId', 'rating']]
transaction_train.columns = ["user_id", 'item_id', 'rating']
transaction_test = pd.read_csv("../sample_data/test_set.csv")[['userId', 'movieId', 'rating']]
transaction_test.columns = ["user_id", 'item_id', 'rating']

transaction_train.shape, transaction_test.shape

transaction_train.head()

transaction_test.head()

# ## Using Random Forest Regressor as base model

from collab_filtering import CollabFilteringModel

from sklearn.ensemble import RandomForestRegressor

# +
# %%time

df_test_fin_nans = transaction_test.copy()
df_test_fin_nans['rating'] = np.nan

re = CollabFilteringModel(pd.concat([transaction_train, df_test_fin_nans], axis=0))
utility_matrix = re.construct_utility_matrix()
print(utility_matrix.shape)
rs_model = RandomForestRegressor(random_state=202109)

outputs = re.train_model_svd(
    re.utility_matrix, rs_model, d=20, return_models=True)


# +
from sklearn.metrics import mean_absolute_error, mean_squared_error

utility_matrix_imputed, trained_model = outputs
utility_matrix_imputed = re.mean_filled_utilmat(utility_matrix_imputed)
imputed_transactions = utility_matrix_imputed.stack().reset_index()
test_data = transaction_test.copy()
merged_test_data = test_data.merge(
    imputed_transactions, 
    on=imputed_transactions.columns.tolist()[:2],
    how='left').dropna()
mse = mean_squared_error(merged_test_data.rating, merged_test_data[0].values)
mae = mean_absolute_error(merged_test_data.rating, merged_test_data[0].values)
# -

print(f'MSE:{mse}, MAE:{mae}')

# #### Increase dimensions in SVD

# +
# %%time

df_test_fin_nans = transaction_test.copy()
df_test_fin_nans['rating'] = np.nan

re = CollabFilteringModel(pd.concat([transaction_train, df_test_fin_nans], axis=0))
utility_matrix = re.construct_utility_matrix()
print(utility_matrix.shape)
rs_model = RandomForestRegressor(random_state=202109)

outputs = re.train_model_svd(
    re.utility_matrix, rs_model, d=50, return_models=True)

utility_matrix_imputed, trained_model = outputs
utility_matrix_imputed = re.mean_filled_utilmat(utility_matrix_imputed)
imputed_transactions = utility_matrix_imputed.stack().reset_index()
test_data = transaction_test.copy()
merged_test_data = test_data.merge(
    imputed_transactions, 
    on=imputed_transactions.columns.tolist()[:2],
    how='left').dropna()
mse = mean_squared_error(merged_test_data.rating, merged_test_data[0].values)
mae = mean_absolute_error(merged_test_data.rating, merged_test_data[0].values)

print(f'MSE:{mse}, MAE:{mae}')
# -

# ## Using Decision Tree Regressor as base model

from sklearn.tree import DecisionTreeRegressor

# +
# %%time

df_test_fin_nans = transaction_test.copy()
df_test_fin_nans['rating'] = np.nan

re = CollabFilteringModel(pd.concat([transaction_train, df_test_fin_nans], axis=0))
utility_matrix = re.construct_utility_matrix()
print(utility_matrix.shape)

rs_model = DecisionTreeRegressor(random_state=202109)
outputs = re.train_model_svd(
    re.utility_matrix, rs_model, d=50, return_models=True)


utility_matrix_imputed, trained_model = outputs
utility_matrix_imputed = re.mean_filled_utilmat(utility_matrix_imputed)
imputed_transactions = utility_matrix_imputed.stack().reset_index()
test_data = transaction_test.copy()
merged_test_data = test_data.merge(
    imputed_transactions, 
    on=imputed_transactions.columns.tolist()[:2],
    how='left').dropna()
mse = mean_squared_error(merged_test_data.rating, merged_test_data[0].values)
mae = mean_absolute_error(merged_test_data.rating, merged_test_data[0].values)

print(f'MSE:{mse}, MAE:{mae}')
# -



StarcoderdataJupyterScriptsDedupFiltered