workshops_recommender_systems/P5. Graph-based.ipynb

1392 lines
554 KiB
Plaintext
Raw Normal View History

2020-05-21 13:42:50 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Self made RP3-beta"
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"import helpers\n",
"import pandas as pd\n",
"import numpy as np\n",
"import scipy.sparse as sparse\n",
"from collections import defaultdict\n",
"from itertools import chain\n",
"import random\n",
"import time\n",
"import matplotlib.pyplot as plt\n",
"\n",
"train_read=pd.read_csv('./Datasets/ml-100k/train.csv', sep='\\t', header=None)\n",
"test_read=pd.read_csv('./Datasets/ml-100k/test.csv', sep='\\t', header=None)\n",
"train_ui, test_ui, user_code_id, user_id_code, item_code_id, item_id_code = helpers.data_to_csr(train_read, test_read)"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"class RP3Beta():\n",
" def fit(self, train_ui, alpha, beta):\n",
" \"\"\"We weight our edges by user's explicit ratings so if user rated movie high we'll follow that path\n",
" with higher probability.\"\"\"\n",
" self.train_ui=train_ui\n",
" self.train_iu=train_ui.transpose()\n",
" \n",
" self.alpha = alpha\n",
" self.beta = beta\n",
" \n",
" # Define Pui \n",
" Pui=sparse.csr_matrix(self.train_ui/self.train_ui.sum(axis=1))\n",
" \n",
" # Define Piu\n",
" to_divide=np.vectorize(lambda x: x if x>0 else 1)(self.train_iu.sum(axis=1)) # to avoid dividing by zero\n",
" Piu=sparse.csr_matrix(self.train_iu/to_divide)\n",
" item_orders=(self.train_ui>0).sum(axis=0)\n",
" \n",
" Pui = Pui.power(self.alpha)\n",
" Piu = Piu.power(self.alpha)\n",
"\n",
" P3=Pui*Piu*Pui\n",
" \n",
" P3/=np.power(np.vectorize(lambda x: x if x>0 else 1)(item_orders), self.beta)\n",
" \n",
" self.estimations=np.array(P3)\n",
" \n",
" def recommend(self, user_code_id, item_code_id, topK=10):\n",
" \n",
" top_k = defaultdict(list)\n",
" for nb_user, user in enumerate(self.estimations):\n",
" \n",
" user_rated=self.train_ui.indices[self.train_ui.indptr[nb_user]:self.train_ui.indptr[nb_user+1]]\n",
" for item, score in enumerate(user):\n",
" if item not in user_rated and not np.isnan(score):\n",
" top_k[user_code_id[nb_user]].append((item_code_id[item], score))\n",
" result=[]\n",
" # Let's choose k best items in the format: (user, item1, score1, item2, score2, ...)\n",
" for uid, item_scores in top_k.items():\n",
" item_scores.sort(key=lambda x: x[1], reverse=True)\n",
" result.append([uid]+list(chain(*item_scores[:topK])))\n",
" return result\n",
" \n",
" def estimate(self, user_code_id, item_code_id, test_ui):\n",
" result=[]\n",
" for user, item in zip(*test_ui.nonzero()):\n",
" result.append([user_code_id[user], item_code_id[item], \n",
" self.estimations[user,item] if not np.isnan(self.estimations[user,item]) else 1])\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [],
"source": [
"model=RP3Beta()\n",
"model.fit(train_ui, alpha=1, beta=0)"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"top_n=pd.DataFrame(model.recommend(user_code_id, item_code_id, topK=10))\n",
"\n",
"top_n.to_csv('Recommendations generated/ml-100k/Self_P3_reco.csv', index=False, header=False)\n",
"\n",
"estimations=pd.DataFrame(model.estimate(user_code_id, item_code_id, test_ui))\n",
"estimations.to_csv('Recommendations generated/ml-100k/Self_P3_estimations.csv', index=False, header=False)"
]
},
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"943it [00:00, 7582.24it/s]\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>RMSE</th>\n",
" <th>MAE</th>\n",
" <th>precision</th>\n",
" <th>recall</th>\n",
" <th>F_1</th>\n",
" <th>F_05</th>\n",
" <th>precision_super</th>\n",
" <th>recall_super</th>\n",
" <th>NDCG</th>\n",
" <th>mAP</th>\n",
" <th>MRR</th>\n",
" <th>LAUC</th>\n",
" <th>HR</th>\n",
" <th>Reco in test</th>\n",
" <th>Test coverage</th>\n",
" <th>Shannon</th>\n",
" <th>Gini</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>3.702446</td>\n",
" <td>3.527273</td>\n",
" <td>0.282185</td>\n",
" <td>0.192092</td>\n",
" <td>0.186749</td>\n",
" <td>0.21698</td>\n",
" <td>0.204185</td>\n",
" <td>0.240096</td>\n",
" <td>0.339114</td>\n",
" <td>0.204905</td>\n",
" <td>0.572157</td>\n",
" <td>0.593544</td>\n",
" <td>0.875928</td>\n",
" <td>1.0</td>\n",
" <td>0.077201</td>\n",
" <td>3.875892</td>\n",
" <td>0.974947</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" RMSE MAE precision recall F_1 F_05 \\\n",
"0 3.702446 3.527273 0.282185 0.192092 0.186749 0.21698 \n",
"\n",
" precision_super recall_super NDCG mAP MRR LAUC \\\n",
"0 0.204185 0.240096 0.339114 0.204905 0.572157 0.593544 \n",
"\n",
" HR Reco in test Test coverage Shannon Gini \n",
"0 0.875928 1.0 0.077201 3.875892 0.974947 "
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import evaluation_measures as ev\n",
"estimations_df=pd.read_csv('Recommendations generated/ml-100k/Self_P3_estimations.csv', header=None)\n",
"reco=np.loadtxt('Recommendations generated/ml-100k/Self_P3_reco.csv', delimiter=',')\n",
"\n",
"ev.evaluate(test=pd.read_csv('./Datasets/ml-100k/test.csv', sep='\\t', header=None),\n",
" estimations_df=estimations_df, \n",
" reco=reco,\n",
" super_reactions=[4,5])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Let's check hiperparameters"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Alpha"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 0/8 [00:00<?, ?it/s]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 7004.99it/s]\u001b[A\n",
" 12%|█▎ | 1/8 [00:10<01:12, 10.38s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 7038.01it/s]\u001b[A\n",
" 25%|██▌ | 2/8 [00:20<01:01, 10.33s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 7039.70it/s]\u001b[A\n",
" 38%|███▊ | 3/8 [00:30<00:51, 10.26s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 6956.11it/s]\u001b[A\n",
" 50%|█████ | 4/8 [00:41<00:41, 10.29s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 6729.43it/s]\u001b[A\n",
" 62%|██████▎ | 5/8 [00:51<00:30, 10.25s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 7159.95it/s]\u001b[A\n",
" 75%|███████▌ | 6/8 [01:01<00:20, 10.21s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 7053.74it/s]\u001b[A\n",
" 88%|████████▊ | 7/8 [01:11<00:10, 10.18s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 6796.15it/s]\u001b[A\n",
"100%|██████████| 8/8 [01:21<00:00, 10.18s/it]\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Alpha</th>\n",
" <th>RMSE</th>\n",
" <th>MAE</th>\n",
" <th>precision</th>\n",
" <th>recall</th>\n",
" <th>F_1</th>\n",
" <th>F_05</th>\n",
" <th>precision_super</th>\n",
" <th>recall_super</th>\n",
" <th>NDCG</th>\n",
" <th>mAP</th>\n",
" <th>MRR</th>\n",
" <th>LAUC</th>\n",
" <th>HR</th>\n",
" <th>Reco in test</th>\n",
" <th>Test coverage</th>\n",
" <th>Shannon</th>\n",
" <th>Gini</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.2</td>\n",
" <td>268.177832</td>\n",
" <td>211.732649</td>\n",
" <td>0.262672</td>\n",
" <td>0.166858</td>\n",
" <td>0.166277</td>\n",
" <td>0.197184</td>\n",
" <td>0.187661</td>\n",
" <td>0.203252</td>\n",
" <td>0.320910</td>\n",
" <td>0.196132</td>\n",
" <td>0.563378</td>\n",
" <td>0.580866</td>\n",
" <td>0.850477</td>\n",
" <td>1.000000</td>\n",
" <td>0.060606</td>\n",
" <td>3.669627</td>\n",
" <td>0.979636</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.4</td>\n",
" <td>10.546689</td>\n",
" <td>7.792373</td>\n",
" <td>0.268505</td>\n",
" <td>0.172669</td>\n",
" <td>0.171569</td>\n",
" <td>0.202643</td>\n",
" <td>0.192489</td>\n",
" <td>0.212653</td>\n",
" <td>0.326760</td>\n",
" <td>0.200172</td>\n",
" <td>0.565148</td>\n",
" <td>0.583801</td>\n",
" <td>0.854719</td>\n",
" <td>1.000000</td>\n",
" <td>0.064214</td>\n",
" <td>3.726996</td>\n",
" <td>0.978426</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.6</td>\n",
" <td>3.143988</td>\n",
" <td>2.948790</td>\n",
" <td>0.274655</td>\n",
" <td>0.180502</td>\n",
" <td>0.177820</td>\n",
" <td>0.208730</td>\n",
" <td>0.198176</td>\n",
" <td>0.222746</td>\n",
" <td>0.332872</td>\n",
" <td>0.203290</td>\n",
" <td>0.568872</td>\n",
" <td>0.587738</td>\n",
" <td>0.870626</td>\n",
" <td>1.000000</td>\n",
" <td>0.065657</td>\n",
" <td>3.785282</td>\n",
" <td>0.977090</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.8</td>\n",
" <td>3.670728</td>\n",
" <td>3.495735</td>\n",
" <td>0.281972</td>\n",
" <td>0.189868</td>\n",
" <td>0.185300</td>\n",
" <td>0.216071</td>\n",
" <td>0.203541</td>\n",
" <td>0.236751</td>\n",
" <td>0.339867</td>\n",
" <td>0.206688</td>\n",
" <td>0.573729</td>\n",
" <td>0.592432</td>\n",
" <td>0.874867</td>\n",
" <td>1.000000</td>\n",
" <td>0.070707</td>\n",
" <td>3.832415</td>\n",
" <td>0.975998</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.0</td>\n",
" <td>3.702446</td>\n",
" <td>3.527273</td>\n",
" <td>0.282185</td>\n",
" <td>0.192092</td>\n",
" <td>0.186749</td>\n",
" <td>0.216980</td>\n",
" <td>0.204185</td>\n",
" <td>0.240096</td>\n",
" <td>0.339114</td>\n",
" <td>0.204905</td>\n",
" <td>0.572157</td>\n",
" <td>0.593544</td>\n",
" <td>0.875928</td>\n",
" <td>1.000000</td>\n",
" <td>0.077201</td>\n",
" <td>3.875892</td>\n",
" <td>0.974947</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.2</td>\n",
" <td>3.704441</td>\n",
" <td>3.529251</td>\n",
" <td>0.280912</td>\n",
" <td>0.193633</td>\n",
" <td>0.187311</td>\n",
" <td>0.216872</td>\n",
" <td>0.203004</td>\n",
" <td>0.240588</td>\n",
" <td>0.338049</td>\n",
" <td>0.203453</td>\n",
" <td>0.571830</td>\n",
" <td>0.594313</td>\n",
" <td>0.883351</td>\n",
" <td>1.000000</td>\n",
" <td>0.085859</td>\n",
" <td>3.910718</td>\n",
" <td>0.974073</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.4</td>\n",
" <td>3.704580</td>\n",
" <td>3.529388</td>\n",
" <td>0.273595</td>\n",
" <td>0.190651</td>\n",
" <td>0.183874</td>\n",
" <td>0.212183</td>\n",
" <td>0.199464</td>\n",
" <td>0.239118</td>\n",
" <td>0.329550</td>\n",
" <td>0.195433</td>\n",
" <td>0.566171</td>\n",
" <td>0.592793</td>\n",
" <td>0.871686</td>\n",
" <td>1.000000</td>\n",
" <td>0.107504</td>\n",
" <td>3.961915</td>\n",
" <td>0.972674</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1.6</td>\n",
" <td>3.704591</td>\n",
" <td>3.529399</td>\n",
" <td>0.263097</td>\n",
" <td>0.186255</td>\n",
" <td>0.178709</td>\n",
" <td>0.205170</td>\n",
" <td>0.191094</td>\n",
" <td>0.232920</td>\n",
" <td>0.317439</td>\n",
" <td>0.184917</td>\n",
" <td>0.552349</td>\n",
" <td>0.590545</td>\n",
" <td>0.868505</td>\n",
" <td>0.999576</td>\n",
" <td>0.156566</td>\n",
" <td>4.060156</td>\n",
" <td>0.969203</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Alpha RMSE MAE precision recall F_1 F_05 \\\n",
"0 0.2 268.177832 211.732649 0.262672 0.166858 0.166277 0.197184 \n",
"0 0.4 10.546689 7.792373 0.268505 0.172669 0.171569 0.202643 \n",
"0 0.6 3.143988 2.948790 0.274655 0.180502 0.177820 0.208730 \n",
"0 0.8 3.670728 3.495735 0.281972 0.189868 0.185300 0.216071 \n",
"0 1.0 3.702446 3.527273 0.282185 0.192092 0.186749 0.216980 \n",
"0 1.2 3.704441 3.529251 0.280912 0.193633 0.187311 0.216872 \n",
"0 1.4 3.704580 3.529388 0.273595 0.190651 0.183874 0.212183 \n",
"0 1.6 3.704591 3.529399 0.263097 0.186255 0.178709 0.205170 \n",
"\n",
" precision_super recall_super NDCG mAP MRR LAUC \\\n",
"0 0.187661 0.203252 0.320910 0.196132 0.563378 0.580866 \n",
"0 0.192489 0.212653 0.326760 0.200172 0.565148 0.583801 \n",
"0 0.198176 0.222746 0.332872 0.203290 0.568872 0.587738 \n",
"0 0.203541 0.236751 0.339867 0.206688 0.573729 0.592432 \n",
"0 0.204185 0.240096 0.339114 0.204905 0.572157 0.593544 \n",
"0 0.203004 0.240588 0.338049 0.203453 0.571830 0.594313 \n",
"0 0.199464 0.239118 0.329550 0.195433 0.566171 0.592793 \n",
"0 0.191094 0.232920 0.317439 0.184917 0.552349 0.590545 \n",
"\n",
" HR Reco in test Test coverage Shannon Gini \n",
"0 0.850477 1.000000 0.060606 3.669627 0.979636 \n",
"0 0.854719 1.000000 0.064214 3.726996 0.978426 \n",
"0 0.870626 1.000000 0.065657 3.785282 0.977090 \n",
"0 0.874867 1.000000 0.070707 3.832415 0.975998 \n",
"0 0.875928 1.000000 0.077201 3.875892 0.974947 \n",
"0 0.883351 1.000000 0.085859 3.910718 0.974073 \n",
"0 0.871686 1.000000 0.107504 3.961915 0.972674 \n",
"0 0.868505 0.999576 0.156566 4.060156 0.969203 "
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from tqdm import tqdm\n",
"result=[]\n",
"for alpha in tqdm([round(i,1) for i in np.arange(0.2,1.6001,0.2)]):\n",
" model=RP3Beta()\n",
" model.fit(train_ui, alpha=alpha, beta=0)\n",
" reco=pd.DataFrame(model.recommend(user_code_id, item_code_id, topK=10))\n",
" estimations_df=pd.DataFrame(model.estimate(user_code_id, item_code_id, test_ui))\n",
" to_append=ev.evaluate(test=pd.read_csv('./Datasets/ml-100k/test.csv', sep='\\t', header=None),\n",
" estimations_df=estimations_df, \n",
" reco=np.array(reco),\n",
" super_reactions=[4,5])\n",
" to_append.insert(0, \"Alpha\", alpha)\n",
" result.append(to_append)\n",
" \n",
"result=pd.concat(result)\n",
"result"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABCwAAAkoCAYAAACgVC5GAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAgAElEQVR4nOzde3xV9ZX//9fKPYdbOCFAQhJAReUaUEAsUhXRiiVoW2t1atupnXGm1XY67fy+Y6f9tR2nPmaqvY0t7bSddmynFm/TC7F4L7b6HRFRCVdRlHsChHBLIBeSrO8fZyceYpBbztknOe/n43EenPPZe5+sjbrdWXt91sfcHRERERERERGRVJIRdgAiIiIiIiIiIt0pYSEiIiIiIiIiKUcJCxERERERERFJOUpYiIiIiIiIiEjKUcJCRERERERERFKOEhYiIiIiIiIiknKUsBAREREREZEuZvYfZvb/n8R+68zssiSEJGlKCQvpd8xsi5k1mVmjme0ys/vMbGCw7T4zczO7ttsx3w3G/zL4nGNm3zazHcH3bDGz7x3nZ3S+fpDUExUR6UOC62armQ3rNv5qcP0dEzf29WDsom77/qWZtXe79jaaWUlyzkJEJD24+9+6+7+cxH4T3f3ZJIQkaUoJC+mvKt19IDAVmAZ8KW7b68DHOz+YWRZwA/Bm3D5fAqYDM4FBwGXAKz39jLjX7b1+FiIi/ctm4KbOD2Y2GYjE72BmRuwavY+4a3WcF7pdewe6e00igxYR6YuCe1yRPk0JC+nX3H0X8ASxxEWnKuASMxsafL4aWA3sittnBvBbd6/xmC3u/sukBC0i0n/9N8cmIT4BdL+2zgGKgc8BN5pZTpJiExHpE4KKtS+Z2Xoz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JYtWzAzvvKVr3Ttu3fvXrKzs7n99tsB2LhxI5dddhlTp05l/Pjx3HrrrQA8++yzDBkyhKlTp3a9nn766eSfnIhIH2Zm3HzzzV2f29raKCoqYsGCBcfsd9111zFr1qxjxr7+9a8zatSoY67Dndf3JBoFbI/7vCMYO55PAY8BuPsLwDKgNng94e4b4nc2swKgEngmbvhDZrbazB4xs7IzPwURSUcrV67kc5/73HG319TUcP311ycxopOnhIWI9Cv5+fmsWrWKtWvXEo1GWbRoUde2sWPH8oc//KHr88MPP8zEiRO7Pn/uc5/j7//+71m1ahUbNmzgs5/9bNe2OXPmsGrVqq7XvHnzknNCIiL9xIABA1i7di1NTU0APPXUU4wadezv+wcOHODll1/m4MGDvPXWW8ds67w+d74KCgqSFvupMrObgenEelBgZucA44lVT4wC5prZnLj9s4DFwL3u3nniVcCYYHrJU8AvjvOzbjWzlWa2sq6uLlGnJCIppL29/ZT2nz59Ovfee+9xt5eUlPDII4+caVgJoYSFiPRbF198MTt37uz6HIlEGD9+PCtXrgTgwQcf5IYbbujaXltbS2np25W4kydPTl6wIiJp4JprrulKHC9evJibbrrpmO2/+c1vqKys5MYbb+SBBx4II8R3sxOIr3Io5e2GmF3MbB7wZWChu7cEwx8Alrt7o7s3Equ8uDjusJ8Ab7j79zoH3L0+7vj/BC7sKSh3/4m7T3f36UVFRad5aiKSKrZs2cL555/PRz/6UcaPH8/111/PkSNHGDNmDP/4j//IBRdcwMMPP8yTTz7JxRdfzAUXXMCHP/xhGhsbAXjppZd4z3veQ0VFBTNnzqShoYFnn322q5rtT3/6U1el2rRp02hoaGDLli1MmjQJiPXx+OQnP8nkyZOZNm0ay5YtA+C+++7jgx/8IFdffTXjxo3j//yf/5OUvw+tEiIiCfHPVetYX3OoV79zQslgvlY58cQ7Ess8P/PMM3zqU586ZrzzJnjEiBF
"text/plain": [
"<Figure size 1296x3024 with 18 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"metrics=list(result.columns[[i not in ['Alpha'] for i in result.columns]])\n",
"\n",
"charts_per_row=6\n",
"charts_per_column=3\n",
"\n",
"fig, axes = plt.subplots(nrows=charts_per_row, ncols=charts_per_column,figsize=(18, 7*charts_per_row ))\n",
"import itertools\n",
"to_iter=[i for i in itertools.product(range(charts_per_row), range(charts_per_column))]\n",
"\n",
"for i in range(len(metrics)):\n",
" df=result[['Alpha', metrics[i]]]\n",
" df.plot(ax=axes[to_iter[i]], title=metrics[i], x=0, y=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Beta"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 0/10 [00:00<?, ?it/s]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"458it [00:00, 4578.48it/s]\u001b[A\n",
"943it [00:00, 4542.31it/s]\u001b[A\n",
" 10%|█ | 1/10 [00:19<02:58, 19.88s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"463it [00:00, 4623.79it/s]\u001b[A\n",
"943it [00:00, 4327.22it/s]\u001b[A\n",
" 20%|██ | 2/10 [00:38<02:36, 19.53s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"450it [00:00, 4493.26it/s]\u001b[A\n",
"943it [00:00, 4423.59it/s]\u001b[A\n",
" 30%|███ | 3/10 [00:57<02:15, 19.30s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 4799.99it/s]\u001b[A\n",
" 40%|████ | 4/10 [01:16<01:55, 19.19s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"213it [00:00, 2129.95it/s]\u001b[A\n",
"943it [00:00, 3465.43it/s]\u001b[A\n",
" 50%|█████ | 5/10 [01:34<01:34, 19.00s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"393it [00:00, 3924.08it/s]\u001b[A\n",
"943it [00:00, 4149.53it/s]\u001b[A\n",
" 60%|██████ | 6/10 [01:53<01:15, 18.96s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"459it [00:00, 4583.01it/s]\u001b[A\n",
"943it [00:00, 4424.21it/s]\u001b[A\n",
" 70%|███████ | 7/10 [02:12<00:56, 18.76s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 4770.55it/s]\u001b[A\n",
" 80%|████████ | 8/10 [02:30<00:37, 18.69s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 5007.72it/s]\u001b[A\n",
" 90%|█████████ | 9/10 [02:49<00:18, 18.69s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 5210.56it/s]\u001b[A\n",
"100%|██████████| 10/10 [03:07<00:00, 18.79s/it]\n"
]
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>Beta</th>\n",
" <th>RMSE</th>\n",
" <th>MAE</th>\n",
" <th>precision</th>\n",
" <th>recall</th>\n",
" <th>F_1</th>\n",
" <th>F_05</th>\n",
" <th>precision_super</th>\n",
" <th>recall_super</th>\n",
" <th>NDCG</th>\n",
" <th>mAP</th>\n",
" <th>MRR</th>\n",
" <th>LAUC</th>\n",
" <th>HR</th>\n",
" <th>Reco in test</th>\n",
" <th>Test coverage</th>\n",
" <th>Shannon</th>\n",
" <th>Gini</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.0</td>\n",
" <td>3.702446</td>\n",
" <td>3.527273</td>\n",
" <td>0.282185</td>\n",
" <td>0.192092</td>\n",
" <td>0.186749</td>\n",
" <td>0.216980</td>\n",
" <td>0.204185</td>\n",
" <td>0.240096</td>\n",
" <td>0.339114</td>\n",
" <td>0.204905</td>\n",
" <td>0.572157</td>\n",
" <td>0.593544</td>\n",
" <td>0.875928</td>\n",
" <td>1.000000</td>\n",
" <td>0.077201</td>\n",
" <td>3.875892</td>\n",
" <td>0.974947</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.1</td>\n",
" <td>3.703312</td>\n",
" <td>3.528128</td>\n",
" <td>0.290138</td>\n",
" <td>0.197597</td>\n",
" <td>0.192259</td>\n",
" <td>0.223336</td>\n",
" <td>0.210944</td>\n",
" <td>0.246153</td>\n",
" <td>0.347768</td>\n",
" <td>0.212034</td>\n",
" <td>0.581038</td>\n",
" <td>0.596328</td>\n",
" <td>0.884411</td>\n",
" <td>1.000000</td>\n",
" <td>0.085137</td>\n",
" <td>3.957416</td>\n",
" <td>0.972784</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.2</td>\n",
" <td>3.703825</td>\n",
" <td>3.528636</td>\n",
" <td>0.297137</td>\n",
" <td>0.201202</td>\n",
" <td>0.196067</td>\n",
" <td>0.228169</td>\n",
" <td>0.218026</td>\n",
" <td>0.252767</td>\n",
" <td>0.355655</td>\n",
" <td>0.219909</td>\n",
" <td>0.588904</td>\n",
" <td>0.598160</td>\n",
" <td>0.886532</td>\n",
" <td>1.000000</td>\n",
" <td>0.094517</td>\n",
" <td>4.053212</td>\n",
" <td>0.969980</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.3</td>\n",
" <td>3.704130</td>\n",
" <td>3.528939</td>\n",
" <td>0.303499</td>\n",
" <td>0.204749</td>\n",
" <td>0.199901</td>\n",
" <td>0.232829</td>\n",
" <td>0.225107</td>\n",
" <td>0.260797</td>\n",
" <td>0.363757</td>\n",
" <td>0.226825</td>\n",
" <td>0.599969</td>\n",
" <td>0.599964</td>\n",
" <td>0.888653</td>\n",
" <td>1.000000</td>\n",
" <td>0.105339</td>\n",
" <td>4.147779</td>\n",
" <td>0.966948</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.4</td>\n",
" <td>3.704313</td>\n",
" <td>3.529120</td>\n",
" <td>0.308908</td>\n",
" <td>0.208811</td>\n",
" <td>0.203854</td>\n",
" <td>0.237241</td>\n",
" <td>0.229614</td>\n",
" <td>0.266918</td>\n",
" <td>0.370758</td>\n",
" <td>0.232673</td>\n",
" <td>0.609385</td>\n",
" <td>0.602014</td>\n",
" <td>0.895016</td>\n",
" <td>0.999894</td>\n",
" <td>0.132035</td>\n",
" <td>4.259682</td>\n",
" <td>0.962989</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.5</td>\n",
" <td>3.704422</td>\n",
" <td>3.529229</td>\n",
" <td>0.314316</td>\n",
" <td>0.211411</td>\n",
" <td>0.206768</td>\n",
" <td>0.240986</td>\n",
" <td>0.237124</td>\n",
" <td>0.273416</td>\n",
" <td>0.378307</td>\n",
" <td>0.239297</td>\n",
" <td>0.622792</td>\n",
" <td>0.603327</td>\n",
" <td>0.903499</td>\n",
" <td>0.999046</td>\n",
" <td>0.168831</td>\n",
" <td>4.411281</td>\n",
" <td>0.956648</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.6</td>\n",
" <td>3.704488</td>\n",
" <td>3.529295</td>\n",
" <td>0.314634</td>\n",
" <td>0.206209</td>\n",
" <td>0.204818</td>\n",
" <td>0.240159</td>\n",
" <td>0.242489</td>\n",
" <td>0.273850</td>\n",
" <td>0.376438</td>\n",
" <td>0.238428</td>\n",
" <td>0.622042</td>\n",
" <td>0.600721</td>\n",
" <td>0.897137</td>\n",
" <td>0.996394</td>\n",
" <td>0.212843</td>\n",
" <td>4.621938</td>\n",
" <td>0.945932</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.7</td>\n",
" <td>3.704528</td>\n",
" <td>3.529335</td>\n",
" <td>0.304136</td>\n",
" <td>0.187298</td>\n",
" <td>0.191990</td>\n",
" <td>0.228749</td>\n",
" <td>0.238305</td>\n",
" <td>0.256201</td>\n",
" <td>0.358807</td>\n",
" <td>0.226808</td>\n",
" <td>0.593897</td>\n",
" <td>0.591207</td>\n",
" <td>0.868505</td>\n",
" <td>0.983033</td>\n",
" <td>0.256854</td>\n",
" <td>4.898568</td>\n",
" <td>0.928065</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.8</td>\n",
" <td>3.704552</td>\n",
" <td>3.529360</td>\n",
" <td>0.266384</td>\n",
" <td>0.147571</td>\n",
" <td>0.158660</td>\n",
" <td>0.194838</td>\n",
" <td>0.214485</td>\n",
" <td>0.209336</td>\n",
" <td>0.299850</td>\n",
" <td>0.184356</td>\n",
" <td>0.492852</td>\n",
" <td>0.571152</td>\n",
" <td>0.803818</td>\n",
" <td>0.936373</td>\n",
" <td>0.341270</td>\n",
" <td>5.257397</td>\n",
" <td>0.895882</td>\n",
" </tr>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0.9</td>\n",
" <td>3.704567</td>\n",
" <td>3.529375</td>\n",
" <td>0.162354</td>\n",
" <td>0.076967</td>\n",
" <td>0.089233</td>\n",
" <td>0.114583</td>\n",
" <td>0.134657</td>\n",
" <td>0.113253</td>\n",
" <td>0.160868</td>\n",
" <td>0.085486</td>\n",
" <td>0.243590</td>\n",
" <td>0.535405</td>\n",
" <td>0.580064</td>\n",
" <td>0.800106</td>\n",
" <td>0.415584</td>\n",
" <td>5.563910</td>\n",
" <td>0.857396</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" Beta RMSE MAE precision recall F_1 F_05 \\\n",
"0 0.0 3.702446 3.527273 0.282185 0.192092 0.186749 0.216980 \n",
"0 0.1 3.703312 3.528128 0.290138 0.197597 0.192259 0.223336 \n",
"0 0.2 3.703825 3.528636 0.297137 0.201202 0.196067 0.228169 \n",
"0 0.3 3.704130 3.528939 0.303499 0.204749 0.199901 0.232829 \n",
"0 0.4 3.704313 3.529120 0.308908 0.208811 0.203854 0.237241 \n",
"0 0.5 3.704422 3.529229 0.314316 0.211411 0.206768 0.240986 \n",
"0 0.6 3.704488 3.529295 0.314634 0.206209 0.204818 0.240159 \n",
"0 0.7 3.704528 3.529335 0.304136 0.187298 0.191990 0.228749 \n",
"0 0.8 3.704552 3.529360 0.266384 0.147571 0.158660 0.194838 \n",
"0 0.9 3.704567 3.529375 0.162354 0.076967 0.089233 0.114583 \n",
"\n",
" precision_super recall_super NDCG mAP MRR LAUC \\\n",
"0 0.204185 0.240096 0.339114 0.204905 0.572157 0.593544 \n",
"0 0.210944 0.246153 0.347768 0.212034 0.581038 0.596328 \n",
"0 0.218026 0.252767 0.355655 0.219909 0.588904 0.598160 \n",
"0 0.225107 0.260797 0.363757 0.226825 0.599969 0.599964 \n",
"0 0.229614 0.266918 0.370758 0.232673 0.609385 0.602014 \n",
"0 0.237124 0.273416 0.378307 0.239297 0.622792 0.603327 \n",
"0 0.242489 0.273850 0.376438 0.238428 0.622042 0.600721 \n",
"0 0.238305 0.256201 0.358807 0.226808 0.593897 0.591207 \n",
"0 0.214485 0.209336 0.299850 0.184356 0.492852 0.571152 \n",
"0 0.134657 0.113253 0.160868 0.085486 0.243590 0.535405 \n",
"\n",
" HR Reco in test Test coverage Shannon Gini \n",
"0 0.875928 1.000000 0.077201 3.875892 0.974947 \n",
"0 0.884411 1.000000 0.085137 3.957416 0.972784 \n",
"0 0.886532 1.000000 0.094517 4.053212 0.969980 \n",
"0 0.888653 1.000000 0.105339 4.147779 0.966948 \n",
"0 0.895016 0.999894 0.132035 4.259682 0.962989 \n",
"0 0.903499 0.999046 0.168831 4.411281 0.956648 \n",
"0 0.897137 0.996394 0.212843 4.621938 0.945932 \n",
"0 0.868505 0.983033 0.256854 4.898568 0.928065 \n",
"0 0.803818 0.936373 0.341270 5.257397 0.895882 \n",
"0 0.580064 0.800106 0.415584 5.563910 0.857396 "
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from tqdm import tqdm\n",
"result=[]\n",
"for beta in tqdm([round(i,1) for i in np.arange(0,1,0.1)]):\n",
" model=RP3Beta()\n",
" model.fit(train_ui, alpha=1, beta=beta)\n",
" reco=pd.DataFrame(model.recommend(user_code_id, item_code_id, topK=10))\n",
" estimations_df=pd.DataFrame(model.estimate(user_code_id, item_code_id, test_ui))\n",
" to_append=ev.evaluate(test=pd.read_csv('./Datasets/ml-100k/test.csv', sep='\\t', header=None),\n",
" estimations_df=estimations_df, \n",
" reco=np.array(reco),\n",
" super_reactions=[4,5])\n",
" to_append.insert(0, \"Beta\", beta)\n",
" result.append(to_append)\n",
" \n",
"result=pd.concat(result)\n",
"result"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABCwAAAkoCAYAAACgVC5GAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADh0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uMy4yLjEsIGh0dHA6Ly9tYXRwbG90bGliLm9yZy+j8jraAAAgAElEQVR4nOzdeXxU1fnH8c/JvpGEAAkkIexLAoGAYRNRZBMVxeICinWhLXW3rnW3tba/WhdsLbW1rbVWRNCquLOouFR2CTuExZCQhAQCIQnZk/P7YyYxRBAEMjfJfN+v17wy99xzZ54b8eTOM+c+x1hrERERERERERFpTnycDkBEREREREREpDElLERERERERESk2VHCQkRERERERESaHSUsRERERERERKTZUcJCRERERERERJodJSxEREREREREpNlRwkJERERERETqGWP+aox5+AT6bTLGjPZASOKllLCQVscYk2GMKTPGlBhj9hpjXjLGhLn3vWSMscaYyY2OmeVuv869HWCMedoYs8f9OhnGmGeP8R51jz979ERFRFoI95hZaYxp36h9rXvs7dqg7VfutmGN+l5njKlpNO6WGGNiPXMWIiLew1p7g7X2NyfQr5+1dqkHQhIvpYSFt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yIiXq61j8Uab0XkdLDWzgZmG2OuAh7CdQsIAMaYfsATwIRjHPsC8AJAamqqbfpoRVqv1n7d0tSa4rqo1SYs9uzZQ5s2bejatSvGGKfD8RhrLQUFBezZs4du3bo5HY6IeLnWPBZrvBWRE5ANdG6wHe9uO5bXgOfrNowx8cBbwDXW2p1NEqGI1GvN1y1Nramui1ptDYvy8nLatWvndf/QjDG0a9dOWUERaRZa81is8VZETsAqoJcxppsxJgCYBrzTsIMxpleDzQuB7e72SOB94D5r7f88FK+IV2vN1y1Nramui1ptwgLw2n9o3nreItI8teYxqTWfm4icOmttNXALsBDYAsy31m4yxjxmjLnY3e0WY8wmY0warjoWdbeD3AL0BB4xxqS5H9GePgcRb6O/7SevKX53rTph4TRfX19SUlLo378/F110EYWFhQBkZGRgjOGhhx6q77t//378/f255ZZbANi2bRujR48mJSWFxMREZs6cCcDSpUuJiIggJSWl/rFkyRLPn5yISAthjOHqq6+u366urqZDhw5MmjTpiH6XXHIJw4cPP6LtV7/6FXFxcUeMuXVjuYjIibDWfmCt7W2t7WGt/a277RFr7Tvu57dba/tZa1Ostedaaze52x+31oa62+se+U6ei4i0TKtXr+a222475v6cnBwuu+wyD0Z04pSwaELBwcGkpaWxceNGoqKimD17dv2+bt268f7779dvv/766/Tr169++7bbbuOOO+4gLS2NLVu2cOutt9bvGzVqFGlpafWPcePGeeaERERaoNDQUDZu3EhZWRkAixcvJi4u7og+hYWFrFmzhkOHDrFr164j9tWNxXWPyMhIj8UuIiIi0lhNTc0P6p+amsqf/vSnY+6PjY3ljTfeONWwmoQSFh4yYsQIsrO/rbEUEhJCYmIiq1evBmDevHlcccUV9ftzc3OJj4+v305OTvZcsCIircwFF1xQnySeO3cuV1555RH733zzTS666CKmTZvGa6+95kSIIiIiImRkZNC3b1+mT59OYmIil112GaWlpXTt2pVf/vKXDB48mNdff51FixYxYsQIBg8ezOWXX05JSQkAq1at4swzz2TgwIEMHTqU4uJili5dWj+z9LPPPqufNTpo0CCKi4vJyMigf//+gKuOx/XXX09ycjKDBg3i008/BeCll15iypQpTJw4kV69enHvvfd65PfRalcJaejX725ic07RaX3NpNhwHr2
"text/plain": [
"<Figure size 1296x3024 with 18 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"### import matplotlib.pyplot as plt\n",
"\n",
"metrics=list(result.columns[[i not in ['Beta'] for i in result.columns]])\n",
"\n",
"charts_per_row=6\n",
"charts_per_column=3\n",
"\n",
"fig, axes = plt.subplots(nrows=charts_per_row, ncols=charts_per_column,figsize=(18, 7*charts_per_row ))\n",
"import itertools\n",
"to_iter=[i for i in itertools.product(range(charts_per_row), range(charts_per_column))]\n",
"\n",
"for i in range(len(metrics)):\n",
" df=result[['Beta', metrics[i]]]\n",
" df.plot(ax=axes[to_iter[i]], title=metrics[i], x=0, y=1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Check sample recommendations"
]
},
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>user</th>\n",
" <th>rating</th>\n",
" <th>title</th>\n",
" <th>genres</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>25689</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>Citizen Kane (1941)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>46234</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>Miller's Crossing (1990)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29481</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>Psycho (1960)</td>\n",
" <td>Horror, Romance, Thriller</td>\n",
" </tr>\n",
" <tr>\n",
" <th>29861</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>To Kill a Mockingbird (1962)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>24699</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>One Flew Over the Cuckoo's Nest (1975)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>64848</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>Taxi Driver (1976)</td>\n",
" <td>Drama, Thriller</td>\n",
" </tr>\n",
" <tr>\n",
" <th>31022</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>GoodFellas (1990)</td>\n",
" <td>Crime, Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>23585</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>Casablanca (1942)</td>\n",
" <td>Drama, Romance, War</td>\n",
" </tr>\n",
" <tr>\n",
" <th>18551</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>Amadeus (1984)</td>\n",
" <td>Drama, Mystery</td>\n",
" </tr>\n",
" <tr>\n",
" <th>40333</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>Exotica (1994)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>42006</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>Dr. Strangelove or: How I Learned to Stop Worr...</td>\n",
" <td>Sci-Fi, War</td>\n",
" </tr>\n",
" <tr>\n",
" <th>27477</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>Young Frankenstein (1974)</td>\n",
" <td>Comedy, Horror</td>\n",
" </tr>\n",
" <tr>\n",
" <th>43025</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>2001: A Space Odyssey (1968)</td>\n",
" <td>Drama, Mystery, Sci-Fi, Thriller</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12217</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>Graduate, The (1967)</td>\n",
" <td>Drama, Romance</td>\n",
" </tr>\n",
" <tr>\n",
" <th>42731</th>\n",
" <td>645</td>\n",
" <td>5</td>\n",
" <td>Brazil (1985)</td>\n",
" <td>Sci-Fi</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" user rating title \\\n",
"25689 645 5 Citizen Kane (1941) \n",
"46234 645 5 Miller's Crossing (1990) \n",
"29481 645 5 Psycho (1960) \n",
"29861 645 5 To Kill a Mockingbird (1962) \n",
"24699 645 5 One Flew Over the Cuckoo's Nest (1975) \n",
"64848 645 5 Taxi Driver (1976) \n",
"31022 645 5 GoodFellas (1990) \n",
"23585 645 5 Casablanca (1942) \n",
"18551 645 5 Amadeus (1984) \n",
"40333 645 5 Exotica (1994) \n",
"42006 645 5 Dr. Strangelove or: How I Learned to Stop Worr... \n",
"27477 645 5 Young Frankenstein (1974) \n",
"43025 645 5 2001: A Space Odyssey (1968) \n",
"12217 645 5 Graduate, The (1967) \n",
"42731 645 5 Brazil (1985) \n",
"\n",
" genres \n",
"25689 Drama \n",
"46234 Drama \n",
"29481 Horror, Romance, Thriller \n",
"29861 Drama \n",
"24699 Drama \n",
"64848 Drama, Thriller \n",
"31022 Crime, Drama \n",
"23585 Drama, Romance, War \n",
"18551 Drama, Mystery \n",
"40333 Drama \n",
"42006 Sci-Fi, War \n",
"27477 Comedy, Horror \n",
"43025 Drama, Mystery, Sci-Fi, Thriller \n",
"12217 Drama, Romance \n",
"42731 Sci-Fi "
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>user</th>\n",
" <th>rec_nb</th>\n",
" <th>title</th>\n",
" <th>genres</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>284</th>\n",
" <td>645.0</td>\n",
" <td>1</td>\n",
" <td>Star Wars (1977)</td>\n",
" <td>Action, Adventure, Romance, Sci-Fi, War</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7185</th>\n",
" <td>645.0</td>\n",
" <td>2</td>\n",
" <td>Fargo (1996)</td>\n",
" <td>Crime, Drama, Thriller</td>\n",
" </tr>\n",
" <tr>\n",
" <th>620</th>\n",
" <td>645.0</td>\n",
" <td>3</td>\n",
" <td>Raiders of the Lost Ark (1981)</td>\n",
" <td>Action, Adventure</td>\n",
" </tr>\n",
" <tr>\n",
" <th>872</th>\n",
" <td>645.0</td>\n",
" <td>4</td>\n",
" <td>Silence of the Lambs, The (1991)</td>\n",
" <td>Drama, Thriller</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2483</th>\n",
" <td>645.0</td>\n",
" <td>5</td>\n",
" <td>Godfather, The (1972)</td>\n",
" <td>Action, Crime, Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6723</th>\n",
" <td>645.0</td>\n",
" <td>6</td>\n",
" <td>Empire Strikes Back, The (1980)</td>\n",
" <td>Action, Adventure, Drama, Romance, Sci-Fi, War</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1440</th>\n",
" <td>645.0</td>\n",
" <td>7</td>\n",
" <td>Fugitive, The (1993)</td>\n",
" <td>Action, Thriller</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3288</th>\n",
" <td>645.0</td>\n",
" <td>8</td>\n",
" <td>Toy Story (1995)</td>\n",
" <td>Animation, Children's, Comedy</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8416</th>\n",
" <td>645.0</td>\n",
" <td>9</td>\n",
" <td>Indiana Jones and the Last Crusade (1989)</td>\n",
" <td>Action, Adventure</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2062</th>\n",
" <td>645.0</td>\n",
" <td>10</td>\n",
" <td>Back to the Future (1985)</td>\n",
" <td>Comedy, Sci-Fi</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" user rec_nb title \\\n",
"284 645.0 1 Star Wars (1977) \n",
"7185 645.0 2 Fargo (1996) \n",
"620 645.0 3 Raiders of the Lost Ark (1981) \n",
"872 645.0 4 Silence of the Lambs, The (1991) \n",
"2483 645.0 5 Godfather, The (1972) \n",
"6723 645.0 6 Empire Strikes Back, The (1980) \n",
"1440 645.0 7 Fugitive, The (1993) \n",
"3288 645.0 8 Toy Story (1995) \n",
"8416 645.0 9 Indiana Jones and the Last Crusade (1989) \n",
"2062 645.0 10 Back to the Future (1985) \n",
"\n",
" genres \n",
"284 Action, Adventure, Romance, Sci-Fi, War \n",
"7185 Crime, Drama, Thriller \n",
"620 Action, Adventure \n",
"872 Drama, Thriller \n",
"2483 Action, Crime, Drama \n",
"6723 Action, Adventure, Drama, Romance, Sci-Fi, War \n",
"1440 Action, Thriller \n",
"3288 Animation, Children's, Comedy \n",
"8416 Action, Adventure \n",
"2062 Comedy, Sci-Fi "
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train=pd.read_csv('./Datasets/ml-100k/train.csv', sep='\\t', header=None, names=['user', 'item', 'rating', 'timestamp'])\n",
"items=pd.read_csv('./Datasets/ml-100k/movies.csv')\n",
"\n",
"user=random.choice(list(set(train['user'])))\n",
"\n",
"train_content=pd.merge(train, items, left_on='item', right_on='id')\n",
"display(train_content[train_content['user']==user][['user', 'rating', 'title', 'genres']]\\\n",
" .sort_values(by='rating', ascending=False)[:15])\n",
"\n",
"reco = np.loadtxt('Recommendations generated/ml-100k/Self_RP3Beta_reco.csv', delimiter=',')\n",
"items=pd.read_csv('./Datasets/ml-100k/movies.csv')\n",
"\n",
"# Let's ignore scores - they are not used in evaluation: \n",
"reco_users=reco[:,:1]\n",
"reco_items=reco[:,1::2]\n",
"# Let's put them into one array\n",
"reco=np.concatenate((reco_users, reco_items), axis=1)\n",
"\n",
"# Let's rebuild it user-item dataframe\n",
"recommended=[]\n",
"for row in reco:\n",
" for rec_nb, entry in enumerate(row[1:]):\n",
" recommended.append((row[0], rec_nb+1, entry))\n",
"recommended=pd.DataFrame(recommended, columns=['user','rec_nb', 'item'])\n",
"\n",
"recommended_content=pd.merge(recommended, items, left_on='item', right_on='id')\n",
"recommended_content[recommended_content['user']==user][['user', 'rec_nb', 'title', 'genres']].sort_values(by='rec_nb')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# project task 6: generate recommendations of RP3Beta for hiperparameters found to optimize recall"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
"# use better values than (1,0) for alpha and beta\n",
"# if you want you can also modify the model to consider different weights (we took as weights user ratings, maybe take ones or squares of ratings instead)\n",
"# save the outptut in 'Recommendations generated/ml-100k/Self_RP3Beta_estimations.csv'\n",
"# and 'Recommendations generated/ml-100k/Self_RP3Beta_reco.csv'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# project task 7 (optional): implement graph-based model of your choice "
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
"# for example change length of paths in RP3beta\n",
"# save the outptut in 'Recommendations generated/ml-100k/Self_GraphTask_estimations.csv'\n",
"# and 'Recommendations generated/ml-100k/Self_GraphTask_reco.csv'"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
}
},
"nbformat": 4,
"nbformat_minor": 4
}