<jupyter_start><jupyter_text>Access Derive<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
cd '/content/drive/MyDrive/Colab Notebooks'<jupyter_output>/content/drive/MyDrive/Colab Notebooks<jupyter_text>Read Data<jupyter_code>import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as pyplot
import os
URM_train=pd.read_csv('input/data_train.csv')
R_test=pd.read_csv('input/data_target_users_test.csv')

URM=pd.read_csv('input/data_train.csv')
URM_tuples = [tuple(x) for x in URM.to_numpy()]


URM_tuples = [tuple(x) for x in URM_train.to_numpy()]

userList, itemList, ratingList = zip(*URM_tuples)

userList = list(userList)
userList=np.array(userList,dtype=np.int64)
itemList = list(itemList)
itemList=np.array(itemList,dtype=np.int64)

ratingList = list(ratingList)                        #not needed
ratingList=np.array(ratingList,dtype=np.int64)       #not needed




len(ratingList)-np.count_nonzero(ratingList)
itemList
userList_unique = list(set(userList))
itemList_unique = list(set(itemList))

numUsers = len(userList_unique)
numItems = len(itemList_unique)

numberInteractions= len(URM_tuples)
print ("Number of items\t {}, Number of users\t {}".format(numItems, numUsers))
print("Number of Intraction \t {}" .format(numberInteractions))
print ("Max ID items\t {}, Max Id users\t {}\n".format(max(itemList_unique), max(userList_unique)))
print ("Average interactions per user {:.2f}".format(numberInteractions/numUsers))
print ("Average interactions per item {:.2f}\n".format(numberInteractions/numItems))

print ("Sparsity {:.2f} %".format((1-float(numberInteractions)/(numItems*numUsers))*100))<jupyter_output>Number of items	 24896, Number of users	 7947
Number of Intraction 	 113268
Max ID items	 25974, Max Id users	 7946

Average interactions per user 14.25
Average interactions per item 4.55

Sparsity 99.94 %<jupyter_text>To store the data we use a sparse matrix. We build it as a COO matrix and then change its formatThe COO constructor expects (data, (row, column))<jupyter_code>import scipy.sparse as sps

URM_all = sps.coo_matrix((ratingList, (userList, itemList)))

URM_all
URM_all.tocsr()<jupyter_output><empty_output><jupyter_text>** Item popularity**<jupyter_code>import numpy as np

itemPopularity = (URM_all>0).sum(axis=0)
itemPopularity
itemPopularity = np.array(itemPopularity).squeeze()
itemPopularity
itemPopularity = np.sort(itemPopularity)
itemPopularity<jupyter_output><empty_output><jupyter_text>Split data for test and train<jupyter_code>train_test_split = 0.999

numInteractions = URM_all.nnz


train_mask = np.random.choice([True,False], numInteractions, p=[train_test_split, 1-train_test_split])
train_mask
userList = np.array(userList)
itemList = np.array(itemList)
ratingList = np.array(ratingList)


URM_train = sps.coo_matrix((ratingList[train_mask], (userList[train_mask], itemList[train_mask])))   #changed variable specification
URM_train = URM_train.tocsr()
URM_train
test_mask = np.logical_not(train_mask)

URM_test = sps.coo_matrix((ratingList[test_mask], (userList[test_mask], itemList[test_mask])))
URM_test = URM_test.tocsr()
URM_test<jupyter_output><empty_output><jupyter_text>Evaluation Metrics<jupyter_code>user_id = userList_unique[1]
user_id
 from Base.Evaluation import Evaluator
#from Data_manager.Movielens.Movielens10MReader import Movielens10MReader
from Base.Evaluation.Evaluator import EvaluatorHoldout
from Data_manager.split_functions.split_train_validation_random_holdout import split_train_in_two_percentage_global_sample
#dataReader = Movielens10MReader()
#dataset = dataReader.load_data()

#URM_all = dataset.get_URM_all()

URM_train, URM_test = split_train_in_two_percentage_global_sample(URM_all, train_percentage = 0.999)
URM_train, URM_validation = split_train_in_two_percentage_global_sample(URM_train, train_percentage = 0.999)

