{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Self made simplified I-KNN"
]
},
{
"cell_type": "code",
"execution_count": 18,
"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",
"\n",
"train_read = pd.read_csv(\"./Datasets/ml-100k/train.csv\", sep=\"\\t\", header=None)\n",
"test_read = pd.read_csv(\"./Datasets/ml-100k/test.csv\", sep=\"\\t\", header=None)\n",
"(\n",
" train_ui,\n",
" test_ui,\n",
" user_code_id,\n",
" user_id_code,\n",
" item_code_id,\n",
" item_id_code,\n",
") = helpers.data_to_csr(train_read, test_read)"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"class IKNN:\n",
" def fit(self, train_ui):\n",
" self.train_ui = train_ui\n",
"\n",
" train_iu = train_ui.transpose()\n",
" norms = np.linalg.norm(\n",
" train_iu.A, axis=1\n",
" ) # here we compute length of each item ratings vector\n",
" norms = np.vectorize(lambda x: max(x, 1))(\n",
" norms[:, None]\n",
" ) # to avoid dividing by zero\n",
"\n",
" normalized_train_iu = sparse.csr_matrix(train_iu / norms)\n",
"\n",
" self.similarity_matrix_ii = (\n",
" normalized_train_iu * normalized_train_iu.transpose()\n",
" )\n",
"\n",
" self.estimations = np.array(\n",
" train_ui\n",
" * self.similarity_matrix_ii\n",
" / ((train_ui > 0) * self.similarity_matrix_ii)\n",
" )\n",
"\n",
" def recommend(self, user_code_id, item_code_id, topK=10):\n",
"\n",
" top_k = defaultdict(list)\n",
" for nb_user, user in enumerate(self.estimations):\n",
"\n",
" user_rated = self.train_ui.indices[\n",
" self.train_ui.indptr[nb_user] : self.train_ui.indptr[nb_user + 1]\n",
" ]\n",
" for item, score in enumerate(user):\n",
" if item not in user_rated and not np.isnan(score):\n",
" top_k[user_code_id[nb_user]].append((item_code_id[item], score))\n",
" result = []\n",
" # Let's choose k best items in the format: (user, item1, score1, item2, score2, ...)\n",
" for uid, item_scores in top_k.items():\n",
" item_scores.sort(key=lambda x: x[1], reverse=True)\n",
" result.append([uid] + list(chain(*item_scores[:topK])))\n",
" return result\n",
"\n",
" def estimate(self, user_code_id, item_code_id, test_ui):\n",
" result = []\n",
" for user, item in zip(*test_ui.nonzero()):\n",
" result.append(\n",
" [\n",
" user_code_id[user],\n",
" item_code_id[item],\n",
" self.estimations[user, item]\n",
" if not np.isnan(self.estimations[user, item])\n",
" else 1,\n",
" ]\n",
" )\n",
" return result"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"toy train ui:\n"
]
},
{
"data": {
"text/plain": [
"array([[3, 4, 0, 0, 5, 0, 0, 4],\n",
" [0, 1, 2, 3, 0, 0, 0, 0],\n",
" [0, 0, 0, 5, 0, 3, 4, 0]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"similarity matrix:\n"
]
},
{
"data": {
"text/plain": [
"array([[1. , 0.9701425 , 0. , 0. , 1. ,\n",
" 0. , 0. , 1. ],\n",
" [0.9701425 , 1. , 0.24253563, 0.12478355, 0.9701425 ,\n",
" 0. , 0. , 0.9701425 ],\n",
" [0. , 0.24253563, 1. , 0.51449576, 0. ,\n",
" 0. , 0. , 0. ],\n",
" [0. , 0.12478355, 0.51449576, 1. , 0. ,\n",
" 0.85749293, 0.85749293, 0. ],\n",
" [1. , 0.9701425 , 0. , 0. , 1. ,\n",
" 0. , 0. , 1. ],\n",
" [0. , 0. , 0. , 0.85749293, 0. ,\n",
" 1. , 1. , 0. ],\n",
" [0. , 0. , 0. , 0.85749293, 0. ,\n",
" 1. , 1. , 0. ],\n",
" [1. , 0.9701425 , 0. , 0. , 1. ,\n",
" 0. , 0. , 1. ]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"estimations matrix:\n"
]
},
{
"data": {
"text/plain": [
"array([[4. , 4. , 4. , 4. , 4. ,\n",
" nan, nan, 4. ],\n",
" [1. , 1.35990333, 2.15478388, 2.53390319, 1. ,\n",
" 3. , 3. , 1. ],\n",
" [ nan, 5. , 5. , 4.05248907, nan,\n",
" 3.95012863, 3.95012863, nan]])"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"[[0, 20, 4.0, 30, 4.0],\n",
" [10, 50, 3.0, 60, 3.0, 0, 1.0, 40, 1.0, 70, 1.0],\n",
" [20, 10, 5.0, 20, 5.0]]"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# toy example\n",
"toy_train_read = pd.read_csv(\n",
" \"./Datasets/toy-example/train.csv\",\n",
" sep=\"\\t\",\n",
" header=None,\n",
" names=[\"user\", \"item\", \"rating\", \"timestamp\"],\n",
")\n",
"toy_test_read = pd.read_csv(\n",
" \"./Datasets/toy-example/test.csv\",\n",
" sep=\"\\t\",\n",
" header=None,\n",
" names=[\"user\", \"item\", \"rating\", \"timestamp\"],\n",
")\n",
"\n",
"(\n",
" toy_train_ui,\n",
" toy_test_ui,\n",
" toy_user_code_id,\n",
" toy_user_id_code,\n",
" toy_item_code_id,\n",
" toy_item_id_code,\n",
") = helpers.data_to_csr(toy_train_read, toy_test_read)\n",
"\n",
"\n",
"model = IKNN()\n",
"model.fit(toy_train_ui)\n",
"\n",
"print(\"toy train ui:\")\n",
"display(toy_train_ui.A)\n",
"\n",
"print(\"similarity matrix:\")\n",
"display(model.similarity_matrix_ii.A)\n",
"\n",
"print(\"estimations matrix:\")\n",
"display(model.estimations)\n",
"\n",
"model.recommend(toy_user_code_id, toy_item_code_id)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"model = IKNN()\n",
"model.fit(train_ui)\n",
"\n",
"top_n = pd.DataFrame(model.recommend(user_code_id, item_code_id, topK=10))\n",
"\n",
"top_n.to_csv(\n",
" \"Recommendations generated/ml-100k/Self_IKNN_reco.csv\", index=False, header=False\n",
")\n",
"\n",
"estimations = pd.DataFrame(model.estimate(user_code_id, item_code_id, test_ui))\n",
"estimations.to_csv(\n",
" \"Recommendations generated/ml-100k/Self_IKNN_estimations.csv\",\n",
" index=False,\n",
" header=False,\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"943it [00:00, 8576.73it/s]\n"
]
},
{
"data": {
"text/html": [
"
\n",
"\n",
"
\n",
" \n",
" \n",
" | \n",
" RMSE | \n",
" MAE | \n",
" precision | \n",
" recall | \n",
" F_1 | \n",
" F_05 | \n",
" precision_super | \n",
" recall_super | \n",
" NDCG | \n",
" mAP | \n",
" MRR | \n",
" LAUC | \n",
" HR | \n",
" H2R | \n",
" Reco in test | \n",
" Test coverage | \n",
" Shannon | \n",
" Gini | \n",
"
\n",
" \n",
" \n",
" \n",
" 0 | \n",
" 1.018363 | \n",
" 0.808793 | \n",
" 0.000318 | \n",
" 0.000108 | \n",
" 0.00014 | \n",
" 0.000189 | \n",
" 0.0 | \n",
" 0.0 | \n",
" 0.000214 | \n",
" 0.000037 | \n",
" 0.000368 | \n",
" 0.496391 | \n",
" 0.003181 | \n",
" 0.0 | \n",
" 0.392153 | \n",
" 0.11544 | \n",
" 4.174741 | \n",
" 0.965327 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" RMSE MAE precision recall F_1 F_05 \\\n",
"0 1.018363 0.808793 0.000318 0.000108 0.00014 0.000189 \n",
"\n",
" precision_super recall_super NDCG mAP MRR LAUC \\\n",
"0 0.0 0.0 0.000214 0.000037 0.000368 0.496391 \n",
"\n",
" HR H2R Reco in test Test coverage Shannon Gini \n",
"0 0.003181 0.0 0.392153 0.11544 4.174741 0.965327 "
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import evaluation_measures as ev\n",
"\n",
"estimations_df = pd.read_csv(\n",
" \"Recommendations generated/ml-100k/Self_IKNN_estimations.csv\", header=None\n",
")\n",
"reco = np.loadtxt(\"Recommendations generated/ml-100k/Self_IKNN_reco.csv\", delimiter=\",\")\n",
"\n",
"ev.evaluate(\n",
" test=pd.read_csv(\"./Datasets/ml-100k/test.csv\", sep=\"\\t\", header=None),\n",
" estimations_df=estimations_df,\n",
" reco=reco,\n",
" super_reactions=[4, 5],\n",
")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"943it [00:00, 7330.28it/s]\n",
"943it [00:00, 7827.86it/s]\n",
"943it [00:00, 8071.29it/s]\n",
"943it [00:00, 8658.58it/s]\n",
"943it [00:00, 8305.62it/s]\n",
"943it [00:00, 8619.80it/s]\n",
"943it [00:00, 8313.50it/s]\n",
"943it [00:00, 8324.13it/s]\n",
"943it [00:00, 8154.03it/s]\n",
"943it [00:00, 7567.74it/s]\n",
"943it [00:00, 7721.03it/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",
" NDCG | \n",
" mAP | \n",
" MRR | \n",
" LAUC | \n",
" HR | \n",
" H2R | \n",
" Reco in test | \n",
" Test coverage | \n",
" Shannon | \n",
" Gini | \n",
"
\n",
" \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",
" 0.214651 | \n",
" 0.111707 | \n",
" 0.400939 | \n",
" 0.555546 | \n",
" 0.765642 | \n",
" 0.492047 | \n",
" 1.000000 | \n",
" 0.038961 | \n",
" 3.159079 | \n",
" 0.987317 | \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",
" 0.095957 | \n",
" 0.043178 | \n",
" 0.198193 | \n",
" 0.515501 | \n",
" 0.437964 | \n",
" 0.239661 | \n",
" 1.000000 | \n",
" 0.033911 | \n",
" 2.836513 | \n",
" 0.991139 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_KNNSurprisetask | \n",
" 0.946255 | \n",
" 0.745209 | \n",
" 0.083457 | \n",
" 0.032848 | \n",
" 0.041227 | \n",
" 0.055493 | \n",
" 0.074785 | \n",
" 0.048890 | \n",
" 0.089577 | \n",
" 0.040902 | \n",
" 0.189057 | \n",
" 0.513076 | \n",
" 0.417815 | \n",
" 0.217391 | \n",
" 0.888547 | \n",
" 0.130592 | \n",
" 3.611806 | \n",
" 0.978659 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_GlobalAvg | \n",
" 1.125760 | \n",
" 0.943534 | \n",
" 0.061188 | \n",
" 0.025968 | \n",
" 0.031383 | \n",
" 0.041343 | \n",
" 0.040558 | \n",
" 0.032107 | \n",
" 0.067695 | \n",
" 0.027470 | \n",
" 0.171187 | \n",
" 0.509546 | \n",
" 0.384942 | \n",
" 0.142100 | \n",
" 1.000000 | \n",
" 0.025974 | \n",
" 2.711772 | \n",
" 0.992003 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_Random | \n",
" 1.528005 | \n",
" 1.227604 | \n",
" 0.042842 | \n",
" 0.019173 | \n",
" 0.022234 | \n",
" 0.028926 | \n",
" 0.026502 | \n",
" 0.019532 | \n",
" 0.044202 | \n",
" 0.015665 | \n",
" 0.107737 | \n",
" 0.506081 | \n",
" 0.313892 | \n",
" 0.080594 | \n",
" 0.986108 | \n",
" 0.182540 | \n",
" 5.094973 | \n",
" 0.907749 | \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",
" 0.024214 | \n",
" 0.008958 | \n",
" 0.048068 | \n",
" 0.499885 | \n",
" 0.154825 | \n",
" 0.072110 | \n",
" 0.402333 | \n",
" 0.434343 | \n",
" 5.133650 | \n",
" 0.877999 | \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",
" 0.003444 | \n",
" 0.001362 | \n",
" 0.011760 | \n",
" 0.496724 | \n",
" 0.021209 | \n",
" 0.004242 | \n",
" 0.482821 | \n",
" 0.059885 | \n",
" 2.232578 | \n",
" 0.994487 | \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",
" 0.000845 | \n",
" 0.000274 | \n",
" 0.002744 | \n",
" 0.496441 | \n",
" 0.007423 | \n",
" 0.000000 | \n",
" 0.602121 | \n",
" 0.010823 | \n",
" 2.089186 | \n",
" 0.995706 | \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",
" 0.001043 | \n",
" 0.000335 | \n",
" 0.003348 | \n",
" 0.496433 | \n",
" 0.009544 | \n",
" 0.000000 | \n",
" 0.699046 | \n",
" 0.005051 | \n",
" 1.945910 | \n",
" 0.995669 | \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",
" 0.000752 | \n",
" 0.000168 | \n",
" 0.001677 | \n",
" 0.496424 | \n",
" 0.009544 | \n",
" 0.000000 | \n",
" 0.600530 | \n",
" 0.005051 | \n",
" 1.803126 | \n",
" 0.996380 | \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",
" 0.000214 | \n",
" 0.000037 | \n",
" 0.000368 | \n",
" 0.496391 | \n",
" 0.003181 | \n",
" 0.000000 | \n",
" 0.392153 | \n",
" 0.115440 | \n",
" 4.174741 | \n",
" 0.965327 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Model RMSE MAE precision recall F_1 \\\n",
"0 Self_TopPop 2.508258 2.217909 0.188865 0.116919 0.118732 \n",
"0 Ready_Baseline 0.949459 0.752487 0.091410 0.037652 0.046030 \n",
"0 Self_KNNSurprisetask 0.946255 0.745209 0.083457 0.032848 0.041227 \n",
"0 Self_GlobalAvg 1.125760 0.943534 0.061188 0.025968 0.031383 \n",
"0 Ready_Random 1.528005 1.227604 0.042842 0.019173 0.022234 \n",
"0 Ready_I-KNN 1.030386 0.813067 0.026087 0.006908 0.010593 \n",
"0 Ready_I-KNNBaseline 0.935327 0.737424 0.002545 0.000755 0.001105 \n",
"0 Ready_U-KNN 1.023495 0.807913 0.000742 0.000205 0.000305 \n",
"0 Self_TopRated 1.030712 0.820904 0.000954 0.000188 0.000298 \n",
"0 Self_BaselineUI 0.967585 0.762740 0.000954 0.000170 0.000278 \n",
"0 Self_IKNN 1.018363 0.808793 0.000318 0.000108 0.000140 \n",
"\n",
" F_05 precision_super recall_super NDCG mAP MRR \\\n",
"0 0.141584 0.130472 0.137473 0.214651 0.111707 0.400939 \n",
"0 0.061286 0.079614 0.056463 0.095957 0.043178 0.198193 \n",
"0 0.055493 0.074785 0.048890 0.089577 0.040902 0.189057 \n",
"0 0.041343 0.040558 0.032107 0.067695 0.027470 0.171187 \n",
"0 0.028926 0.026502 0.019532 0.044202 0.015665 0.107737 \n",
"0 0.016046 0.021137 0.009522 0.024214 0.008958 0.048068 \n",
"0 0.001602 0.002253 0.000930 0.003444 0.001362 0.011760 \n",
"0 0.000449 0.000536 0.000198 0.000845 0.000274 0.002744 \n",
"0 0.000481 0.000644 0.000223 0.001043 0.000335 0.003348 \n",
"0 0.000463 0.000644 0.000189 0.000752 0.000168 0.001677 \n",
"0 0.000189 0.000000 0.000000 0.000214 0.000037 0.000368 \n",
"\n",
" LAUC HR H2R Reco in test Test coverage Shannon \\\n",
"0 0.555546 0.765642 0.492047 1.000000 0.038961 3.159079 \n",
"0 0.515501 0.437964 0.239661 1.000000 0.033911 2.836513 \n",
"0 0.513076 0.417815 0.217391 0.888547 0.130592 3.611806 \n",
"0 0.509546 0.384942 0.142100 1.000000 0.025974 2.711772 \n",
"0 0.506081 0.313892 0.080594 0.986108 0.182540 5.094973 \n",
"0 0.499885 0.154825 0.072110 0.402333 0.434343 5.133650 \n",
"0 0.496724 0.021209 0.004242 0.482821 0.059885 2.232578 \n",
"0 0.496441 0.007423 0.000000 0.602121 0.010823 2.089186 \n",
"0 0.496433 0.009544 0.000000 0.699046 0.005051 1.945910 \n",
"0 0.496424 0.009544 0.000000 0.600530 0.005051 1.803126 \n",
"0 0.496391 0.003181 0.000000 0.392153 0.115440 4.174741 \n",
"\n",
" Gini \n",
"0 0.987317 \n",
"0 0.991139 \n",
"0 0.978659 \n",
"0 0.992003 \n",
"0 0.907749 \n",
"0 0.877999 \n",
"0 0.994487 \n",
"0 0.995706 \n",
"0 0.995669 \n",
"0 0.996380 \n",
"0 0.965327 "
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"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",
"ev.evaluate_all(test, dir_path, super_reactions)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Ready-made KNNs - Surprise implementation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### I-KNN - basic"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Computing the cosine similarity matrix...\n",
"Done computing similarity matrix.\n",
"Generating predictions...\n",
"Generating top N recommendations...\n",
"Generating predictions...\n"
]
}
],
"source": [
"import helpers\n",
"import surprise as sp\n",
"\n",
"sim_options = {\n",
" \"name\": \"cosine\",\n",
" \"user_based\": False,\n",
"} # compute similarities between items\n",
"algo = sp.KNNBasic(sim_options=sim_options)\n",
"\n",
"helpers.ready_made(\n",
" algo,\n",
" reco_path=\"Recommendations generated/ml-100k/Ready_I-KNN_reco.csv\",\n",
" estimations_path=\"Recommendations generated/ml-100k/Ready_I-KNN_estimations.csv\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### U-KNN - basic"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Computing the cosine similarity matrix...\n",
"Done computing similarity matrix.\n",
"Generating predictions...\n"
]
},
{
"ename": "KeyboardInterrupt",
"evalue": "",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mKeyboardInterrupt\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0malgo\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0mreco_path\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"Recommendations generated/ml-100k/Ready_U-KNN_reco.csv\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 10\u001b[0;31m \u001b[0mestimations_path\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m\"Recommendations generated/ml-100k/Ready_U-KNN_estimations.csv\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 11\u001b[0m )\n",
"\u001b[0;32m~/Desktop/helpers.py\u001b[0m in \u001b[0;36mready_made\u001b[0;34m(algo, reco_path, estimations_path)\u001b[0m\n\u001b[1;32m 64\u001b[0m \u001b[0mantitrainset\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mtrainset\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbuild_anti_testset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;31m# We want to predict ratings of pairs (user, item) which are not in train set\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 65\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Generating predictions...'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 66\u001b[0;31m \u001b[0mpredictions\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0malgo\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtest\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mantitrainset\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 67\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Generating top N recommendations...'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 68\u001b[0m \u001b[0mtop_n\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mget_top_n\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpredictions\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mn\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.local/lib/python3.7/site-packages/surprise/prediction_algorithms/algo_base.py\u001b[0m in \u001b[0;36mtest\u001b[0;34m(self, testset, verbose)\u001b[0m\n\u001b[1;32m 166\u001b[0m \u001b[0mr_ui_trans\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 167\u001b[0m verbose=verbose)\n\u001b[0;32m--> 168\u001b[0;31m for (uid, iid, r_ui_trans) in testset]\n\u001b[0m\u001b[1;32m 169\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mpredictions\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 170\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.local/lib/python3.7/site-packages/surprise/prediction_algorithms/algo_base.py\u001b[0m in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 166\u001b[0m \u001b[0mr_ui_trans\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 167\u001b[0m verbose=verbose)\n\u001b[0;32m--> 168\u001b[0;31m for (uid, iid, r_ui_trans) in testset]\n\u001b[0m\u001b[1;32m 169\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mpredictions\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 170\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.local/lib/python3.7/site-packages/surprise/prediction_algorithms/algo_base.py\u001b[0m in \u001b[0;36mpredict\u001b[0;34m(self, uid, iid, r_ui, clip, verbose)\u001b[0m\n\u001b[1;32m 104\u001b[0m \u001b[0mdetails\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 105\u001b[0m \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 106\u001b[0;31m \u001b[0mest\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mestimate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miuid\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0miiid\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 107\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 108\u001b[0m \u001b[0;31m# If the details dict was also returned\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m~/.local/lib/python3.7/site-packages/surprise/prediction_algorithms/knns.py\u001b[0m in \u001b[0;36mestimate\u001b[0;34m(self, u, i)\u001b[0m\n\u001b[1;32m 109\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 110\u001b[0m \u001b[0mneighbors\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msim\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mx\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mx2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mr\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0myr\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0my\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 111\u001b[0;31m \u001b[0mk_neighbors\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mheapq\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mnlargest\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mk\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mneighbors\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mkey\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mlambda\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m:\u001b[0m \u001b[0mt\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 112\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 113\u001b[0m \u001b[0;31m# compute weighted average\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/lib/python3.7/heapq.py\u001b[0m in \u001b[0;36mnlargest\u001b[0;34m(n, iterable, key)\u001b[0m\n\u001b[1;32m 567\u001b[0m \u001b[0;31m# General case, slowest method\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 568\u001b[0m \u001b[0mit\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0miter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miterable\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 569\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0melem\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0melem\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0melem\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mit\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 570\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 571\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/usr/lib/python3.7/heapq.py\u001b[0m in \u001b[0;36m\u001b[0;34m(.0)\u001b[0m\n\u001b[1;32m 567\u001b[0m \u001b[0;31m# General case, slowest method\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 568\u001b[0m \u001b[0mit\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0miter\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0miterable\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 569\u001b[0;31m \u001b[0mresult\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mkey\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0melem\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0melem\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mi\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0melem\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mzip\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mrange\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0mn\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m-\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mit\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 570\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 571\u001b[0m \u001b[0;32mreturn\u001b[0m \u001b[0mresult\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyboardInterrupt\u001b[0m: "
]
}
],
"source": [
"sim_options = {\n",
" \"name\": \"cosine\",\n",
" \"user_based\": True,\n",
"} # compute similarities between users\n",
"algo = sp.KNNBasic(sim_options=sim_options)\n",
"\n",
"helpers.ready_made(\n",
" algo,\n",
" reco_path=\"Recommendations generated/ml-100k/Ready_U-KNN_reco.csv\",\n",
" estimations_path=\"Recommendations generated/ml-100k/Ready_U-KNN_estimations.csv\",\n",
")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### I-KNN - on top baseline"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sim_options = {\n",
" \"name\": \"cosine\",\n",
" \"user_based\": False,\n",
"} # compute similarities between items\n",
"algo = sp.KNNBaseline()\n",
"\n",
"helpers.ready_made(\n",
" algo,\n",
" reco_path=\"Recommendations generated/ml-100k/Ready_I-KNNBaseline_reco.csv\",\n",
" estimations_path=\"Recommendations generated/ml-100k/Ready_I-KNNBaseline_estimations.csv\",\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"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",
"ev.evaluate_all(test, dir_path, super_reactions)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# project task 3: use a version of your choice of Surprise KNNalgorithm"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# read the docs and try to find best parameter configuration (let say in terms of RMSE)\n",
"# https://surprise.readthedocs.io/en/stable/knn_inspired.html##surprise.prediction_algorithms.knns.KNNBaseline\n",
"# the solution here can be similar to examples above\n",
"# please save the output in 'Recommendations generated/ml-100k/Self_KNNSurprisetask_reco.csv' and\n",
"# 'Recommendations generated/ml-100k/Self_KNNSurprisetask_estimations.csv'"
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Computing the cosine similarity matrix...\n",
"Done computing similarity matrix.\n",
"Generating predictions...\n",
"Generating top N recommendations...\n",
"Generating predictions...\n"
]
}
],
"source": [
"import helpers\n",
"import surprise as sp\n",
"import imp\n",
"import evaluation_measures as ev\n",
"\n",
"sim_options = {\n",
" \"name\": \"cosine\",\n",
" \"user_based\": False,\n",
"} # compute similarities between items\n",
"algo = sp.KNNWithZScore(k=40, sim_options=sim_options)\n",
"\n",
"helpers.ready_made(\n",
" algo, \n",
" reco_path='Recommendations generated/ml-100k/Ready_I-KNNWithZScore_reco.csv',\n",
" estimations_path='Recommendations generated/ml-100k/Ready_I-KNNWithZScore_estimations.csv')"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"943it [00:00, 8503.18it/s]\n",
"943it [00:00, 8069.90it/s]\n",
"943it [00:00, 7330.46it/s]\n",
"943it [00:00, 8456.26it/s]\n",
"943it [00:00, 8289.09it/s]\n",
"943it [00:00, 8115.60it/s]\n",
"943it [00:00, 8452.48it/s]\n",
"943it [00:00, 8448.04it/s]\n",
"943it [00:00, 8171.91it/s]\n",
"943it [00:00, 7235.55it/s]\n",
"943it [00:00, 7667.03it/s]\n",
"943it [00:00, 7646.62it/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",
" NDCG | \n",
" mAP | \n",
" MRR | \n",
" LAUC | \n",
" HR | \n",
" H2R | \n",
" Reco in test | \n",
" Test coverage | \n",
" Shannon | \n",
" Gini | \n",
"
\n",
" \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",
" 0.214651 | \n",
" 0.111707 | \n",
" 0.400939 | \n",
" 0.555546 | \n",
" 0.765642 | \n",
" 0.492047 | \n",
" 1.000000 | \n",
" 0.038961 | \n",
" 3.159079 | \n",
" 0.987317 | \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",
" 0.095957 | \n",
" 0.043178 | \n",
" 0.198193 | \n",
" 0.515501 | \n",
" 0.437964 | \n",
" 0.239661 | \n",
" 1.000000 | \n",
" 0.033911 | \n",
" 2.836513 | \n",
" 0.991139 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_KNNSurprisetask | \n",
" 0.946255 | \n",
" 0.745209 | \n",
" 0.083457 | \n",
" 0.032848 | \n",
" 0.041227 | \n",
" 0.055493 | \n",
" 0.074785 | \n",
" 0.048890 | \n",
" 0.089577 | \n",
" 0.040902 | \n",
" 0.189057 | \n",
" 0.513076 | \n",
" 0.417815 | \n",
" 0.217391 | \n",
" 0.888547 | \n",
" 0.130592 | \n",
" 3.611806 | \n",
" 0.978659 | \n",
"
\n",
" \n",
" 0 | \n",
" Self_GlobalAvg | \n",
" 1.125760 | \n",
" 0.943534 | \n",
" 0.061188 | \n",
" 0.025968 | \n",
" 0.031383 | \n",
" 0.041343 | \n",
" 0.040558 | \n",
" 0.032107 | \n",
" 0.067695 | \n",
" 0.027470 | \n",
" 0.171187 | \n",
" 0.509546 | \n",
" 0.384942 | \n",
" 0.142100 | \n",
" 1.000000 | \n",
" 0.025974 | \n",
" 2.711772 | \n",
" 0.992003 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_Random | \n",
" 1.528005 | \n",
" 1.227604 | \n",
" 0.042842 | \n",
" 0.019173 | \n",
" 0.022234 | \n",
" 0.028926 | \n",
" 0.026502 | \n",
" 0.019532 | \n",
" 0.044202 | \n",
" 0.015665 | \n",
" 0.107737 | \n",
" 0.506081 | \n",
" 0.313892 | \n",
" 0.080594 | \n",
" 0.986108 | \n",
" 0.182540 | \n",
" 5.094973 | \n",
" 0.907749 | \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",
" 0.024214 | \n",
" 0.008958 | \n",
" 0.048068 | \n",
" 0.499885 | \n",
" 0.154825 | \n",
" 0.072110 | \n",
" 0.402333 | \n",
" 0.434343 | \n",
" 5.133650 | \n",
" 0.877999 | \n",
"
\n",
" \n",
" 0 | \n",
" Ready_I-KNNWithZScore | \n",
" 0.950328 | \n",
" 0.745109 | \n",
" 0.003924 | \n",
" 0.001466 | \n",
" 0.001953 | \n",
" 0.002652 | \n",
" 0.003433 | \n",
" 0.001608 | \n",
" 0.004968 | \n",
" 0.002111 | \n",
" 0.014694 | \n",
" 0.497085 | \n",
" 0.029692 | \n",
" 0.006363 | \n",
" 0.574019 | \n",
" 0.065657 | \n",
" 2.646787 | \n",
" 0.991825 | \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",
" 0.003444 | \n",
" 0.001362 | \n",
" 0.011760 | \n",
" 0.496724 | \n",
" 0.021209 | \n",
" 0.004242 | \n",
" 0.482821 | \n",
" 0.059885 | \n",
" 2.232578 | \n",
" 0.994487 | \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",
" 0.000845 | \n",
" 0.000274 | \n",
" 0.002744 | \n",
" 0.496441 | \n",
" 0.007423 | \n",
" 0.000000 | \n",
" 0.602121 | \n",
" 0.010823 | \n",
" 2.089186 | \n",
" 0.995706 | \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",
" 0.001043 | \n",
" 0.000335 | \n",
" 0.003348 | \n",
" 0.496433 | \n",
" 0.009544 | \n",
" 0.000000 | \n",
" 0.699046 | \n",
" 0.005051 | \n",
" 1.945910 | \n",
" 0.995669 | \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",
" 0.000752 | \n",
" 0.000168 | \n",
" 0.001677 | \n",
" 0.496424 | \n",
" 0.009544 | \n",
" 0.000000 | \n",
" 0.600530 | \n",
" 0.005051 | \n",
" 1.803126 | \n",
" 0.996380 | \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",
" 0.000214 | \n",
" 0.000037 | \n",
" 0.000368 | \n",
" 0.496391 | \n",
" 0.003181 | \n",
" 0.000000 | \n",
" 0.392153 | \n",
" 0.115440 | \n",
" 4.174741 | \n",
" 0.965327 | \n",
"
\n",
" \n",
"
\n",
"
"
],
"text/plain": [
" Model RMSE MAE precision recall F_1 \\\n",
"0 Self_TopPop 2.508258 2.217909 0.188865 0.116919 0.118732 \n",
"0 Ready_Baseline 0.949459 0.752487 0.091410 0.037652 0.046030 \n",
"0 Self_KNNSurprisetask 0.946255 0.745209 0.083457 0.032848 0.041227 \n",
"0 Self_GlobalAvg 1.125760 0.943534 0.061188 0.025968 0.031383 \n",
"0 Ready_Random 1.528005 1.227604 0.042842 0.019173 0.022234 \n",
"0 Ready_I-KNN 1.030386 0.813067 0.026087 0.006908 0.010593 \n",
"0 Ready_I-KNNWithZScore 0.950328 0.745109 0.003924 0.001466 0.001953 \n",
"0 Ready_I-KNNBaseline 0.935327 0.737424 0.002545 0.000755 0.001105 \n",
"0 Ready_U-KNN 1.023495 0.807913 0.000742 0.000205 0.000305 \n",
"0 Self_TopRated 1.030712 0.820904 0.000954 0.000188 0.000298 \n",
"0 Self_BaselineUI 0.967585 0.762740 0.000954 0.000170 0.000278 \n",
"0 Self_IKNN 1.018363 0.808793 0.000318 0.000108 0.000140 \n",
"\n",
" F_05 precision_super recall_super NDCG mAP MRR \\\n",
"0 0.141584 0.130472 0.137473 0.214651 0.111707 0.400939 \n",
"0 0.061286 0.079614 0.056463 0.095957 0.043178 0.198193 \n",
"0 0.055493 0.074785 0.048890 0.089577 0.040902 0.189057 \n",
"0 0.041343 0.040558 0.032107 0.067695 0.027470 0.171187 \n",
"0 0.028926 0.026502 0.019532 0.044202 0.015665 0.107737 \n",
"0 0.016046 0.021137 0.009522 0.024214 0.008958 0.048068 \n",
"0 0.002652 0.003433 0.001608 0.004968 0.002111 0.014694 \n",
"0 0.001602 0.002253 0.000930 0.003444 0.001362 0.011760 \n",
"0 0.000449 0.000536 0.000198 0.000845 0.000274 0.002744 \n",
"0 0.000481 0.000644 0.000223 0.001043 0.000335 0.003348 \n",
"0 0.000463 0.000644 0.000189 0.000752 0.000168 0.001677 \n",
"0 0.000189 0.000000 0.000000 0.000214 0.000037 0.000368 \n",
"\n",
" LAUC HR H2R Reco in test Test coverage Shannon \\\n",
"0 0.555546 0.765642 0.492047 1.000000 0.038961 3.159079 \n",
"0 0.515501 0.437964 0.239661 1.000000 0.033911 2.836513 \n",
"0 0.513076 0.417815 0.217391 0.888547 0.130592 3.611806 \n",
"0 0.509546 0.384942 0.142100 1.000000 0.025974 2.711772 \n",
"0 0.506081 0.313892 0.080594 0.986108 0.182540 5.094973 \n",
"0 0.499885 0.154825 0.072110 0.402333 0.434343 5.133650 \n",
"0 0.497085 0.029692 0.006363 0.574019 0.065657 2.646787 \n",
"0 0.496724 0.021209 0.004242 0.482821 0.059885 2.232578 \n",
"0 0.496441 0.007423 0.000000 0.602121 0.010823 2.089186 \n",
"0 0.496433 0.009544 0.000000 0.699046 0.005051 1.945910 \n",
"0 0.496424 0.009544 0.000000 0.600530 0.005051 1.803126 \n",
"0 0.496391 0.003181 0.000000 0.392153 0.115440 4.174741 \n",
"\n",
" Gini \n",
"0 0.987317 \n",
"0 0.991139 \n",
"0 0.978659 \n",
"0 0.992003 \n",
"0 0.907749 \n",
"0 0.877999 \n",
"0 0.991825 \n",
"0 0.994487 \n",
"0 0.995706 \n",
"0 0.995669 \n",
"0 0.996380 \n",
"0 0.965327 "
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"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",
"ev.evaluate_all(test, dir_path, super_reactions)"
]
},
{
"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.7.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}