{
"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": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 0 | \n",
" 1 | \n",
" 2 | \n",
" 3 | \n",
" 4 | \n",
" 5 | \n",
" 6 | \n",
" 7 | \n",
" 8 | \n",
" 9 | \n",
" ... | \n",
" 14 | \n",
" 15 | \n",
" 16 | \n",
" 17 | \n",
" 18 | \n",
" 19 | \n",
" 20 | \n",
" 21 | \n",
" 22 | \n",
" 23 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" Toy Story (1995) | \n",
" 01-Jan-1995 | \n",
" NaN | \n",
" http://us.imdb.com/M/title-exact?Toy%20Story%2... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 1 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 1 | \n",
" 2 | \n",
" GoldenEye (1995) | \n",
" 01-Jan-1995 | \n",
" NaN | \n",
" http://us.imdb.com/M/title-exact?GoldenEye%20(... | \n",
" 0 | \n",
" 1 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 2 | \n",
" 3 | \n",
" Four Rooms (1995) | \n",
" 01-Jan-1995 | \n",
" NaN | \n",
" http://us.imdb.com/M/title-exact?Four%20Rooms%... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
"
\n",
"
3 rows × 24 columns
\n",
"
"
],
"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": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" id_1 | \n",
" id_2 | \n",
" id_3 | \n",
" id_4 | \n",
" id_5 | \n",
" id_6 | \n",
" id_7 | \n",
" id_8 | \n",
" id_9 | \n",
" id_10 | \n",
" ... | \n",
" date_30-Mar-1996 | \n",
" date_30-May-1997 | \n",
" date_30-Nov-1996 | \n",
" date_30-Oct-1995 | \n",
" date_30-Oct-1996 | \n",
" date_31-Dec-1997 | \n",
" date_31-Jan-1997 | \n",
" date_31-Jul-1996 | \n",
" date_31-May-1996 | \n",
" date_4-Feb-1971 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 1 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 2 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
"
\n",
"
3 rows × 1922 columns
\n",
"
"
],
"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": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 0 | \n",
" 1 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" unknown | \n",
" 0 | \n",
"
\n",
" \n",
" 1 | \n",
" Action | \n",
" 1 | \n",
"
\n",
" \n",
" 2 | \n",
" Adventure | \n",
" 2 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"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": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" id_1 | \n",
" id_2 | \n",
" id_3 | \n",
" id_4 | \n",
" id_5 | \n",
" id_6 | \n",
" id_7 | \n",
" id_8 | \n",
" id_9 | \n",
" id_10 | \n",
" ... | \n",
" Fantasy | \n",
" Film-Noir | \n",
" Horror | \n",
" Musical | \n",
" Mystery | \n",
" Romance | \n",
" Sci-Fi | \n",
" Thriller | \n",
" War | \n",
" Western | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 1 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 2 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 3 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 4 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
" ... | \n",
"
\n",
" \n",
" 1677 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 1678 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 1679 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 1680 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 1681 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
"
\n",
"
1682 rows × 1941 columns
\n",
"
"
],
"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 ''\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": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" 0 | \n",
" 1 | \n",
" 2 | \n",
" 3 | \n",
" 4 | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" 24 | \n",
" M | \n",
" technician | \n",
" 85711 | \n",
"
\n",
" \n",
" 1 | \n",
" 2 | \n",
" 53 | \n",
" F | \n",
" other | \n",
" 94043 | \n",
"
\n",
" \n",
" 2 | \n",
" 3 | \n",
" 23 | \n",
" M | \n",
" writer | \n",
" 32067 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"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": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" id_1 | \n",
" id_2 | \n",
" id_3 | \n",
" id_4 | \n",
" id_5 | \n",
" id_6 | \n",
" id_7 | \n",
" id_8 | \n",
" id_9 | \n",
" id_10 | \n",
" ... | \n",
" Fantasy | \n",
" Film-Noir | \n",
" Horror | \n",
" Musical | \n",
" Mystery | \n",
" Romance | \n",
" Sci-Fi | \n",
" Thriller | \n",
" War | \n",
" Western | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 1 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
" 2 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" ... | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 0 | \n",
" 1 | \n",
" 0 | \n",
" 0 | \n",
"
\n",
" \n",
"
\n",
"
3 rows × 1941 columns
\n",
"
"
],
"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 ''\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 ''\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": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Model | \n",
" RMSE | \n",
" MAE | \n",
" precision | \n",
" recall | \n",
" F_1 | \n",
" F_05 | \n",
" precision_super | \n",
" recall_super | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Ready_LightFM | \n",
" 164.986935 | \n",
" 163.074324 | \n",
" 0.347508 | \n",
" 0.222821 | \n",
" 0.222253 | \n",
" 0.262861 | \n",
" 0.244957 | \n",
" 0.266155 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_LightFMpureMF | \n",
" 7.984518 | \n",
" 7.487804 | \n",
" 0.335949 | \n",
" 0.215474 | \n",
" 0.216350 | \n",
" 0.255187 | \n",
" 0.235622 | \n",
" 0.259289 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_P3 | \n",
" 3.702446 | \n",
" 3.527273 | \n",
" 0.282185 | \n",
" 0.192092 | \n",
" 0.186749 | \n",
" 0.216980 | \n",
" 0.204185 | \n",
" 0.240096 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_ImplicitALS | \n",
" 3.269156 | \n",
" 3.070003 | \n",
" 0.257582 | \n",
" 0.186640 | \n",
" 0.178445 | \n",
" 0.202974 | \n",
" 0.171137 | \n",
" 0.216258 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_TopPop | \n",
" 2.508258 | \n",
" 2.217909 | \n",
" 0.188865 | \n",
" 0.116919 | \n",
" 0.118732 | \n",
" 0.141584 | \n",
" 0.130472 | \n",
" 0.137473 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_LightFMcontent | \n",
" 184.450812 | \n",
" 182.327275 | \n",
" 0.161612 | \n",
" 0.101836 | \n",
" 0.102829 | \n",
" 0.121845 | \n",
" 0.102039 | \n",
" 0.110954 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_SVD | \n",
" 0.951652 | \n",
" 0.750975 | \n",
" 0.096394 | \n",
" 0.047252 | \n",
" 0.052870 | \n",
" 0.067257 | \n",
" 0.085515 | \n",
" 0.074754 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_SVD | \n",
" 0.914393 | \n",
" 0.717199 | \n",
" 0.101697 | \n",
" 0.042334 | \n",
" 0.051787 | \n",
" 0.068811 | \n",
" 0.092489 | \n",
" 0.072360 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_Baseline | \n",
" 0.949459 | \n",
" 0.752487 | \n",
" 0.091410 | \n",
" 0.037652 | \n",
" 0.046030 | \n",
" 0.061286 | \n",
" 0.079614 | \n",
" 0.056463 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_SVDBiased | \n",
" 0.940413 | \n",
" 0.739571 | \n",
" 0.086002 | \n",
" 0.035478 | \n",
" 0.043196 | \n",
" 0.057507 | \n",
" 0.075751 | \n",
" 0.053460 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_Random | \n",
" 1.527935 | \n",
" 1.225393 | \n",
" 0.049311 | \n",
" 0.020479 | \n",
" 0.024944 | \n",
" 0.032990 | \n",
" 0.032189 | \n",
" 0.024725 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_I-KNN | \n",
" 1.030386 | \n",
" 0.813067 | \n",
" 0.026087 | \n",
" 0.006908 | \n",
" 0.010593 | \n",
" 0.016046 | \n",
" 0.021137 | \n",
" 0.009522 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_I-KNNBaseline | \n",
" 0.935327 | \n",
" 0.737424 | \n",
" 0.002545 | \n",
" 0.000755 | \n",
" 0.001105 | \n",
" 0.001602 | \n",
" 0.002253 | \n",
" 0.000930 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_U-KNN | \n",
" 1.023495 | \n",
" 0.807913 | \n",
" 0.000742 | \n",
" 0.000205 | \n",
" 0.000305 | \n",
" 0.000449 | \n",
" 0.000536 | \n",
" 0.000198 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_TopRated | \n",
" 1.030712 | \n",
" 0.820904 | \n",
" 0.000954 | \n",
" 0.000188 | \n",
" 0.000298 | \n",
" 0.000481 | \n",
" 0.000644 | \n",
" 0.000223 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_BaselineIU | \n",
" 0.958136 | \n",
" 0.754051 | \n",
" 0.000954 | \n",
" 0.000188 | \n",
" 0.000298 | \n",
" 0.000481 | \n",
" 0.000644 | \n",
" 0.000223 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_BaselineUI | \n",
" 0.967585 | \n",
" 0.762740 | \n",
" 0.000954 | \n",
" 0.000170 | \n",
" 0.000278 | \n",
" 0.000463 | \n",
" 0.000644 | \n",
" 0.000189 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_IKNN | \n",
" 1.018363 | \n",
" 0.808793 | \n",
" 0.000318 | \n",
" 0.000108 | \n",
" 0.000140 | \n",
" 0.000189 | \n",
" 0.000000 | \n",
" 0.000000 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"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": [
"\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" Model | \n",
" NDCG | \n",
" mAP | \n",
" MRR | \n",
" LAUC | \n",
" HR | \n",
" Reco in test | \n",
" Test coverage | \n",
" Shannon | \n",
" Gini | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" Ready_LightFM | \n",
" 0.412873 | \n",
" 0.276177 | \n",
" 0.648569 | \n",
" 0.609166 | \n",
" 0.907741 | \n",
" 1.000000 | \n",
" 0.360029 | \n",
" 5.364983 | \n",
" 0.884435 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_LightFMpureMF | \n",
" 0.397751 | \n",
" 0.261900 | \n",
" 0.633698 | \n",
" 0.605444 | \n",
" 0.900318 | \n",
" 1.000000 | \n",
" 0.279221 | \n",
" 5.086905 | \n",
" 0.913551 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_P3 | \n",
" 0.339114 | \n",
" 0.204905 | \n",
" 0.572157 | \n",
" 0.593544 | \n",
" 0.875928 | \n",
" 1.000000 | \n",
" 0.077201 | \n",
" 3.875892 | \n",
" 0.974947 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_ImplicitALS | \n",
" 0.308415 | \n",
" 0.175796 | \n",
" 0.532835 | \n",
" 0.590709 | \n",
" 0.878049 | \n",
" 0.999788 | \n",
" 0.504329 | \n",
" 5.761941 | \n",
" 0.820874 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_TopPop | \n",
" 0.214651 | \n",
" 0.111707 | \n",
" 0.400939 | \n",
" 0.555546 | \n",
" 0.765642 | \n",
" 1.000000 | \n",
" 0.038961 | \n",
" 3.159079 | \n",
" 0.987317 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_LightFMcontent | \n",
" 0.179840 | \n",
" 0.086900 | \n",
" 0.334937 | \n",
" 0.547874 | \n",
" 0.720042 | \n",
" 0.976352 | \n",
" 0.251082 | \n",
" 4.886664 | \n",
" 0.928488 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_SVD | \n",
" 0.109578 | \n",
" 0.051562 | \n",
" 0.235567 | \n",
" 0.520341 | \n",
" 0.496288 | \n",
" 0.995546 | \n",
" 0.208514 | \n",
" 4.455755 | \n",
" 0.951624 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_SVD | \n",
" 0.104839 | \n",
" 0.048970 | \n",
" 0.196117 | \n",
" 0.517889 | \n",
" 0.480382 | \n",
" 0.867338 | \n",
" 0.147186 | \n",
" 3.852545 | \n",
" 0.972694 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_Baseline | \n",
" 0.095957 | \n",
" 0.043178 | \n",
" 0.198193 | \n",
" 0.515501 | \n",
" 0.437964 | \n",
" 1.000000 | \n",
" 0.033911 | \n",
" 2.836513 | \n",
" 0.991139 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_SVDBiased | \n",
" 0.094897 | \n",
" 0.043361 | \n",
" 0.209124 | \n",
" 0.514405 | \n",
" 0.428420 | \n",
" 0.997349 | \n",
" 0.177489 | \n",
" 4.212509 | \n",
" 0.962656 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_Random | \n",
" 0.053647 | \n",
" 0.020462 | \n",
" 0.136036 | \n",
" 0.506763 | \n",
" 0.339343 | \n",
" 0.986108 | \n",
" 0.191198 | \n",
" 5.101215 | \n",
" 0.907796 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_I-KNN | \n",
" 0.024214 | \n",
" 0.008958 | \n",
" 0.048068 | \n",
" 0.499885 | \n",
" 0.154825 | \n",
" 0.402333 | \n",
" 0.434343 | \n",
" 5.133650 | \n",
" 0.877999 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_I-KNNBaseline | \n",
" 0.003444 | \n",
" 0.001362 | \n",
" 0.011760 | \n",
" 0.496724 | \n",
" 0.021209 | \n",
" 0.482821 | \n",
" 0.059885 | \n",
" 2.232578 | \n",
" 0.994487 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_U-KNN | \n",
" 0.000845 | \n",
" 0.000274 | \n",
" 0.002744 | \n",
" 0.496441 | \n",
" 0.007423 | \n",
" 0.602121 | \n",
" 0.010823 | \n",
" 2.089186 | \n",
" 0.995706 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_TopRated | \n",
" 0.001043 | \n",
" 0.000335 | \n",
" 0.003348 | \n",
" 0.496433 | \n",
" 0.009544 | \n",
" 0.699046 | \n",
" 0.005051 | \n",
" 1.945910 | \n",
" 0.995669 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_BaselineIU | \n",
" 0.001043 | \n",
" 0.000335 | \n",
" 0.003348 | \n",
" 0.496433 | \n",
" 0.009544 | \n",
" 0.699046 | \n",
" 0.005051 | \n",
" 1.945910 | \n",
" 0.995669 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_BaselineUI | \n",
" 0.000752 | \n",
" 0.000168 | \n",
" 0.001677 | \n",
" 0.496424 | \n",
" 0.009544 | \n",
" 0.600530 | \n",
" 0.005051 | \n",
" 1.803126 | \n",
" 0.996380 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_IKNN | \n",
" 0.000214 | \n",
" 0.000037 | \n",
" 0.000368 | \n",
" 0.496391 | \n",
" 0.003181 | \n",
" 0.392153 | \n",
" 0.115440 | \n",
" 4.174741 | \n",
" 0.965327 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"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
}