evaluator_validation = EvaluatorHoldout(URM_validation, cutoff_list=[10])
evaluator_test = EvaluatorHoldout(URM_test, cutoff_list=[10])<jupyter_output>Warning: 7840 (98.65 %) of 7947 users have no sampled items
Warning: 7845 (98.72 %) of 7947 users have no sampled items<jupyter_text>Run on Train and Test<jupyter_code>from Base.Similarity.Compute_Similarity_Python import Compute_Similarity_Python
#from KNN import *
from KNN import ItemKNNCFRecommender as ICF
best_parameters = {'topK': 10, 'shrink': 10, 'similarity': 'cosine', 'normalize': True}

itemKNNCF = ICF.ItemKNNCFRecommender(URM_train)
itemKNNCF.fit(**best_parameters)
from GraphBased.P3alphaRecommender import P3alphaRecommender

P3alpha = P3alphaRecommender(URM_train)
P3alpha.fit()
itemKNNCF.W_sparse

P3alpha.W_sparse
from KNN import ItemKNNSimilarityHybridRecommender as Hyb
hybridrecommender = Hyb.ItemKNNSimilarityHybridRecommender(URM_train, itemKNNCF.W_sparse, P3alpha.W_sparse)
for i in range(60, 150, 10):
    hybridrecommender.fit(topK = i,  alpha = 0.36) #k = 90
    temp =  evaluator_validation.evaluateRecommender(hybridrecommender)
    print("i = " + str(i) + "   map = " + str(temp[0][10]['MAP']))<jupyter_output>EvaluatorHoldout: Processed 102 ( 100.00% ) in 0.07 sec. Users per second: 1526
i = 60   map = 0.039390756302521014
EvaluatorHoldout: Processed 102 ( 100.00% ) in 0.07 sec. Users per second: 1519
i = 70   map = 0.039390756302521014
EvaluatorHoldout: Processed 102 ( 100.00% ) in 0.07 sec. Users per second: 1458
i = 80   map = 0.039390756302521014
EvaluatorHoldout: Processed 102 ( 100.00% ) in 0.07 sec. Users per second: 1501
i = 90   map = 0.034488795518207285
EvaluatorHoldout: Processed 102 ( 100.00% ) in 0.07 sec. Users per second: 1459
i = 100   map = 0.04024665421724245
EvaluatorHoldout: Processed 102 ( 100.00% ) in 0.07 sec. Users per second: 1428
i = 110   map = 0.040301120448179274
EvaluatorHoldout: Processed 102 ( 100.00% ) in 0.07 sec. Users per second: 1494
i = 120   map = 0.040301120448179274
EvaluatorHoldout: Processed 102 ( 100.00% ) in 0.07 sec. Users per second: 1458
i = 130   map = 0.040301120448179274
EvaluatorHoldout: Processed 102 ( 100.00% ) in 0.07 sec. Users per se[...]<jupyter_text>Output<jupyter_code>hybridrecommender.fit(topK = 105,  alpha = 0.36) #k = 105

for user_id in range():
    print(topPopRecommender.recommend(user_id, at=10))
#R_test=pd.read_csv('input/data_target_users_test.csv')
n = len(R_test)

azina = []
for i in range(n):
  usr_id=R_test['user_id'][i]
  str_temp = str(hybridrecommender.recommend(usr_id,10))[1:-1]
  str_temp = str_temp.replace(',','')
  R_test['item_list'][i]=str_temp

R_test=R_test.set_index('user_id')
R_test
R_test.to_csv('result_itemLinearHybrid.csv')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11131032421933364, 'fraction_numerical': 0.06588973554459883, 'mean_word_length': 6.268186753528773, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 30, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '26847', 'license': 'MIT', 'n_tokens_mistral': 2840, 'n_tokens_neox': 2463, 'n_words': 518, 'path': 'my_linear_hybrid_itemBased.ipynb', 'repo_name': 'SamanFekri/BookRecommendation', 'size': '6693'}