2220 lines
69 KiB
Plaintext
2220 lines
69 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"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": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# User and item features preparation"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Item features"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"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>0</th>\n",
|
||
" <th>1</th>\n",
|
||
" <th>2</th>\n",
|
||
" <th>3</th>\n",
|
||
" <th>4</th>\n",
|
||
" <th>5</th>\n",
|
||
" <th>6</th>\n",
|
||
" <th>7</th>\n",
|
||
" <th>8</th>\n",
|
||
" <th>9</th>\n",
|
||
" <th>...</th>\n",
|
||
" <th>14</th>\n",
|
||
" <th>15</th>\n",
|
||
" <th>16</th>\n",
|
||
" <th>17</th>\n",
|
||
" <th>18</th>\n",
|
||
" <th>19</th>\n",
|
||
" <th>20</th>\n",
|
||
" <th>21</th>\n",
|
||
" <th>22</th>\n",
|
||
" <th>23</th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>1</td>\n",
|
||
" <td>Toy Story (1995)</td>\n",
|
||
" <td>01-Jan-1995</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>http://us.imdb.com/M/title-exact?Toy%20Story%2...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1</th>\n",
|
||
" <td>2</td>\n",
|
||
" <td>GoldenEye (1995)</td>\n",
|
||
" <td>01-Jan-1995</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>http://us.imdb.com/M/title-exact?GoldenEye%20(...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2</th>\n",
|
||
" <td>3</td>\n",
|
||
" <td>Four Rooms (1995)</td>\n",
|
||
" <td>01-Jan-1995</td>\n",
|
||
" <td>NaN</td>\n",
|
||
" <td>http://us.imdb.com/M/title-exact?Four%20Rooms%...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"<p>3 rows × 24 columns</p>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" 0 1 2 3 \\\n",
|
||
"0 1 Toy Story (1995) 01-Jan-1995 NaN \n",
|
||
"1 2 GoldenEye (1995) 01-Jan-1995 NaN \n",
|
||
"2 3 Four Rooms (1995) 01-Jan-1995 NaN \n",
|
||
"\n",
|
||
" 4 5 6 7 8 9 ... 14 \\\n",
|
||
"0 http://us.imdb.com/M/title-exact?Toy%20Story%2... 0 0 0 1 1 ... 0 \n",
|
||
"1 http://us.imdb.com/M/title-exact?GoldenEye%20(... 0 1 1 0 0 ... 0 \n",
|
||
"2 http://us.imdb.com/M/title-exact?Four%20Rooms%... 0 0 0 0 0 ... 0 \n",
|
||
"\n",
|
||
" 15 16 17 18 19 20 21 22 23 \n",
|
||
"0 0 0 0 0 0 0 0 0 0 \n",
|
||
"1 0 0 0 0 0 0 1 0 0 \n",
|
||
"2 0 0 0 0 0 0 1 0 0 \n",
|
||
"\n",
|
||
"[3 rows x 24 columns]"
|
||
]
|
||
},
|
||
"execution_count": 2,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"movies = pd.read_csv(\n",
|
||
" \"./Datasets/ml-100k/u.item\", sep=\"|\", encoding=\"latin-1\", header=None\n",
|
||
").astype(object)\n",
|
||
"\n",
|
||
"movies[:3]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"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>id_1</th>\n",
|
||
" <th>id_2</th>\n",
|
||
" <th>id_3</th>\n",
|
||
" <th>id_4</th>\n",
|
||
" <th>id_5</th>\n",
|
||
" <th>id_6</th>\n",
|
||
" <th>id_7</th>\n",
|
||
" <th>id_8</th>\n",
|
||
" <th>id_9</th>\n",
|
||
" <th>id_10</th>\n",
|
||
" <th>...</th>\n",
|
||
" <th>date_30-Mar-1996</th>\n",
|
||
" <th>date_30-May-1997</th>\n",
|
||
" <th>date_30-Nov-1996</th>\n",
|
||
" <th>date_30-Oct-1995</th>\n",
|
||
" <th>date_30-Oct-1996</th>\n",
|
||
" <th>date_31-Dec-1997</th>\n",
|
||
" <th>date_31-Jan-1997</th>\n",
|
||
" <th>date_31-Jul-1996</th>\n",
|
||
" <th>date_31-May-1996</th>\n",
|
||
" <th>date_4-Feb-1971</th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1</th>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2</th>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"<p>3 rows × 1922 columns</p>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" id_1 id_2 id_3 id_4 id_5 id_6 id_7 id_8 id_9 id_10 ... \\\n",
|
||
"0 1 0 0 0 0 0 0 0 0 0 ... \n",
|
||
"1 0 1 0 0 0 0 0 0 0 0 ... \n",
|
||
"2 0 0 1 0 0 0 0 0 0 0 ... \n",
|
||
"\n",
|
||
" date_30-Mar-1996 date_30-May-1997 date_30-Nov-1996 date_30-Oct-1995 \\\n",
|
||
"0 0 0 0 0 \n",
|
||
"1 0 0 0 0 \n",
|
||
"2 0 0 0 0 \n",
|
||
"\n",
|
||
" date_30-Oct-1996 date_31-Dec-1997 date_31-Jan-1997 date_31-Jul-1996 \\\n",
|
||
"0 0 0 0 0 \n",
|
||
"1 0 0 0 0 \n",
|
||
"2 0 0 0 0 \n",
|
||
"\n",
|
||
" date_31-May-1996 date_4-Feb-1971 \n",
|
||
"0 0 0 \n",
|
||
"1 0 0 \n",
|
||
"2 0 0 \n",
|
||
"\n",
|
||
"[3 rows x 1922 columns]"
|
||
]
|
||
},
|
||
"execution_count": 3,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"id_date = pd.get_dummies(data=movies[[0, 2]], prefix=[\"id\", \"date\"])\n",
|
||
"id_date[:3]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"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>0</th>\n",
|
||
" <th>1</th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>unknown</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1</th>\n",
|
||
" <td>Action</td>\n",
|
||
" <td>1</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2</th>\n",
|
||
" <td>Adventure</td>\n",
|
||
" <td>2</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" 0 1\n",
|
||
"0 unknown 0\n",
|
||
"1 Action 1\n",
|
||
"2 Adventure 2"
|
||
]
|
||
},
|
||
"execution_count": 4,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"genres = pd.read_csv(\n",
|
||
" \"./Datasets/ml-100k/u.genre\", sep=\"|\", header=None, encoding=\"latin-1\"\n",
|
||
")\n",
|
||
"genres[:3]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"item_genres = movies[np.arange(5, 24)]\n",
|
||
"item_genres.columns = list(genres[0])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"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>id_1</th>\n",
|
||
" <th>id_2</th>\n",
|
||
" <th>id_3</th>\n",
|
||
" <th>id_4</th>\n",
|
||
" <th>id_5</th>\n",
|
||
" <th>id_6</th>\n",
|
||
" <th>id_7</th>\n",
|
||
" <th>id_8</th>\n",
|
||
" <th>id_9</th>\n",
|
||
" <th>id_10</th>\n",
|
||
" <th>...</th>\n",
|
||
" <th>Fantasy</th>\n",
|
||
" <th>Film-Noir</th>\n",
|
||
" <th>Horror</th>\n",
|
||
" <th>Musical</th>\n",
|
||
" <th>Mystery</th>\n",
|
||
" <th>Romance</th>\n",
|
||
" <th>Sci-Fi</th>\n",
|
||
" <th>Thriller</th>\n",
|
||
" <th>War</th>\n",
|
||
" <th>Western</th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1</th>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2</th>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>3</th>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>4</th>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>...</th>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>...</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1677</th>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1678</th>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1679</th>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1680</th>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1681</th>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"<p>1682 rows × 1941 columns</p>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" id_1 id_2 id_3 id_4 id_5 id_6 id_7 id_8 id_9 id_10 ... \\\n",
|
||
"0 1 0 0 0 0 0 0 0 0 0 ... \n",
|
||
"1 0 1 0 0 0 0 0 0 0 0 ... \n",
|
||
"2 0 0 1 0 0 0 0 0 0 0 ... \n",
|
||
"3 0 0 0 1 0 0 0 0 0 0 ... \n",
|
||
"4 0 0 0 0 1 0 0 0 0 0 ... \n",
|
||
"... ... ... ... ... ... ... ... ... ... ... ... \n",
|
||
"1677 0 0 0 0 0 0 0 0 0 0 ... \n",
|
||
"1678 0 0 0 0 0 0 0 0 0 0 ... \n",
|
||
"1679 0 0 0 0 0 0 0 0 0 0 ... \n",
|
||
"1680 0 0 0 0 0 0 0 0 0 0 ... \n",
|
||
"1681 0 0 0 0 0 0 0 0 0 0 ... \n",
|
||
"\n",
|
||
" Fantasy Film-Noir Horror Musical Mystery Romance Sci-Fi Thriller \\\n",
|
||
"0 0 0 0 0 0 0 0 0 \n",
|
||
"1 0 0 0 0 0 0 0 1 \n",
|
||
"2 0 0 0 0 0 0 0 1 \n",
|
||
"3 0 0 0 0 0 0 0 0 \n",
|
||
"4 0 0 0 0 0 0 0 1 \n",
|
||
"... ... ... ... ... ... ... ... ... \n",
|
||
"1677 0 0 0 0 0 0 0 0 \n",
|
||
"1678 0 0 0 0 0 1 0 1 \n",
|
||
"1679 0 0 0 0 0 1 0 0 \n",
|
||
"1680 0 0 0 0 0 0 0 0 \n",
|
||
"1681 0 0 0 0 0 0 0 0 \n",
|
||
"\n",
|
||
" War Western \n",
|
||
"0 0 0 \n",
|
||
"1 0 0 \n",
|
||
"2 0 0 \n",
|
||
"3 0 0 \n",
|
||
"4 0 0 \n",
|
||
"... ... ... \n",
|
||
"1677 0 0 \n",
|
||
"1678 0 0 \n",
|
||
"1679 0 0 \n",
|
||
"1680 0 0 \n",
|
||
"1681 0 0 \n",
|
||
"\n",
|
||
"[1682 rows x 1941 columns]"
|
||
]
|
||
},
|
||
"execution_count": 6,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"item_features_df = pd.concat([id_date, item_genres], axis=1).astype(int)\n",
|
||
"item_features_df"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"<1682x1941 sparse matrix of type '<class 'numpy.int64'>'\n",
|
||
"\twith 6256 stored elements in Compressed Sparse Row format>"
|
||
]
|
||
},
|
||
"execution_count": 7,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"item_features = sparse.csr_matrix(item_features_df.values)\n",
|
||
"item_features"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"### User features"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 8,
|
||
"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>0</th>\n",
|
||
" <th>1</th>\n",
|
||
" <th>2</th>\n",
|
||
" <th>3</th>\n",
|
||
" <th>4</th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>1</td>\n",
|
||
" <td>24</td>\n",
|
||
" <td>M</td>\n",
|
||
" <td>technician</td>\n",
|
||
" <td>85711</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1</th>\n",
|
||
" <td>2</td>\n",
|
||
" <td>53</td>\n",
|
||
" <td>F</td>\n",
|
||
" <td>other</td>\n",
|
||
" <td>94043</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2</th>\n",
|
||
" <td>3</td>\n",
|
||
" <td>23</td>\n",
|
||
" <td>M</td>\n",
|
||
" <td>writer</td>\n",
|
||
" <td>32067</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" 0 1 2 3 4\n",
|
||
"0 1 24 M technician 85711\n",
|
||
"1 2 53 F other 94043\n",
|
||
"2 3 23 M writer 32067"
|
||
]
|
||
},
|
||
"execution_count": 8,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"users = pd.read_csv(\n",
|
||
" \"./Datasets/ml-100k/u.user\", sep=\"|\", encoding=\"latin-1\", header=None\n",
|
||
")\n",
|
||
"users[:3]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"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>id_1</th>\n",
|
||
" <th>id_2</th>\n",
|
||
" <th>id_3</th>\n",
|
||
" <th>id_4</th>\n",
|
||
" <th>id_5</th>\n",
|
||
" <th>id_6</th>\n",
|
||
" <th>id_7</th>\n",
|
||
" <th>id_8</th>\n",
|
||
" <th>id_9</th>\n",
|
||
" <th>id_10</th>\n",
|
||
" <th>...</th>\n",
|
||
" <th>Fantasy</th>\n",
|
||
" <th>Film-Noir</th>\n",
|
||
" <th>Horror</th>\n",
|
||
" <th>Musical</th>\n",
|
||
" <th>Mystery</th>\n",
|
||
" <th>Romance</th>\n",
|
||
" <th>Sci-Fi</th>\n",
|
||
" <th>Thriller</th>\n",
|
||
" <th>War</th>\n",
|
||
" <th>Western</th>\n",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>1</th>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>2</th>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>...</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>1</td>\n",
|
||
" <td>0</td>\n",
|
||
" <td>0</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"<p>3 rows × 1941 columns</p>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" id_1 id_2 id_3 id_4 id_5 id_6 id_7 id_8 id_9 id_10 ... Fantasy \\\n",
|
||
"0 1 0 0 0 0 0 0 0 0 0 ... 0 \n",
|
||
"1 0 1 0 0 0 0 0 0 0 0 ... 0 \n",
|
||
"2 0 0 1 0 0 0 0 0 0 0 ... 0 \n",
|
||
"\n",
|
||
" Film-Noir Horror Musical Mystery Romance Sci-Fi Thriller War \\\n",
|
||
"0 0 0 0 0 0 0 0 0 \n",
|
||
"1 0 0 0 0 0 0 1 0 \n",
|
||
"2 0 0 0 0 0 0 1 0 \n",
|
||
"\n",
|
||
" Western \n",
|
||
"0 0 \n",
|
||
"1 0 \n",
|
||
"2 0 \n",
|
||
"\n",
|
||
"[3 rows x 1941 columns]"
|
||
]
|
||
},
|
||
"execution_count": 9,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"users = users.astype(object)\n",
|
||
"user_features_df = pd.get_dummies(users, [\"id\", \"age\", \"sex\", \"profesion\", \"zip_code\"])\n",
|
||
"item_features_df[:3]"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 10,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"<943x1682 sparse matrix of type '<class 'numpy.int64'>'\n",
|
||
"\twith 80000 stored elements in Compressed Sparse Row format>"
|
||
]
|
||
},
|
||
"execution_count": 10,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"train_ui"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 11,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"data": {
|
||
"text/plain": [
|
||
"<943x1822 sparse matrix of type '<class 'numpy.uint8'>'\n",
|
||
"\twith 4715 stored elements in Compressed Sparse Row format>"
|
||
]
|
||
},
|
||
"execution_count": 11,
|
||
"metadata": {},
|
||
"output_type": "execute_result"
|
||
}
|
||
],
|
||
"source": [
|
||
"user_features = sparse.csr_matrix(user_features_df.values)\n",
|
||
"user_features"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Model"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"#### LightFM with user and item features"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 12,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stderr",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"/Users/robert.kwiecinski/opt/anaconda3/lib/python3.8/site-packages/lightfm/_lightfm_fast.py:9: UserWarning: LightFM was compiled without OpenMP support. Only a single thread will be used.\n",
|
||
" warnings.warn(\n"
|
||
]
|
||
},
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"logistic\n",
|
||
"Train precision: 0.09\n",
|
||
"Test precision: 0.03\n",
|
||
"bpr\n",
|
||
"Train precision: 0.57\n",
|
||
"Test precision: 0.24\n",
|
||
"warp\n",
|
||
"Train precision: 0.63\n",
|
||
"Test precision: 0.35\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"from lightfm import LightFM\n",
|
||
"from lightfm.evaluation import precision_at_k\n",
|
||
"\n",
|
||
"for loss in [\"logistic\", \"bpr\", \"warp\"]:\n",
|
||
"\n",
|
||
" model = LightFM(no_components=10, loss=loss)\n",
|
||
" model.fit(\n",
|
||
" train_ui,\n",
|
||
" user_features=user_features,\n",
|
||
" item_features=item_features,\n",
|
||
" epochs=30,\n",
|
||
" num_threads=4,\n",
|
||
" )\n",
|
||
"\n",
|
||
" print(loss)\n",
|
||
" print(\n",
|
||
" \"Train precision: %.2f\"\n",
|
||
" % precision_at_k(\n",
|
||
" model,\n",
|
||
" test_interactions=train_ui,\n",
|
||
" user_features=user_features,\n",
|
||
" item_features=item_features,\n",
|
||
" k=10,\n",
|
||
" preserve_rows=True,\n",
|
||
" ).mean()\n",
|
||
" )\n",
|
||
" print(\n",
|
||
" \"Test precision: %.2f\"\n",
|
||
" % precision_at_k(\n",
|
||
" model,\n",
|
||
" test_interactions=test_ui,\n",
|
||
" train_interactions=train_ui,\n",
|
||
" user_features=user_features,\n",
|
||
" item_features=item_features,\n",
|
||
" k=10,\n",
|
||
" preserve_rows=True,\n",
|
||
" ).mean()\n",
|
||
" )"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 13,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"def top_k_recommendations(\n",
|
||
" model, user_features, item_features, user_code_id, item_code_id, topK=10\n",
|
||
"):\n",
|
||
" result = []\n",
|
||
" for user_code in range(test_ui.shape[0]):\n",
|
||
" user_rated = train_ui.indices[\n",
|
||
" train_ui.indptr[user_code] : train_ui.indptr[user_code + 1]\n",
|
||
" ]\n",
|
||
" scores = model.predict(\n",
|
||
" user_code,\n",
|
||
" np.arange(train_ui.shape[1]),\n",
|
||
" user_features=user_features,\n",
|
||
" item_features=item_features,\n",
|
||
" )\n",
|
||
"\n",
|
||
" scores[user_rated] = -np.inf # to put rated items at the end of the list\n",
|
||
"\n",
|
||
" top_items = [item_code_id[item] for item in np.argsort(-scores)[:topK]]\n",
|
||
" result.append(\n",
|
||
" [user_code_id[user_code]]\n",
|
||
" + list(chain(*zip(top_items, -np.sort(-scores)[:topK])))\n",
|
||
" )\n",
|
||
" return result\n",
|
||
"\n",
|
||
"\n",
|
||
"def estimate(model, user_features, item_features, 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",
|
||
" model.predict(\n",
|
||
" int(user),\n",
|
||
" np.array([int(item)]),\n",
|
||
" user_features=user_features,\n",
|
||
" item_features=item_features,\n",
|
||
" )[0],\n",
|
||
" ]\n",
|
||
" )\n",
|
||
" return result"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 14,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"top_n = pd.DataFrame(\n",
|
||
" top_k_recommendations(\n",
|
||
" model=model,\n",
|
||
" user_features=user_features,\n",
|
||
" item_features=item_features,\n",
|
||
" user_code_id=user_code_id,\n",
|
||
" item_code_id=item_code_id,\n",
|
||
" topK=10,\n",
|
||
" )\n",
|
||
")\n",
|
||
"top_n.to_csv(\n",
|
||
" \"Recommendations generated/ml-100k/Ready_LightFM_reco.csv\",\n",
|
||
" index=False,\n",
|
||
" header=False,\n",
|
||
")\n",
|
||
"\n",
|
||
"estimations = pd.DataFrame(\n",
|
||
" estimate(\n",
|
||
" model=model,\n",
|
||
" user_features=user_features,\n",
|
||
" item_features=item_features,\n",
|
||
" user_code_id=user_code_id,\n",
|
||
" item_code_id=item_code_id,\n",
|
||
" test_ui=test_ui,\n",
|
||
" )\n",
|
||
")\n",
|
||
"estimations.to_csv(\n",
|
||
" \"Recommendations generated/ml-100k/Ready_LightFM_estimations.csv\",\n",
|
||
" index=False,\n",
|
||
" header=False,\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"#### Pure MF with LightFM"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 15,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"item_features_interactions = sparse.csr_matrix(\n",
|
||
" item_features_df[\n",
|
||
" [\n",
|
||
" item_feature\n",
|
||
" for item_feature in item_features_df.columns\n",
|
||
" if \"id_\" in item_feature\n",
|
||
" ]\n",
|
||
" ].values\n",
|
||
")\n",
|
||
"user_features_interactions = sparse.csr_matrix(\n",
|
||
" user_features_df[\n",
|
||
" [\n",
|
||
" user_feature\n",
|
||
" for user_feature in user_features_df.columns\n",
|
||
" if \"id_\" in user_feature\n",
|
||
" ]\n",
|
||
" ].values\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 16,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Train precision: 0.62\n",
|
||
"Test precision: 0.34\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"from lightfm import LightFM\n",
|
||
"\n",
|
||
"model = LightFM(loss=\"warp\")\n",
|
||
"model.fit(\n",
|
||
" train_ui,\n",
|
||
" user_features=user_features_interactions,\n",
|
||
" item_features=item_features_interactions,\n",
|
||
" epochs=30,\n",
|
||
" num_threads=4,\n",
|
||
")\n",
|
||
"\n",
|
||
"from lightfm.evaluation import precision_at_k\n",
|
||
"\n",
|
||
"print(\n",
|
||
" \"Train precision: %.2f\"\n",
|
||
" % precision_at_k(model, test_interactions=train_ui, k=10).mean()\n",
|
||
")\n",
|
||
"print(\n",
|
||
" \"Test precision: %.2f\"\n",
|
||
" % precision_at_k(\n",
|
||
" model, test_interactions=test_ui, train_interactions=train_ui, k=10\n",
|
||
" ).mean()\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 17,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"top_n = pd.DataFrame(\n",
|
||
" top_k_recommendations(\n",
|
||
" model=model,\n",
|
||
" user_features=user_features_interactions,\n",
|
||
" item_features=item_features_interactions,\n",
|
||
" user_code_id=user_code_id,\n",
|
||
" item_code_id=item_code_id,\n",
|
||
" topK=10,\n",
|
||
" )\n",
|
||
")\n",
|
||
"top_n.to_csv(\n",
|
||
" \"Recommendations generated/ml-100k/Ready_LightFMpureMF_reco.csv\",\n",
|
||
" index=False,\n",
|
||
" header=False,\n",
|
||
")\n",
|
||
"\n",
|
||
"estimations = pd.DataFrame(\n",
|
||
" estimate(\n",
|
||
" model=model,\n",
|
||
" user_features=user_features_interactions,\n",
|
||
" item_features=item_features_interactions,\n",
|
||
" user_code_id=user_code_id,\n",
|
||
" item_code_id=item_code_id,\n",
|
||
" test_ui=test_ui,\n",
|
||
" )\n",
|
||
")\n",
|
||
"estimations.to_csv(\n",
|
||
" \"Recommendations generated/ml-100k/Ready_LightFMpureMF_estimations.csv\",\n",
|
||
" index=False,\n",
|
||
" header=False,\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"metadata": {},
|
||
"source": [
|
||
"#### LightFM with user/item attributes only (without treating id as a feature)"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 18,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"item_features_only = sparse.csr_matrix(\n",
|
||
" item_features_df[\n",
|
||
" [\n",
|
||
" item_feature\n",
|
||
" for item_feature in item_features_df.columns\n",
|
||
" if \"id_\" not in item_feature\n",
|
||
" ]\n",
|
||
" ].values\n",
|
||
")\n",
|
||
"user_features_only = sparse.csr_matrix(\n",
|
||
" user_features_df[\n",
|
||
" [\n",
|
||
" user_feature\n",
|
||
" for user_feature in user_features_df.columns\n",
|
||
" if \"id_\" not in user_feature\n",
|
||
" ]\n",
|
||
" ].values\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 19,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"Train precision: 0.39\n",
|
||
"Test precision: 0.16\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"from lightfm import LightFM\n",
|
||
"\n",
|
||
"model = LightFM(loss=\"warp\")\n",
|
||
"model.fit(\n",
|
||
" train_ui,\n",
|
||
" user_features=user_features_only,\n",
|
||
" item_features=item_features_only,\n",
|
||
" epochs=30,\n",
|
||
" num_threads=4,\n",
|
||
")\n",
|
||
"\n",
|
||
"from lightfm.evaluation import precision_at_k\n",
|
||
"\n",
|
||
"print(\n",
|
||
" \"Train precision: %.2f\"\n",
|
||
" % precision_at_k(\n",
|
||
" model,\n",
|
||
" test_interactions=train_ui,\n",
|
||
" user_features=user_features_only,\n",
|
||
" item_features=item_features_only,\n",
|
||
" k=10,\n",
|
||
" ).mean()\n",
|
||
")\n",
|
||
"print(\n",
|
||
" \"Test precision: %.2f\"\n",
|
||
" % precision_at_k(\n",
|
||
" model,\n",
|
||
" test_interactions=test_ui,\n",
|
||
" train_interactions=train_ui,\n",
|
||
" user_features=user_features_only,\n",
|
||
" item_features=item_features_only,\n",
|
||
" k=10,\n",
|
||
" ).mean()\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 20,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": [
|
||
"top_n = pd.DataFrame(\n",
|
||
" top_k_recommendations(\n",
|
||
" model=model,\n",
|
||
" user_features=user_features_only,\n",
|
||
" item_features=item_features_only,\n",
|
||
" user_code_id=user_code_id,\n",
|
||
" item_code_id=item_code_id,\n",
|
||
" topK=10,\n",
|
||
" )\n",
|
||
")\n",
|
||
"top_n.to_csv(\n",
|
||
" \"Recommendations generated/ml-100k/Ready_LightFMcontent_reco.csv\",\n",
|
||
" index=False,\n",
|
||
" header=False,\n",
|
||
")\n",
|
||
"\n",
|
||
"estimations = pd.DataFrame(\n",
|
||
" estimate(\n",
|
||
" model=model,\n",
|
||
" user_features=user_features_only,\n",
|
||
" item_features=item_features_only,\n",
|
||
" user_code_id=user_code_id,\n",
|
||
" item_code_id=item_code_id,\n",
|
||
" test_ui=test_ui,\n",
|
||
" )\n",
|
||
")\n",
|
||
"estimations.to_csv(\n",
|
||
" \"Recommendations generated/ml-100k/Ready_LightFMcontent_estimations.csv\",\n",
|
||
" index=False,\n",
|
||
" header=False,\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 21,
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stderr",
|
||
"output_type": "stream",
|
||
"text": [
|
||
"943it [00:00, 11300.75it/s]\n",
|
||
"943it [00:00, 11070.14it/s]\n",
|
||
"943it [00:00, 11045.26it/s]\n",
|
||
"943it [00:00, 11373.51it/s]\n",
|
||
"943it [00:00, 10314.45it/s]\n",
|
||
"943it [00:00, 11760.03it/s]\n",
|
||
"943it [00:00, 11634.63it/s]\n",
|
||
"943it [00:00, 11158.87it/s]\n",
|
||
"943it [00:00, 12014.16it/s]\n",
|
||
"943it [00:00, 11089.66it/s]\n",
|
||
"943it [00:00, 10880.72it/s]\n",
|
||
"943it [00:00, 11381.59it/s]\n",
|
||
"943it [00:00, 10462.79it/s]\n",
|
||
"943it [00:00, 11886.63it/s]\n",
|
||
"943it [00:00, 11710.11it/s]\n",
|
||
"943it [00:00, 12056.35it/s]\n",
|
||
"943it [00:00, 10062.43it/s]\n",
|
||
"943it [00:00, 10174.38it/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>Model</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",
|
||
" </tr>\n",
|
||
" </thead>\n",
|
||
" <tbody>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_LightFM</td>\n",
|
||
" <td>164.986935</td>\n",
|
||
" <td>163.074324</td>\n",
|
||
" <td>0.347508</td>\n",
|
||
" <td>0.222821</td>\n",
|
||
" <td>0.222253</td>\n",
|
||
" <td>0.262861</td>\n",
|
||
" <td>0.244957</td>\n",
|
||
" <td>0.266155</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_LightFMpureMF</td>\n",
|
||
" <td>7.984518</td>\n",
|
||
" <td>7.487804</td>\n",
|
||
" <td>0.335949</td>\n",
|
||
" <td>0.215474</td>\n",
|
||
" <td>0.216350</td>\n",
|
||
" <td>0.255187</td>\n",
|
||
" <td>0.235622</td>\n",
|
||
" <td>0.259289</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_P3</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",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_ImplicitALS</td>\n",
|
||
" <td>3.269156</td>\n",
|
||
" <td>3.070003</td>\n",
|
||
" <td>0.257582</td>\n",
|
||
" <td>0.186640</td>\n",
|
||
" <td>0.178445</td>\n",
|
||
" <td>0.202974</td>\n",
|
||
" <td>0.171137</td>\n",
|
||
" <td>0.216258</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_TopPop</td>\n",
|
||
" <td>2.508258</td>\n",
|
||
" <td>2.217909</td>\n",
|
||
" <td>0.188865</td>\n",
|
||
" <td>0.116919</td>\n",
|
||
" <td>0.118732</td>\n",
|
||
" <td>0.141584</td>\n",
|
||
" <td>0.130472</td>\n",
|
||
" <td>0.137473</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_LightFMcontent</td>\n",
|
||
" <td>184.450812</td>\n",
|
||
" <td>182.327275</td>\n",
|
||
" <td>0.161612</td>\n",
|
||
" <td>0.101836</td>\n",
|
||
" <td>0.102829</td>\n",
|
||
" <td>0.121845</td>\n",
|
||
" <td>0.102039</td>\n",
|
||
" <td>0.110954</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_SVD</td>\n",
|
||
" <td>0.951652</td>\n",
|
||
" <td>0.750975</td>\n",
|
||
" <td>0.096394</td>\n",
|
||
" <td>0.047252</td>\n",
|
||
" <td>0.052870</td>\n",
|
||
" <td>0.067257</td>\n",
|
||
" <td>0.085515</td>\n",
|
||
" <td>0.074754</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_SVD</td>\n",
|
||
" <td>0.914393</td>\n",
|
||
" <td>0.717199</td>\n",
|
||
" <td>0.101697</td>\n",
|
||
" <td>0.042334</td>\n",
|
||
" <td>0.051787</td>\n",
|
||
" <td>0.068811</td>\n",
|
||
" <td>0.092489</td>\n",
|
||
" <td>0.072360</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_Baseline</td>\n",
|
||
" <td>0.949459</td>\n",
|
||
" <td>0.752487</td>\n",
|
||
" <td>0.091410</td>\n",
|
||
" <td>0.037652</td>\n",
|
||
" <td>0.046030</td>\n",
|
||
" <td>0.061286</td>\n",
|
||
" <td>0.079614</td>\n",
|
||
" <td>0.056463</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_SVDBiased</td>\n",
|
||
" <td>0.940413</td>\n",
|
||
" <td>0.739571</td>\n",
|
||
" <td>0.086002</td>\n",
|
||
" <td>0.035478</td>\n",
|
||
" <td>0.043196</td>\n",
|
||
" <td>0.057507</td>\n",
|
||
" <td>0.075751</td>\n",
|
||
" <td>0.053460</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_Random</td>\n",
|
||
" <td>1.527935</td>\n",
|
||
" <td>1.225393</td>\n",
|
||
" <td>0.049311</td>\n",
|
||
" <td>0.020479</td>\n",
|
||
" <td>0.024944</td>\n",
|
||
" <td>0.032990</td>\n",
|
||
" <td>0.032189</td>\n",
|
||
" <td>0.024725</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_I-KNN</td>\n",
|
||
" <td>1.030386</td>\n",
|
||
" <td>0.813067</td>\n",
|
||
" <td>0.026087</td>\n",
|
||
" <td>0.006908</td>\n",
|
||
" <td>0.010593</td>\n",
|
||
" <td>0.016046</td>\n",
|
||
" <td>0.021137</td>\n",
|
||
" <td>0.009522</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_I-KNNBaseline</td>\n",
|
||
" <td>0.935327</td>\n",
|
||
" <td>0.737424</td>\n",
|
||
" <td>0.002545</td>\n",
|
||
" <td>0.000755</td>\n",
|
||
" <td>0.001105</td>\n",
|
||
" <td>0.001602</td>\n",
|
||
" <td>0.002253</td>\n",
|
||
" <td>0.000930</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_U-KNN</td>\n",
|
||
" <td>1.023495</td>\n",
|
||
" <td>0.807913</td>\n",
|
||
" <td>0.000742</td>\n",
|
||
" <td>0.000205</td>\n",
|
||
" <td>0.000305</td>\n",
|
||
" <td>0.000449</td>\n",
|
||
" <td>0.000536</td>\n",
|
||
" <td>0.000198</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_TopRated</td>\n",
|
||
" <td>1.030712</td>\n",
|
||
" <td>0.820904</td>\n",
|
||
" <td>0.000954</td>\n",
|
||
" <td>0.000188</td>\n",
|
||
" <td>0.000298</td>\n",
|
||
" <td>0.000481</td>\n",
|
||
" <td>0.000644</td>\n",
|
||
" <td>0.000223</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_BaselineIU</td>\n",
|
||
" <td>0.958136</td>\n",
|
||
" <td>0.754051</td>\n",
|
||
" <td>0.000954</td>\n",
|
||
" <td>0.000188</td>\n",
|
||
" <td>0.000298</td>\n",
|
||
" <td>0.000481</td>\n",
|
||
" <td>0.000644</td>\n",
|
||
" <td>0.000223</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_BaselineUI</td>\n",
|
||
" <td>0.967585</td>\n",
|
||
" <td>0.762740</td>\n",
|
||
" <td>0.000954</td>\n",
|
||
" <td>0.000170</td>\n",
|
||
" <td>0.000278</td>\n",
|
||
" <td>0.000463</td>\n",
|
||
" <td>0.000644</td>\n",
|
||
" <td>0.000189</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_IKNN</td>\n",
|
||
" <td>1.018363</td>\n",
|
||
" <td>0.808793</td>\n",
|
||
" <td>0.000318</td>\n",
|
||
" <td>0.000108</td>\n",
|
||
" <td>0.000140</td>\n",
|
||
" <td>0.000189</td>\n",
|
||
" <td>0.000000</td>\n",
|
||
" <td>0.000000</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" Model RMSE MAE precision recall \\\n",
|
||
"0 Ready_LightFM 164.986935 163.074324 0.347508 0.222821 \n",
|
||
"0 Ready_LightFMpureMF 7.984518 7.487804 0.335949 0.215474 \n",
|
||
"0 Self_P3 3.702446 3.527273 0.282185 0.192092 \n",
|
||
"0 Ready_ImplicitALS 3.269156 3.070003 0.257582 0.186640 \n",
|
||
"0 Self_TopPop 2.508258 2.217909 0.188865 0.116919 \n",
|
||
"0 Ready_LightFMcontent 184.450812 182.327275 0.161612 0.101836 \n",
|
||
"0 Ready_SVD 0.951652 0.750975 0.096394 0.047252 \n",
|
||
"0 Self_SVD 0.914393 0.717199 0.101697 0.042334 \n",
|
||
"0 Ready_Baseline 0.949459 0.752487 0.091410 0.037652 \n",
|
||
"0 Ready_SVDBiased 0.940413 0.739571 0.086002 0.035478 \n",
|
||
"0 Ready_Random 1.527935 1.225393 0.049311 0.020479 \n",
|
||
"0 Ready_I-KNN 1.030386 0.813067 0.026087 0.006908 \n",
|
||
"0 Ready_I-KNNBaseline 0.935327 0.737424 0.002545 0.000755 \n",
|
||
"0 Ready_U-KNN 1.023495 0.807913 0.000742 0.000205 \n",
|
||
"0 Self_TopRated 1.030712 0.820904 0.000954 0.000188 \n",
|
||
"0 Self_BaselineIU 0.958136 0.754051 0.000954 0.000188 \n",
|
||
"0 Self_BaselineUI 0.967585 0.762740 0.000954 0.000170 \n",
|
||
"0 Self_IKNN 1.018363 0.808793 0.000318 0.000108 \n",
|
||
"\n",
|
||
" F_1 F_05 precision_super recall_super \n",
|
||
"0 0.222253 0.262861 0.244957 0.266155 \n",
|
||
"0 0.216350 0.255187 0.235622 0.259289 \n",
|
||
"0 0.186749 0.216980 0.204185 0.240096 \n",
|
||
"0 0.178445 0.202974 0.171137 0.216258 \n",
|
||
"0 0.118732 0.141584 0.130472 0.137473 \n",
|
||
"0 0.102829 0.121845 0.102039 0.110954 \n",
|
||
"0 0.052870 0.067257 0.085515 0.074754 \n",
|
||
"0 0.051787 0.068811 0.092489 0.072360 \n",
|
||
"0 0.046030 0.061286 0.079614 0.056463 \n",
|
||
"0 0.043196 0.057507 0.075751 0.053460 \n",
|
||
"0 0.024944 0.032990 0.032189 0.024725 \n",
|
||
"0 0.010593 0.016046 0.021137 0.009522 \n",
|
||
"0 0.001105 0.001602 0.002253 0.000930 \n",
|
||
"0 0.000305 0.000449 0.000536 0.000198 \n",
|
||
"0 0.000298 0.000481 0.000644 0.000223 \n",
|
||
"0 0.000298 0.000481 0.000644 0.000223 \n",
|
||
"0 0.000278 0.000463 0.000644 0.000189 \n",
|
||
"0 0.000140 0.000189 0.000000 0.000000 "
|
||
]
|
||
},
|
||
"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>Model</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>Ready_LightFM</td>\n",
|
||
" <td>0.412873</td>\n",
|
||
" <td>0.276177</td>\n",
|
||
" <td>0.648569</td>\n",
|
||
" <td>0.609166</td>\n",
|
||
" <td>0.907741</td>\n",
|
||
" <td>1.000000</td>\n",
|
||
" <td>0.360029</td>\n",
|
||
" <td>5.364983</td>\n",
|
||
" <td>0.884435</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_LightFMpureMF</td>\n",
|
||
" <td>0.397751</td>\n",
|
||
" <td>0.261900</td>\n",
|
||
" <td>0.633698</td>\n",
|
||
" <td>0.605444</td>\n",
|
||
" <td>0.900318</td>\n",
|
||
" <td>1.000000</td>\n",
|
||
" <td>0.279221</td>\n",
|
||
" <td>5.086905</td>\n",
|
||
" <td>0.913551</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_P3</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>Ready_ImplicitALS</td>\n",
|
||
" <td>0.308415</td>\n",
|
||
" <td>0.175796</td>\n",
|
||
" <td>0.532835</td>\n",
|
||
" <td>0.590709</td>\n",
|
||
" <td>0.878049</td>\n",
|
||
" <td>0.999788</td>\n",
|
||
" <td>0.504329</td>\n",
|
||
" <td>5.761941</td>\n",
|
||
" <td>0.820874</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_TopPop</td>\n",
|
||
" <td>0.214651</td>\n",
|
||
" <td>0.111707</td>\n",
|
||
" <td>0.400939</td>\n",
|
||
" <td>0.555546</td>\n",
|
||
" <td>0.765642</td>\n",
|
||
" <td>1.000000</td>\n",
|
||
" <td>0.038961</td>\n",
|
||
" <td>3.159079</td>\n",
|
||
" <td>0.987317</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_LightFMcontent</td>\n",
|
||
" <td>0.179840</td>\n",
|
||
" <td>0.086900</td>\n",
|
||
" <td>0.334937</td>\n",
|
||
" <td>0.547874</td>\n",
|
||
" <td>0.720042</td>\n",
|
||
" <td>0.976352</td>\n",
|
||
" <td>0.251082</td>\n",
|
||
" <td>4.886664</td>\n",
|
||
" <td>0.928488</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_SVD</td>\n",
|
||
" <td>0.109578</td>\n",
|
||
" <td>0.051562</td>\n",
|
||
" <td>0.235567</td>\n",
|
||
" <td>0.520341</td>\n",
|
||
" <td>0.496288</td>\n",
|
||
" <td>0.995546</td>\n",
|
||
" <td>0.208514</td>\n",
|
||
" <td>4.455755</td>\n",
|
||
" <td>0.951624</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_SVD</td>\n",
|
||
" <td>0.104839</td>\n",
|
||
" <td>0.048970</td>\n",
|
||
" <td>0.196117</td>\n",
|
||
" <td>0.517889</td>\n",
|
||
" <td>0.480382</td>\n",
|
||
" <td>0.867338</td>\n",
|
||
" <td>0.147186</td>\n",
|
||
" <td>3.852545</td>\n",
|
||
" <td>0.972694</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_Baseline</td>\n",
|
||
" <td>0.095957</td>\n",
|
||
" <td>0.043178</td>\n",
|
||
" <td>0.198193</td>\n",
|
||
" <td>0.515501</td>\n",
|
||
" <td>0.437964</td>\n",
|
||
" <td>1.000000</td>\n",
|
||
" <td>0.033911</td>\n",
|
||
" <td>2.836513</td>\n",
|
||
" <td>0.991139</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_SVDBiased</td>\n",
|
||
" <td>0.094897</td>\n",
|
||
" <td>0.043361</td>\n",
|
||
" <td>0.209124</td>\n",
|
||
" <td>0.514405</td>\n",
|
||
" <td>0.428420</td>\n",
|
||
" <td>0.997349</td>\n",
|
||
" <td>0.177489</td>\n",
|
||
" <td>4.212509</td>\n",
|
||
" <td>0.962656</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_Random</td>\n",
|
||
" <td>0.053647</td>\n",
|
||
" <td>0.020462</td>\n",
|
||
" <td>0.136036</td>\n",
|
||
" <td>0.506763</td>\n",
|
||
" <td>0.339343</td>\n",
|
||
" <td>0.986108</td>\n",
|
||
" <td>0.191198</td>\n",
|
||
" <td>5.101215</td>\n",
|
||
" <td>0.907796</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_I-KNN</td>\n",
|
||
" <td>0.024214</td>\n",
|
||
" <td>0.008958</td>\n",
|
||
" <td>0.048068</td>\n",
|
||
" <td>0.499885</td>\n",
|
||
" <td>0.154825</td>\n",
|
||
" <td>0.402333</td>\n",
|
||
" <td>0.434343</td>\n",
|
||
" <td>5.133650</td>\n",
|
||
" <td>0.877999</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_I-KNNBaseline</td>\n",
|
||
" <td>0.003444</td>\n",
|
||
" <td>0.001362</td>\n",
|
||
" <td>0.011760</td>\n",
|
||
" <td>0.496724</td>\n",
|
||
" <td>0.021209</td>\n",
|
||
" <td>0.482821</td>\n",
|
||
" <td>0.059885</td>\n",
|
||
" <td>2.232578</td>\n",
|
||
" <td>0.994487</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Ready_U-KNN</td>\n",
|
||
" <td>0.000845</td>\n",
|
||
" <td>0.000274</td>\n",
|
||
" <td>0.002744</td>\n",
|
||
" <td>0.496441</td>\n",
|
||
" <td>0.007423</td>\n",
|
||
" <td>0.602121</td>\n",
|
||
" <td>0.010823</td>\n",
|
||
" <td>2.089186</td>\n",
|
||
" <td>0.995706</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_TopRated</td>\n",
|
||
" <td>0.001043</td>\n",
|
||
" <td>0.000335</td>\n",
|
||
" <td>0.003348</td>\n",
|
||
" <td>0.496433</td>\n",
|
||
" <td>0.009544</td>\n",
|
||
" <td>0.699046</td>\n",
|
||
" <td>0.005051</td>\n",
|
||
" <td>1.945910</td>\n",
|
||
" <td>0.995669</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_BaselineIU</td>\n",
|
||
" <td>0.001043</td>\n",
|
||
" <td>0.000335</td>\n",
|
||
" <td>0.003348</td>\n",
|
||
" <td>0.496433</td>\n",
|
||
" <td>0.009544</td>\n",
|
||
" <td>0.699046</td>\n",
|
||
" <td>0.005051</td>\n",
|
||
" <td>1.945910</td>\n",
|
||
" <td>0.995669</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_BaselineUI</td>\n",
|
||
" <td>0.000752</td>\n",
|
||
" <td>0.000168</td>\n",
|
||
" <td>0.001677</td>\n",
|
||
" <td>0.496424</td>\n",
|
||
" <td>0.009544</td>\n",
|
||
" <td>0.600530</td>\n",
|
||
" <td>0.005051</td>\n",
|
||
" <td>1.803126</td>\n",
|
||
" <td>0.996380</td>\n",
|
||
" </tr>\n",
|
||
" <tr>\n",
|
||
" <th>0</th>\n",
|
||
" <td>Self_IKNN</td>\n",
|
||
" <td>0.000214</td>\n",
|
||
" <td>0.000037</td>\n",
|
||
" <td>0.000368</td>\n",
|
||
" <td>0.496391</td>\n",
|
||
" <td>0.003181</td>\n",
|
||
" <td>0.392153</td>\n",
|
||
" <td>0.115440</td>\n",
|
||
" <td>4.174741</td>\n",
|
||
" <td>0.965327</td>\n",
|
||
" </tr>\n",
|
||
" </tbody>\n",
|
||
"</table>\n",
|
||
"</div>"
|
||
],
|
||
"text/plain": [
|
||
" Model NDCG mAP MRR LAUC HR \\\n",
|
||
"0 Ready_LightFM 0.412873 0.276177 0.648569 0.609166 0.907741 \n",
|
||
"0 Ready_LightFMpureMF 0.397751 0.261900 0.633698 0.605444 0.900318 \n",
|
||
"0 Self_P3 0.339114 0.204905 0.572157 0.593544 0.875928 \n",
|
||
"0 Ready_ImplicitALS 0.308415 0.175796 0.532835 0.590709 0.878049 \n",
|
||
"0 Self_TopPop 0.214651 0.111707 0.400939 0.555546 0.765642 \n",
|
||
"0 Ready_LightFMcontent 0.179840 0.086900 0.334937 0.547874 0.720042 \n",
|
||
"0 Ready_SVD 0.109578 0.051562 0.235567 0.520341 0.496288 \n",
|
||
"0 Self_SVD 0.104839 0.048970 0.196117 0.517889 0.480382 \n",
|
||
"0 Ready_Baseline 0.095957 0.043178 0.198193 0.515501 0.437964 \n",
|
||
"0 Ready_SVDBiased 0.094897 0.043361 0.209124 0.514405 0.428420 \n",
|
||
"0 Ready_Random 0.053647 0.020462 0.136036 0.506763 0.339343 \n",
|
||
"0 Ready_I-KNN 0.024214 0.008958 0.048068 0.499885 0.154825 \n",
|
||
"0 Ready_I-KNNBaseline 0.003444 0.001362 0.011760 0.496724 0.021209 \n",
|
||
"0 Ready_U-KNN 0.000845 0.000274 0.002744 0.496441 0.007423 \n",
|
||
"0 Self_TopRated 0.001043 0.000335 0.003348 0.496433 0.009544 \n",
|
||
"0 Self_BaselineIU 0.001043 0.000335 0.003348 0.496433 0.009544 \n",
|
||
"0 Self_BaselineUI 0.000752 0.000168 0.001677 0.496424 0.009544 \n",
|
||
"0 Self_IKNN 0.000214 0.000037 0.000368 0.496391 0.003181 \n",
|
||
"\n",
|
||
" Reco in test Test coverage Shannon Gini \n",
|
||
"0 1.000000 0.360029 5.364983 0.884435 \n",
|
||
"0 1.000000 0.279221 5.086905 0.913551 \n",
|
||
"0 1.000000 0.077201 3.875892 0.974947 \n",
|
||
"0 0.999788 0.504329 5.761941 0.820874 \n",
|
||
"0 1.000000 0.038961 3.159079 0.987317 \n",
|
||
"0 0.976352 0.251082 4.886664 0.928488 \n",
|
||
"0 0.995546 0.208514 4.455755 0.951624 \n",
|
||
"0 0.867338 0.147186 3.852545 0.972694 \n",
|
||
"0 1.000000 0.033911 2.836513 0.991139 \n",
|
||
"0 0.997349 0.177489 4.212509 0.962656 \n",
|
||
"0 0.986108 0.191198 5.101215 0.907796 \n",
|
||
"0 0.402333 0.434343 5.133650 0.877999 \n",
|
||
"0 0.482821 0.059885 2.232578 0.994487 \n",
|
||
"0 0.602121 0.010823 2.089186 0.995706 \n",
|
||
"0 0.699046 0.005051 1.945910 0.995669 \n",
|
||
"0 0.699046 0.005051 1.945910 0.995669 \n",
|
||
"0 0.600530 0.005051 1.803126 0.996380 \n",
|
||
"0 0.392153 0.115440 4.174741 0.965327 "
|
||
]
|
||
},
|
||
"metadata": {},
|
||
"output_type": "display_data"
|
||
}
|
||
],
|
||
"source": [
|
||
"import evaluation_measures as ev\n",
|
||
"\n",
|
||
"dir_path = \"Recommendations generated/ml-100k/\"\n",
|
||
"super_reactions = [4, 5]\n",
|
||
"test = pd.read_csv(\"./Datasets/ml-100k/test.csv\", sep=\"\\t\", header=None)\n",
|
||
"\n",
|
||
"df = ev.evaluate_all(test, dir_path, super_reactions)\n",
|
||
"display(df.iloc[:, :9])\n",
|
||
"display(df.iloc[:, np.append(0, np.arange(9, df.shape[1]))])"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": null,
|
||
"metadata": {},
|
||
"outputs": [],
|
||
"source": []
|
||
}
|
||
],
|
||
"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
|
||
}
|