introduction_to_recommender.../P5. Graph-based.ipynb

1432 lines
552 KiB
Plaintext
Raw Normal View History

2021-05-07 22:16:28 +02:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Self made RP3-beta"
]
},
{
"cell_type": "code",
"execution_count": 1,
"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",
"(\n",
" train_ui,\n",
" test_ui,\n",
" user_code_id,\n",
" user_id_code,\n",
" item_code_id,\n",
" item_id_code,\n",
") = helpers.data_to_csr(train_read, test_read)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"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)(\n",
" self.train_iu.sum(axis=1)\n",
" ) # 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(\n",
" np.vectorize(lambda x: x if x > 0 else 1)(item_orders), self.beta\n",
" )\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[\n",
" self.train_ui.indptr[nb_user] : self.train_ui.indptr[nb_user + 1]\n",
" ]\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(\n",
" [\n",
" user_code_id[user],\n",
" item_code_id[item],\n",
" self.estimations[user, item]\n",
" if not np.isnan(self.estimations[user, item])\n",
" else 1,\n",
" ]\n",
" )\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"model = RP3Beta()\n",
"model.fit(train_ui, alpha=1, beta=0)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"top_n = pd.DataFrame(model.recommend(user_code_id, item_code_id, topK=10))\n",
"\n",
"top_n.to_csv(\n",
" \"Recommendations generated/ml-100k/Self_P3_reco.csv\", index=False, header=False\n",
")\n",
"\n",
"estimations = pd.DataFrame(model.estimate(user_code_id, item_code_id, test_ui))\n",
"estimations.to_csv(\n",
" \"Recommendations generated/ml-100k/Self_P3_estimations.csv\",\n",
" index=False,\n",
" header=False,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"943it [00:00, 8787.46it/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": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import evaluation_measures as ev\n",
"\n",
"estimations_df = pd.read_csv(\n",
" \"Recommendations generated/ml-100k/Self_P3_estimations.csv\", header=None\n",
")\n",
"reco = np.loadtxt(\"Recommendations generated/ml-100k/Self_P3_reco.csv\", delimiter=\",\")\n",
"\n",
"ev.evaluate(\n",
" 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],\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Let's check hyperparameters"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"##### Alpha"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 0/8 [00:00<?, ?it/s]\n",
"943it [00:00, 9826.90it/s]\n",
" 12%|█▎ | 1/8 [00:07<00:55, 7.93s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 8050.54it/s]\u001b[A\n",
" 25%|██▌ | 2/8 [00:15<00:47, 7.94s/it]\n",
"943it [00:00, 9441.40it/s]\n",
" 38%|███▊ | 3/8 [00:24<00:40, 8.01s/it]\n",
"943it [00:00, 9745.57it/s]\n",
" 50%|█████ | 4/8 [00:32<00:32, 8.13s/it]\n",
"943it [00:00, 10134.88it/s]\n",
" 62%|██████▎ | 5/8 [00:40<00:23, 7.98s/it]\n",
"943it [00:00, 10252.12it/s]\n",
" 75%|███████▌ | 6/8 [00:47<00:15, 7.94s/it]\n",
"943it [00:00, 9769.78it/s]\n",
" 88%|████████▊ | 7/8 [00:55<00:07, 7.97s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 8272.37it/s]\u001b[A\n",
"100%|██████████| 8/8 [01:03<00:00, 7.99s/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": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from tqdm import tqdm\n",
"\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(\n",
" 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",
" )\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": 7,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABCwAAAkoCAYAAACgVC5GAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAEAAElEQVR4nOz9e3yU9Z3//z9eOWdCIJkQICEZEhVRUEALeD7V2qoVtV3b4lrXWv247qd2u213P7X76bbdtn632213u/2sXX9uW3Wr1R7sAaxnV9t6QEAFBAFFgRASIIRwzjmv3x9zBYcYIEBmrsnM8367zS1zva/rmnlNHN9ceV3v9+tt7o6IiIiIiIiISDrJCTsAEREREREREZGBlLAQERERERERkbSjhIWIiIiIiIiIpB0lLEREREREREQk7ShhISIiIiIiIiJpRwkLEREREREREUk7SliIiIiIiIjIfmZ2l5n9wxCOW2lmFyY/IslWSlhIxjGz9WbWbmZ7zGyzmd1rZqOCffeamZvZlQPO+X7Q/qlgu8DMvmdmjcHrrDOzfzvIe/Q//iOlH1REZAQJ+s0uMxs7oH1p0P/WJbR9PWibM+DYT5lZ74C+d4+ZVafoY4iIZAV3v9XdvzmE46a5+3MpCEmylBIWkqnmuvsoYCZwGvDlhH1vAjf0b5hZHvAx4O2EY74MzALmAKXARcBrg71HwuO2Yf8UIiKZZR1wbf+GmZ0KFCceYGYGXA9sJ6GvTvDSgL53lLs3JTNoEZGRKLjGFRnRlLCQjObum4EniCcu+i0AzjGz8mD7UmA5sDnhmNnAb9y9yePWu/t/pyJmEZEM9lPgLxK2bwAG9q3n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+Z8Q//8A/7j922bRv5+fncdtttAKxZs4YLL7yQmTNncvLJJ3PLLbcA8NxzzzFmzBhmzpy5//H000+n/LOJiIxkZsb111+/f7unp4fKykquuOKKA4676qqrOOussw5o+/rXv87EiRMP6If7+/cUmghsTNhuDNoO5ibgMQB33wR8F2gAmoGd7v7kIOd8uv+cQL2ZvWZmfzCz844leBHJXkuWLOGv//qvD7q/qamJa665JoURDZ0SFiKSUYqLi1m6dCkrVqwgGo1y55137t933HHH8cgjj+zf/uUvf8m0adP2b//1X/81n//851m6dCmrVq3is5/97P595513HkuXLt3/+MAHPpCaDyQikiFKSkpYsWIF7e3tADz11FNMnHjg3/s7duzg1VdfZceOHaxbt+6Aff39c/+jrKwsVaH3G+y2oQ96oNlFxBMWXwq2y4mPxqgHqoESM/vkgHP+L9ADPBA0NQMxdz8N+ALwMzMbPch73WJmS8xsSUtLy1F9MBEZWXp7e4/o+FmzZvGDH/zgoPurq6v51a9+daxhJYUSFiKSsc466yw2bdq0f7u4uJiTTz6ZJUuWAPDzn/+cj3/84/v3Nzc3U1Pz7kjcU089NXXBiohkgcsuu4zf//73ADz44INce+21B+x/+OGHmTt3LvPmzeOhhx4KI8RDaQRqE7ZrCKZ1JDKz6cCPgKvcvTVo/gCwzt1b3L0b+DVwdsI5NwBXANe5uwO4e2f/+e7+CvGCnCcOfD93v9vdZ7n7rMrKymH4mCISpvXr13PSSSdxww03MH36dK655hr27dtHXV0d3/jGNzj33HP55S9/yZNPPslZZ53F6aefzsc+9jH27NkDwOLFizn77LOZMWMGc+bMYffu3Tz33HP7R7P94Q9/2D9S7bTTTmP37t2sX7+eU045BYjX8Ljxxhs59dRTOe2003j22WcBuPfee/noRz/KpZdeyuTJk/k//+f/pOT3oVVCRCQp/nHBSt5o2jWsrzm1ejRfmzvt8AcSzzw/88wz3HTTTQe0918ET5gwgdzcXKqrq2l
"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(\n",
" nrows=charts_per_row, ncols=charts_per_column, figsize=(18, 7 * charts_per_row)\n",
")\n",
"import itertools\n",
"\n",
"to_iter = [\n",
" i for i in itertools.product(range(charts_per_row), range(charts_per_column))\n",
"]\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": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
" 0%| | 0/10 [00:00<?, ?it/s]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 8133.31it/s]\u001b[A\n",
" 10%|█ | 1/10 [00:08<01:19, 8.87s/it]\n",
"0it [00:00, ?it/s]\u001b[A\n",
"943it [00:00, 8733.83it/s]\u001b[A\n",
" 20%|██ | 2/10 [00:17<01:10, 8.81s/it]\n",
"943it [00:00, 9872.77it/s]\n",
" 30%|███ | 3/10 [00:25<01:00, 8.61s/it]\n",
"943it [00:00, 10989.65it/s]\n",
" 40%|████ | 4/10 [00:33<00:50, 8.36s/it]\n",
"943it [00:00, 10108.59it/s]\n",
" 50%|█████ | 5/10 [00:41<00:40, 8.15s/it]\n",
"943it [00:00, 9989.31it/s]\n",
" 60%|██████ | 6/10 [00:49<00:32, 8.09s/it]\n",
"943it [00:00, 10311.97it/s]\n",
" 70%|███████ | 7/10 [00:56<00:23, 7.92s/it]\n",
"943it [00:00, 10283.20it/s]\n",
" 80%|████████ | 8/10 [01:04<00:15, 7.79s/it]\n",
"943it [00:00, 10486.98it/s]\n",
" 90%|█████████ | 9/10 [01:11<00:07, 7.75s/it]\n",
"943it [00:00, 10330.42it/s]\n",
"100%|██████████| 10/10 [01:19<00:00, 7.95s/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": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from tqdm import tqdm\n",
"\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(\n",
" 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",
" )\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": 9,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAABCwAAAkoCAYAAACgVC5GAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjMuMiwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy8vihELAAAACXBIWXMAAAsTAAALEwEAmpwYAAEAAElEQVR4nOzdd3zV5f3//8criwwgARJmEhIQZA9FhrturUpbd9UO21rb2lZrh3bPX4d2fmq/fvx02Dpw1FnrAK2zkgAiIltIAoQZEkII2cnr98c5wRCDBEjyPjnneb/dzq3n/X5f75PXRezFm9e5rtdl7o6IiIiIiIiISCSJCzoAEREREREREZH2lLAQERERERERkYijhIWIiIiIiIiIRBwlLEREREREREQk4ihhISIiIiIiIiIRRwkLEREREREREYk4SliIiIiIiIjIfmZ2l5l9rxPtVprZ6d0fkcQqJSwk6phZiZnVmlm1mW03s3vMrG/42j1m5mZ2cbt7fhc+/6nwcZKZ/drMSsOfU2xmvz3Iz2h9/bFHOyoi0ouEx80GM8tsd35ZePzNa3Puh+FzM9u1/ZSZNbcbe6vNbHgPdUNEJCa4+w3u/pNOtJvo7i/3QEgSo5SwkGh1kbv3BaYB04Hb2lxbB3yy9cDMEoDLgA1t2twGzABmAv2ADwFvdfQz2rxu7PJeiIhEl2LgqtYDM5sMpLRtYGYGXAtU0GasbmNhu7G3r7tv7c6gRUR6o/AzrkivpoSFRDV33w48Tyhx0epfwElmNiB8fB6wHNjeps0JwOPuvtVDStz9Hz0Rs4hIFLsX+ESb408C7cfWU4DhwFeBK80sqYdiExHpFcIz1m4zs1VmttvM/mZmyWZ2enh28LfMbDvwNzOLM7NbzWyDmZWb2cNmNrDNZ51sZm+YWaWZbW4z2/geM/tp+H2mmT0dblNhZq+ZWVybWM4Kv+8TnrW8Nfz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qvn4XcAnwCTNrBGqBK8I7hpwMXAu8Y2bLwh/5bXd/psc7IhJj9Hf7kevqP7uonWERCeLj45k2bRqTJk3ioosuorKyEoCSkhLMjO9973v72+7atYvExERuvPFGANauXcvpp5/OtGnTGD9+PNdffz0AL7/8Munp6UybNm3/64UXXujxvomI9BZmxrXXXrv/uKmpiaysLC688MID2s2dO5c5c+YccO6HP/whI0aMOGDMbR3LRUQ6w92fcfex7j7a3X8WPndXOFmBu//S3Se6+zR3n+Pur4fPv+7u5u5TwtemKVkhIkdiyZIlfOUrXzno9a1bt3LppZf2YESdp4RFN0pJSWHZsmWsWLGCgQMHcuedd+6/NmrUKJ5++un9x4888ggTJ07cf/yVr3yFm2++mWXLlrF69Wq+/OUv7792yimnsGzZsv2vs87aX5dJRETaSUtLY8WKFdTW1gKwYMECRow4sEh/ZWUlS5cupbKykuLi4gOutY7Fra+MjIyeCl1ERETkfZqbmw+r/YwZM/jDH/5w0OvDhw/nn//859GG1S2UsOghc+bMYcuWLfuPU1JSGD9+PEuWLAHgoYce4vLLL99/fdu2bWRnZ+8/njx5cs8FKyISZc4//3z+/e9/AzBv3jyuuuqqA64/+uijXHTRRVx55ZU8+OCDQYQoIiIiQklJCePGjeOTn/wkU6ZM4dJLL6Wmpoa8vDx+/OMfc/LJJ/PII48wf/585syZw3HHHcdll11GdXU1AIsXL+bEE09k6tSpzJw5k7179/Lyyy/vn1n6yiuv7J81On36dPbu3UtJSQmTJk0CQjU8Pv3pTzN58mSmT5/OSy+9BMA999zDxz72Mc477zzGjBnDN7/5zR7584jqGhatfvSvlaza+r5doI7KhOH9+cFFEw/dkFAG7MUXX+Qzn/nMAedbH4yHDh1KfHw
"text/plain": [
"<Figure size 1296x3024 with 18 Axes>"
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"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(\n",
" nrows=charts_per_row, ncols=charts_per_column, figsize=(18, 7 * charts_per_row)\n",
")\n",
"import itertools\n",
"\n",
"to_iter = [\n",
" i for i in itertools.product(range(charts_per_row), range(charts_per_column))\n",
"]\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": 10,
"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>39675</th>\n",
" <td>599</td>\n",
" <td>5</td>\n",
" <td>That Thing You Do! (1996)</td>\n",
" <td>Comedy</td>\n",
" </tr>\n",
" <tr>\n",
" <th>44133</th>\n",
" <td>599</td>\n",
" <td>5</td>\n",
" <td>Father of the Bride Part II (1995)</td>\n",
" <td>Comedy</td>\n",
" </tr>\n",
" <tr>\n",
" <th>55221</th>\n",
" <td>599</td>\n",
" <td>5</td>\n",
" <td>Fan, The (1996)</td>\n",
" <td>Thriller</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34957</th>\n",
" <td>599</td>\n",
" <td>5</td>\n",
" <td>My Best Friend's Wedding (1997)</td>\n",
" <td>Comedy, Romance</td>\n",
" </tr>\n",
" <tr>\n",
" <th>34654</th>\n",
" <td>599</td>\n",
" <td>5</td>\n",
" <td>Sabrina (1995)</td>\n",
" <td>Comedy, Romance</td>\n",
" </tr>\n",
" <tr>\n",
" <th>56202</th>\n",
" <td>599</td>\n",
" <td>5</td>\n",
" <td>Happy Gilmore (1996)</td>\n",
" <td>Comedy</td>\n",
" </tr>\n",
" <tr>\n",
" <th>57099</th>\n",
" <td>599</td>\n",
" <td>5</td>\n",
" <td>Up Close and Personal (1996)</td>\n",
" <td>Drama, Romance</td>\n",
" </tr>\n",
" <tr>\n",
" <th>60181</th>\n",
" <td>599</td>\n",
" <td>5</td>\n",
" <td>One Night Stand (1997)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5385</th>\n",
" <td>599</td>\n",
" <td>5</td>\n",
" <td>Jerry Maguire (1996)</td>\n",
" <td>Drama, Romance</td>\n",
" </tr>\n",
" <tr>\n",
" <th>12546</th>\n",
" <td>599</td>\n",
" <td>5</td>\n",
" <td>Truth About Cats &amp; Dogs, The (1996)</td>\n",
" <td>Comedy, Romance</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9903</th>\n",
" <td>599</td>\n",
" <td>5</td>\n",
" <td>Time to Kill, A (1996)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8705</th>\n",
" <td>599</td>\n",
" <td>5</td>\n",
" <td>To Gillian on Her 37th Birthday (1996)</td>\n",
" <td>Drama, Romance</td>\n",
" </tr>\n",
" <tr>\n",
" <th>63190</th>\n",
" <td>599</td>\n",
" <td>4</td>\n",
" <td>Craft, The (1996)</td>\n",
" <td>Drama, Horror</td>\n",
" </tr>\n",
" <tr>\n",
" <th>60634</th>\n",
" <td>599</td>\n",
" <td>4</td>\n",
" <td>Romy and Michele's High School Reunion (1997)</td>\n",
" <td>Comedy</td>\n",
" </tr>\n",
" <tr>\n",
" <th>63675</th>\n",
" <td>599</td>\n",
" <td>4</td>\n",
" <td>Set It Off (1996)</td>\n",
" <td>Action, Crime</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" user rating title \\\n",
"39675 599 5 That Thing You Do! (1996) \n",
"44133 599 5 Father of the Bride Part II (1995) \n",
"55221 599 5 Fan, The (1996) \n",
"34957 599 5 My Best Friend's Wedding (1997) \n",
"34654 599 5 Sabrina (1995) \n",
"56202 599 5 Happy Gilmore (1996) \n",
"57099 599 5 Up Close and Personal (1996) \n",
"60181 599 5 One Night Stand (1997) \n",
"5385 599 5 Jerry Maguire (1996) \n",
"12546 599 5 Truth About Cats & Dogs, The (1996) \n",
"9903 599 5 Time to Kill, A (1996) \n",
"8705 599 5 To Gillian on Her 37th Birthday (1996) \n",
"63190 599 4 Craft, The (1996) \n",
"60634 599 4 Romy and Michele's High School Reunion (1997) \n",
"63675 599 4 Set It Off (1996) \n",
"\n",
" genres \n",
"39675 Comedy \n",
"44133 Comedy \n",
"55221 Thriller \n",
"34957 Comedy, Romance \n",
"34654 Comedy, Romance \n",
"56202 Comedy \n",
"57099 Drama, Romance \n",
"60181 Drama \n",
"5385 Drama, Romance \n",
"12546 Comedy, Romance \n",
"9903 Drama \n",
"8705 Drama, Romance \n",
"63190 Drama, Horror \n",
"60634 Comedy \n",
"63675 Action, Crime "
]
},
"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>262</th>\n",
" <td>599.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>7163</th>\n",
" <td>599.0</td>\n",
" <td>2</td>\n",
" <td>Fargo (1996)</td>\n",
" <td>Crime, Drama, Thriller</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2874</th>\n",
" <td>599.0</td>\n",
" <td>3</td>\n",
" <td>Return of the Jedi (1983)</td>\n",
" <td>Action, Adventure, Romance, Sci-Fi, War</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4627</th>\n",
" <td>599.0</td>\n",
" <td>4</td>\n",
" <td>Air Force One (1997)</td>\n",
" <td>Action, Thriller</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5873</th>\n",
" <td>599.0</td>\n",
" <td>5</td>\n",
" <td>Contact (1997)</td>\n",
" <td>Drama, Sci-Fi</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8551</th>\n",
" <td>599.0</td>\n",
" <td>6</td>\n",
" <td>Independence Day (ID4) (1996)</td>\n",
" <td>Action, Sci-Fi, War</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1189</th>\n",
" <td>599.0</td>\n",
" <td>7</td>\n",
" <td>English Patient, The (1996)</td>\n",
" <td>Drama, Romance, War</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8274</th>\n",
" <td>599.0</td>\n",
" <td>8</td>\n",
" <td>Mr. Holland's Opus (1995)</td>\n",
" <td>Drama</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7633</th>\n",
" <td>599.0</td>\n",
" <td>9</td>\n",
" <td>Rock, The (1996)</td>\n",
" <td>Action, Adventure, Thriller</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2464</th>\n",
" <td>599.0</td>\n",
" <td>10</td>\n",
" <td>Godfather, The (1972)</td>\n",
" <td>Action, Crime, Drama</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" user rec_nb title \\\n",
"262 599.0 1 Star Wars (1977) \n",
"7163 599.0 2 Fargo (1996) \n",
"2874 599.0 3 Return of the Jedi (1983) \n",
"4627 599.0 4 Air Force One (1997) \n",
"5873 599.0 5 Contact (1997) \n",
"8551 599.0 6 Independence Day (ID4) (1996) \n",
"1189 599.0 7 English Patient, The (1996) \n",
"8274 599.0 8 Mr. Holland's Opus (1995) \n",
"7633 599.0 9 Rock, The (1996) \n",
"2464 599.0 10 Godfather, The (1972) \n",
"\n",
" genres \n",
"262 Action, Adventure, Romance, Sci-Fi, War \n",
"7163 Crime, Drama, Thriller \n",
"2874 Action, Adventure, Romance, Sci-Fi, War \n",
"4627 Action, Thriller \n",
"5873 Drama, Sci-Fi \n",
"8551 Action, Sci-Fi, War \n",
"1189 Drama, Romance, War \n",
"8274 Drama \n",
"7633 Action, Adventure, Thriller \n",
"2464 Action, Crime, Drama "
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"train = pd.read_csv(\n",
" \"./Datasets/ml-100k/train.csv\",\n",
" sep=\"\\t\",\n",
" header=None,\n",
" names=[\"user\", \"item\", \"rating\", \"timestamp\"],\n",
")\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(\n",
" train_content[train_content[\"user\"] == user][\n",
" [\"user\", \"rating\", \"title\", \"genres\"]\n",
" ].sort_values(by=\"rating\", ascending=False)[:15]\n",
")\n",
"\n",
"reco = np.loadtxt(\"Recommendations generated/ml-100k/Self_P3_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][\n",
" [\"user\", \"rec_nb\", \"title\", \"genres\"]\n",
"].sort_values(by=\"rec_nb\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# project task 5: generate recommendations of RP3Beta for hyperparameters found to optimize recall"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# We generated recommendations for P3, a special case of RP3Beta (with alpha=1, beta=0).\n",
"# We've observed that changing alpha and beta impacts the model performance.\n",
"\n",
"# Your task is find values alpha and beta for which recall will be the highest, train the model and generate recommendations.\n",
"\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 6 (optional): implement graph-based model of your choice "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# for example change length of paths in RP3beta or make some other modification (but change more than input and hyperparameters)\n",
"# feel free to implement your idea or search for some ideas\n",
"\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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}