<jupyter_start><jupyter_text>Loading data and necessary libraries<jupyter_code>import pandas as pd
import numpy as np
from sklearn.decomposition import NMF
# Read data
data = pd.read_csv("merged_file.csv", index_col = 0)
# Explore shape
data.shape
# Explore first rows
data.head()
# Unique reviewerIDs (%)
len(data["reviewerID"].unique())/len(data)*100<jupyter_output><empty_output><jupyter_text>Constructing the Matrix<jupyter_code># Pivot table to create the matrix
ratings_matrix = data.pivot_table(index = 'reviewerID', columns = 'productID', values = 'overall', aggfunc = "mean", fill_value = 0)
# Explore first rows
ratings_matrix.head()
# Explore shape of user-item matrix
ratings_matrix.shape
# Unique users
print(len(data["reviewerID"].unique()))
# Unique products
print(len(data["productID"].unique()))
# Check for an example
data.groupby(["reviewerID", "productID"]).count()["overall"]
# Let's look at the matrix for user A0059486XI1Z0P98KP35, product B000WE8JES: ORIGINAL DATA
data.loc[(data["reviewerID"] == "A0059486XI1Z0P98KP35") & (data["productID"] == "B000WE8JES")]
# Let's look at the matrix for user A0059486XI1Z0P98KP35, product B000WE8JES
ratings_matrix.loc["A0059486XI1Z0P98KP35", "B000WE8JES"]
# Check unique combinations of user and product<jupyter_output><empty_output><jupyter_text>Implementing Non-negative Matrix Factorization<jupyter_code># Convert matrix to array
R = np.array(ratings_matrix)
# Instantiate NMF with 4 components
nmf = NMF(n_components = 4, max_iter = 2000, random_state = 232323, verbose = 5)
# Find matrix with features
W = nmf.fit_transform(R)
# Explore shape of Feature Matrix (rows = users, columns = features)
W.shape
# Explore first rows of Feature Matrix
W[:5]
# Construct Coefficient Matrix ( V = W * H)
H = nmf.components_
# Explore shape of Coefficient Matrix (rows = feaures, columns = products)
H.shape
# Reconstruction error
print(nmf.reconstruction_err_)<jupyter_output>2998.448380876802<jupyter_text>Recommending based on NMF<jupyter_code># Reconstruct the matrix by computing dot product between Feature Matrix (W) and Coefficient Matrix (H)
predictions = np.dot(W, H)
# Convert to df
predictions_df = pd.DataFrame(predictions, columns = ratings_matrix.columns)
predictions_df.index = ratings_matrix.index
# Explore resulting df
print(predictions_df.shape)
predictions_df.head()<jupyter_output>(55223, 17408)
{'fraction_non_alphanumeric': 0.1087689713322091, 'fraction_numerical': 0.047639123102866776, 'mean_word_length': 7.04406779661017, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '80159', 'license': 'MIT', 'n_tokens_mistral': 879, 'n_tokens_neox': 789, 'n_words': 211, 'path': 'Analysis/NMF/NMF_first_attempt_LR.ipynb', 'repo_name': 'LUCIA-RONCHI/Amazon_Gaming_Recommender_System', 'size': '2372'}