{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "### Prepare test set" ] }, { "cell_type": "code", "execution_count": 41, "metadata": { "slideshow": { "slide_type": "-" } }, "outputs": [], "source": [ "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", "from tqdm import tqdm\n", "\n", "# In evaluation we do not load train set - it is not needed\n", "test=pd.read_csv('./Datasets/ml-100k/test.csv', sep='\\t', header=None)\n", "test.columns=['user', 'item', 'rating', 'timestamp']\n", "\n", "test['user_code'] = test['user'].astype(\"category\").cat.codes\n", "test['item_code'] = test['item'].astype(\"category\").cat.codes\n", "\n", "user_code_id = dict(enumerate(test['user'].astype(\"category\").cat.categories))\n", "user_id_code = dict((v, k) for k, v in user_code_id.items())\n", "item_code_id = dict(enumerate(test['item'].astype(\"category\").cat.categories))\n", "item_id_code = dict((v, k) for k, v in item_code_id.items())\n", "\n", "test_ui = sparse.csr_matrix((test['rating'], (test['user_code'], test['item_code'])))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Estimations metrics" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "estimations_df=pd.read_csv('Recommendations generated/ml-100k/Ready_Baseline_estimations.csv', header=None)\n", "estimations_df.columns=['user', 'item' ,'score']\n", "\n", "estimations_df['user_code']=[user_id_code[user] for user in estimations_df['user']]\n", "estimations_df['item_code']=[item_id_code[item] for item in estimations_df['item']]\n", "estimations=sparse.csr_matrix((estimations_df['score'], (estimations_df['user_code'], estimations_df['item_code'])), shape=test_ui.shape)" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [], "source": [ "def estimations_metrics(test_ui, estimations):\n", " result=[]\n", "\n", " RMSE=(np.sum((estimations.data-test_ui.data)**2)/estimations.nnz)**(1/2)\n", " result.append(['RMSE', RMSE])\n", "\n", " MAE=np.sum(abs(estimations.data-test_ui.data))/estimations.nnz\n", " result.append(['MAE', MAE])\n", " \n", " df_result=(pd.DataFrame(list(zip(*result))[1])).T\n", " df_result.columns=list(zip(*result))[0]\n", " return df_result" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
RMSEMAE
00.9494590.752487
\n", "
" ], "text/plain": [ " RMSE MAE\n", "0 0.949459 0.752487" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# in case of error (in the laboratories) you might have to switch to the other version of pandas\n", "# try !pip3 install pandas=='1.0.3' (or pip if you use python 2) and restart the kernel\n", "\n", "estimations_metrics(test_ui, estimations)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Ranking metrics" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[663, 475, 62, ..., 472, 269, 503],\n", " [ 48, 313, 475, ..., 591, 175, 466],\n", " [351, 313, 475, ..., 591, 175, 466],\n", " ...,\n", " [259, 313, 475, ..., 11, 591, 175],\n", " [ 33, 313, 475, ..., 11, 591, 175],\n", " [ 77, 313, 475, ..., 11, 591, 175]])" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import numpy as np\n", "reco = np.loadtxt('Recommendations generated/ml-100k/Ready_Baseline_reco.csv', delimiter=',')\n", "# Let's ignore scores - they are not used in evaluation: \n", "users=reco[:,:1]\n", "items=reco[:,1::2]\n", "# Let's use inner ids instead of real ones\n", "users=np.vectorize(lambda x: user_id_code.setdefault(x, -1))(users)\n", "items=np.vectorize(lambda x: item_id_code.setdefault(x, -1))(items) # maybe items we recommend are not in test set\n", "# Let's put them into one array\n", "reco=np.concatenate((users, items), axis=1)\n", "reco" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "def ranking_metrics(test_ui, reco, super_reactions=[], topK=10):\n", " \n", " nb_items=test_ui.shape[1]\n", " relevant_users, super_relevant_users, prec, rec, F_1, F_05, prec_super, rec_super, ndcg, mAP, MRR, LAUC, HR=\\\n", " 0,0,0,0,0,0,0,0,0,0,0,0,0\n", " \n", " cg = (1.0 / np.log2(np.arange(2, topK + 2)))\n", " cg_sum = np.cumsum(cg)\n", " \n", " for (nb_user, user) in tqdm(enumerate(reco[:,0])):\n", " u_rated_items=test_ui.indices[test_ui.indptr[user]:test_ui.indptr[user+1]]\n", " nb_u_rated_items=len(u_rated_items)\n", " if nb_u_rated_items>0: # skip users with no items in test set (still possible that there will be no super items)\n", " relevant_users+=1\n", " \n", " u_super_items=u_rated_items[np.vectorize(lambda x: x in super_reactions)\\\n", " (test_ui.data[test_ui.indptr[user]:test_ui.indptr[user+1]])]\n", " # more natural seems u_super_items=[item for item in u_rated_items if test_ui[user,item] in super_reactions]\n", " # but accesing test_ui[user,item] is expensive -we should avoid doing it\n", " if len(u_super_items)>0:\n", " super_relevant_users+=1\n", " \n", " user_successes=np.zeros(topK)\n", " nb_user_successes=0\n", " user_super_successes=np.zeros(topK)\n", " nb_user_super_successes=0\n", " \n", " # evaluation\n", " for (item_position,item) in enumerate(reco[nb_user,1:topK+1]):\n", " if item in u_rated_items:\n", " user_successes[item_position]=1\n", " nb_user_successes+=1\n", " if item in u_super_items:\n", " user_super_successes[item_position]=1\n", " nb_user_super_successes+=1\n", " \n", " prec_u=nb_user_successes/topK \n", " prec+=prec_u\n", " \n", " rec_u=nb_user_successes/nb_u_rated_items\n", " rec+=rec_u\n", " \n", " F_1+=2*(prec_u*rec_u)/(prec_u+rec_u) if prec_u+rec_u>0 else 0\n", " F_05+=(0.5**2+1)*(prec_u*rec_u)/(0.5**2*prec_u+rec_u) if prec_u+rec_u>0 else 0\n", " \n", " prec_super+=nb_user_super_successes/topK\n", " rec_super+=nb_user_super_successes/max(len(u_super_items),1) # to set 0 if no super items\n", " ndcg+=np.dot(user_successes,cg)/cg_sum[min(topK, nb_u_rated_items)-1]\n", " \n", " cumsum_successes=np.cumsum(user_successes)\n", " mAP+=np.dot(cumsum_successes/np.arange(1,topK+1), user_successes)/min(topK, nb_u_rated_items)\n", " MRR+=1/(user_successes.nonzero()[0][0]+1) if user_successes.nonzero()[0].size>0 else 0\n", " LAUC+=(np.dot(cumsum_successes, 1-user_successes)+\\\n", " (nb_user_successes+nb_u_rated_items)/2*((nb_items-nb_u_rated_items)-(topK-nb_user_successes)))/\\\n", " ((nb_items-nb_u_rated_items)*nb_u_rated_items)\n", " \n", " HR+=nb_user_successes>0\n", " \n", " \n", " result=[]\n", " result.append(('precision', prec/relevant_users))\n", " result.append(('recall', rec/relevant_users))\n", " result.append(('F_1', F_1/relevant_users))\n", " result.append(('F_05', F_05/relevant_users))\n", " result.append(('precision_super', prec_super/super_relevant_users))\n", " result.append(('recall_super', rec_super/super_relevant_users))\n", " result.append(('NDCG', ndcg/relevant_users))\n", " result.append(('mAP', mAP/relevant_users))\n", " result.append(('MRR', MRR/relevant_users))\n", " result.append(('LAUC', LAUC/relevant_users))\n", " result.append(('HR', HR/relevant_users))\n", "\n", " df_result=(pd.DataFrame(list(zip(*result))[1])).T\n", " df_result.columns=list(zip(*result))[0]\n", " return df_result" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "943it [00:00, 11776.77it/s]\n" ] }, { "data": { "text/html": [ "
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
precisionrecallF_1F_05precision_superrecall_superNDCGmAPMRRLAUCHR
00.091410.0376520.046030.0612860.0796140.0564630.0959570.0431780.1981930.5155010.437964
\n", "
" ], "text/plain": [ " precision recall F_1 F_05 precision_super recall_super \\\n", "0 0.09141 0.037652 0.04603 0.061286 0.079614 0.056463 \n", "\n", " NDCG mAP MRR LAUC HR \n", "0 0.095957 0.043178 0.198193 0.515501 0.437964 " ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ranking_metrics(test_ui, reco, super_reactions=[4,5], topK=10)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Diversity metrics" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "def diversity_metrics(test_ui, reco, topK=10):\n", " \n", " frequencies=defaultdict(int)\n", " \n", " # let's assign 0 to all items in test set\n", " for item in list(set(test_ui.indices)):\n", " frequencies[item]=0\n", " \n", " # counting frequencies\n", " for item in reco[:,1:].flat:\n", " frequencies[item]+=1\n", " \n", " nb_reco_outside_test=frequencies[-1]\n", " del frequencies[-1]\n", " \n", " frequencies=np.array(list(frequencies.values()))\n", " \n", " nb_rec_items=len(frequencies[frequencies>0])\n", " nb_reco_inside_test=np.sum(frequencies)\n", " \n", " frequencies=frequencies/np.sum(frequencies)\n", " frequencies=np.sort(frequencies)\n", " \n", " with np.errstate(divide='ignore'): # let's put zeros put items with 0 frequency and ignore division warning\n", " log_frequencies=np.nan_to_num(np.log(frequencies), posinf=0, neginf=0)\n", " \n", " result=[]\n", " result.append(('Reco in test', nb_reco_inside_test/(nb_reco_inside_test+nb_reco_outside_test)))\n", " result.append(('Test coverage', nb_rec_items/test_ui.shape[1]))\n", " result.append(('Shannon', -np.dot(frequencies, log_frequencies)))\n", " result.append(('Gini', np.dot(frequencies, np.arange(1-len(frequencies), len(frequencies), 2))/(len(frequencies)-1)))\n", " \n", " df_result=(pd.DataFrame(list(zip(*result))[1])).T\n", " df_result.columns=list(zip(*result))[0]\n", " return df_result" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Reco in testTest coverageShannonGini
01.00.0339112.8365130.991139
\n", "
" ], "text/plain": [ " Reco in test Test coverage Shannon Gini\n", "0 1.0 0.033911 2.836513 0.991139" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# in case of errors try !pip3 install numpy==1.18.4 (or pip if you use python 2) and restart the kernel\n", "\n", "import evaluation_measures as ev\n", "import imp\n", "imp.reload(ev)\n", "\n", "x=diversity_metrics(test_ui, reco, topK=10)\n", "x" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# To be used in other notebooks" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "943it [00:00, 11489.51it/s]\n" ] }, { "data": { "text/html": [ "
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
RMSEMAEprecisionrecallF_1F_05precision_superrecall_superNDCGmAPMRRLAUCHRF_2Whole_averageReco in testTest coverageShannonGini
00.9494590.7524870.091410.0376520.046030.0612860.0796140.0564630.0959570.0431780.1981930.5155010.4379640.0395490.14191.00.0339112.8365130.991139
\n", "
" ], "text/plain": [ " RMSE MAE precision recall F_1 F_05 \\\n", "0 0.949459 0.752487 0.09141 0.037652 0.04603 0.061286 \n", "\n", " precision_super recall_super NDCG mAP MRR LAUC \\\n", "0 0.079614 0.056463 0.095957 0.043178 0.198193 0.515501 \n", "\n", " HR F_2 Whole_average Reco in test Test coverage Shannon \\\n", "0 0.437964 0.039549 0.1419 1.0 0.033911 2.836513 \n", "\n", " Gini \n", "0 0.991139 " ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import evaluation_measures as ev\n", "import imp\n", "imp.reload(ev)\n", "\n", "estimations_df=pd.read_csv('Recommendations generated/ml-100k/Ready_Baseline_estimations.csv', header=None)\n", "reco=np.loadtxt('Recommendations generated/ml-100k/Ready_Baseline_reco.csv', delimiter=',')\n", "\n", "ev.evaluate(test=pd.read_csv('./Datasets/ml-100k/test.csv', sep='\\t', header=None),\n", " estimations_df=estimations_df, \n", " reco=reco,\n", " super_reactions=[4,5])\n", "#also you can just type ev.evaluate_all(estimations_df, reco) - I put above values as default" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "943it [00:00, 11216.05it/s]\n", "943it [00:00, 11620.62it/s]\n", "943it [00:00, 11489.57it/s]\n", "943it [00:00, 11216.02it/s]\n", "943it [00:00, 11776.74it/s]\n", "943it [00:00, 12396.66it/s]\n", "943it [00:00, 12396.39it/s]\n", "943it [00:00, 12561.74it/s]\n", "943it [00:00, 11216.08it/s]\n", "943it [00:00, 11631.42it/s]\n", "943it [00:00, 11084.07it/s]\n", "943it [00:00, 11776.74it/s]\n", "943it [00:00, 10706.13it/s]\n", "943it [00:00, 11925.84it/s]\n", "943it [00:00, 11925.88it/s]\n", "943it [00:00, 11925.34it/s]\n", "943it [00:00, 12235.52it/s]\n", "943it [00:00, 10829.27it/s]\n" ] } ], "source": [ "import evaluation_measures as ev\n", "import imp\n", "imp.reload(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", "#also you can just type ev.evaluate_all() - I put above values as default" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ModelRMSEMAEprecisionrecallF_1F_05precision_superrecall_super
0Self_RP3Beta3.7029283.5277130.3226940.2160690.2121520.2475380.2452790.284983
0Self_P33.7024463.5272730.2821850.1920920.1867490.2169800.2041850.240096
0Self_TopPop2.5082582.2179090.1888650.1169190.1187320.1415840.1304720.137473
0Self_SVDBaseline3.6458713.4803080.1359490.0788680.0820110.0991880.1069740.103767
0Ready_SVD0.9508350.7486760.0978790.0483350.0537800.0684200.0861590.080289
0Self_SVD0.9139660.7178460.1055140.0445660.0541520.0715750.0953860.075767
0Ready_Baseline0.9494590.7524870.0914100.0376520.0460300.0612860.0796140.056463
0Ready_SVDBiased0.9432770.7436280.0809120.0330480.0404450.0538810.0708150.049631
0Self_KNNSurprisetask0.9462550.7452090.0834570.0328480.0412270.0554930.0747850.048890
0Self_TopRated2.5082582.2179090.0793210.0326670.0399830.0531700.0688840.048582
0Self_GlobalAvg1.1257600.9435340.0611880.0259680.0313830.0413430.0405580.032107
0Ready_Random1.5142651.2159560.0487800.0210070.0246670.0324950.0318670.023414
0Ready_I-KNN1.0303860.8130670.0260870.0069080.0105930.0160460.0211370.009522
0Ready_I-KNNBaseline0.9353270.7374240.0025450.0007550.0011050.0016020.0022530.000930
0Ready_U-KNN1.0234950.8079130.0007420.0002050.0003050.0004490.0005360.000198
0Self_BaselineIU0.9581360.7540510.0009540.0001880.0002980.0004810.0006440.000223
0Self_BaselineUI0.9675850.7627400.0009540.0001700.0002780.0004630.0006440.000189
0Self_IKNN1.0183630.8087930.0003180.0001080.0001400.0001890.0000000.000000
\n", "
" ], "text/plain": [ " Model RMSE MAE precision recall F_1 \\\n", "0 Self_RP3Beta 3.702928 3.527713 0.322694 0.216069 0.212152 \n", "0 Self_P3 3.702446 3.527273 0.282185 0.192092 0.186749 \n", "0 Self_TopPop 2.508258 2.217909 0.188865 0.116919 0.118732 \n", "0 Self_SVDBaseline 3.645871 3.480308 0.135949 0.078868 0.082011 \n", "0 Ready_SVD 0.950835 0.748676 0.097879 0.048335 0.053780 \n", "0 Self_SVD 0.913966 0.717846 0.105514 0.044566 0.054152 \n", "0 Ready_Baseline 0.949459 0.752487 0.091410 0.037652 0.046030 \n", "0 Ready_SVDBiased 0.943277 0.743628 0.080912 0.033048 0.040445 \n", "0 Self_KNNSurprisetask 0.946255 0.745209 0.083457 0.032848 0.041227 \n", "0 Self_TopRated 2.508258 2.217909 0.079321 0.032667 0.039983 \n", "0 Self_GlobalAvg 1.125760 0.943534 0.061188 0.025968 0.031383 \n", "0 Ready_Random 1.514265 1.215956 0.048780 0.021007 0.024667 \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_BaselineIU 0.958136 0.754051 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 \n", "0 0.247538 0.245279 0.284983 \n", "0 0.216980 0.204185 0.240096 \n", "0 0.141584 0.130472 0.137473 \n", "0 0.099188 0.106974 0.103767 \n", "0 0.068420 0.086159 0.080289 \n", "0 0.071575 0.095386 0.075767 \n", "0 0.061286 0.079614 0.056463 \n", "0 0.053881 0.070815 0.049631 \n", "0 0.055493 0.074785 0.048890 \n", "0 0.053170 0.068884 0.048582 \n", "0 0.041343 0.040558 0.032107 \n", "0 0.032495 0.031867 0.023414 \n", "0 0.016046 0.021137 0.009522 \n", "0 0.001602 0.002253 0.000930 \n", "0 0.000449 0.000536 0.000198 \n", "0 0.000481 0.000644 0.000223 \n", "0 0.000463 0.000644 0.000189 \n", "0 0.000189 0.000000 0.000000 " ] }, "execution_count": 52, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iloc[:,:9]" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ModelNDCGmAPMRRLAUCHRF_2Whole_averageReco in testTest coverageShannonGini
0Self_RP3Beta0.3882710.2482390.6363180.6056830.9109230.2054500.3769670.9997880.1789324.5496630.950182
0Self_P30.3391140.2049050.5721570.5935440.8759280.1817020.3408031.0000000.0772013.8758920.974947
0Self_TopPop0.2146510.1117070.4009390.5555460.7656420.1127500.2496071.0000000.0389613.1590790.987317
0Self_SVDBaseline0.1594860.0797830.3285760.5363110.6320250.0771450.2016740.9998940.2813855.1407210.909056
0Ready_SVD0.1135530.0540940.2490370.5208930.4984090.0484390.1599410.9979850.2049064.3957210.954872
0Self_SVD0.1088020.0517300.2009190.5190210.4825030.0467410.1547230.8616120.1421363.8454610.973440
0Ready_Baseline0.0959570.0431780.1981930.5155010.4379640.0395490.1419001.0000000.0339112.8365130.991139
0Ready_SVDBiased0.0904960.0419280.2001920.5131760.4114530.0347760.1350630.9987270.1681104.1656180.964211
0Self_KNNSurprisetask0.0895770.0409020.1890570.5130760.4178150.0349960.1351770.8885470.1305923.6118060.978659
0Self_TopRated0.0707660.0276020.1147900.5129430.4114530.0343850.1245461.0000000.0245312.7612380.991660
0Self_GlobalAvg0.0676950.0274700.1711870.5095460.3849420.0272130.1183831.0000000.0259742.7117720.992003
0Ready_Random0.0529040.0205110.1267900.5070240.3223750.0216350.1027890.9880170.1839835.1004430.906900
0Ready_I-KNN0.0242140.0089580.0480680.4998850.1548250.0080070.0695210.4023330.4343435.1336500.877999
0Ready_I-KNNBaseline0.0034440.0013620.0117600.4967240.0212090.0008620.0453790.4828210.0598852.2325780.994487
0Ready_U-KNN0.0008450.0002740.0027440.4964410.0074230.0002350.0425330.6021210.0108232.0891860.995706
0Self_BaselineIU0.0010430.0003350.0033480.4964330.0095440.0002200.0428090.6990460.0050511.9459100.995669
0Self_BaselineUI0.0007520.0001680.0016770.4964240.0095440.0002010.0426220.6005300.0050511.8031260.996380
0Self_IKNN0.0002140.0000370.0003680.4963910.0031810.0001180.0417550.3921530.1154404.1747410.965327
\n", "
" ], "text/plain": [ " Model NDCG mAP MRR LAUC HR \\\n", "0 Self_RP3Beta 0.388271 0.248239 0.636318 0.605683 0.910923 \n", "0 Self_P3 0.339114 0.204905 0.572157 0.593544 0.875928 \n", "0 Self_TopPop 0.214651 0.111707 0.400939 0.555546 0.765642 \n", "0 Self_SVDBaseline 0.159486 0.079783 0.328576 0.536311 0.632025 \n", "0 Ready_SVD 0.113553 0.054094 0.249037 0.520893 0.498409 \n", "0 Self_SVD 0.108802 0.051730 0.200919 0.519021 0.482503 \n", "0 Ready_Baseline 0.095957 0.043178 0.198193 0.515501 0.437964 \n", "0 Ready_SVDBiased 0.090496 0.041928 0.200192 0.513176 0.411453 \n", "0 Self_KNNSurprisetask 0.089577 0.040902 0.189057 0.513076 0.417815 \n", "0 Self_TopRated 0.070766 0.027602 0.114790 0.512943 0.411453 \n", "0 Self_GlobalAvg 0.067695 0.027470 0.171187 0.509546 0.384942 \n", "0 Ready_Random 0.052904 0.020511 0.126790 0.507024 0.322375 \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_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", " F_2 Whole_average Reco in test Test coverage Shannon Gini \n", "0 0.205450 0.376967 0.999788 0.178932 4.549663 0.950182 \n", "0 0.181702 0.340803 1.000000 0.077201 3.875892 0.974947 \n", "0 0.112750 0.249607 1.000000 0.038961 3.159079 0.987317 \n", "0 0.077145 0.201674 0.999894 0.281385 5.140721 0.909056 \n", "0 0.048439 0.159941 0.997985 0.204906 4.395721 0.954872 \n", "0 0.046741 0.154723 0.861612 0.142136 3.845461 0.973440 \n", "0 0.039549 0.141900 1.000000 0.033911 2.836513 0.991139 \n", "0 0.034776 0.135063 0.998727 0.168110 4.165618 0.964211 \n", "0 0.034996 0.135177 0.888547 0.130592 3.611806 0.978659 \n", "0 0.034385 0.124546 1.000000 0.024531 2.761238 0.991660 \n", "0 0.027213 0.118383 1.000000 0.025974 2.711772 0.992003 \n", "0 0.021635 0.102789 0.988017 0.183983 5.100443 0.906900 \n", "0 0.008007 0.069521 0.402333 0.434343 5.133650 0.877999 \n", "0 0.000862 0.045379 0.482821 0.059885 2.232578 0.994487 \n", "0 0.000235 0.042533 0.602121 0.010823 2.089186 0.995706 \n", "0 0.000220 0.042809 0.699046 0.005051 1.945910 0.995669 \n", "0 0.000201 0.042622 0.600530 0.005051 1.803126 0.996380 \n", "0 0.000118 0.041755 0.392153 0.115440 4.174741 0.965327 " ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.iloc[:,np.append(0,np.arange(9, df.shape[1]))]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Check metrics on toy dataset" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "3it [00:00, ?it/s]\n", "3it [00:00, ?it/s]\n" ] }, { "data": { "text/html": [ "
\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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ModelRMSEMAEprecisionrecallF_1F_05precision_superrecall_superNDCGmAPMRRLAUCHRF_2Whole_averageReco in testTest coverageShannonGini
0Self_BaselineUI1.6124521.4000.4444440.8888890.5555560.4786320.3333330.750.6769070.5740740.6111110.6388891.00.6984130.6375210.8888890.81.3862940.250000
0Self_BaselineIU1.6483371.5750.4444440.8888890.5555560.4786320.3333330.750.7205500.6296300.6666670.7222221.00.6984130.6573610.7777780.81.3517840.357143
\n", "
" ], "text/plain": [ " Model RMSE MAE precision recall F_1 F_05 \\\n", "0 Self_BaselineUI 1.612452 1.400 0.444444 0.888889 0.555556 0.478632 \n", "0 Self_BaselineIU 1.648337 1.575 0.444444 0.888889 0.555556 0.478632 \n", "\n", " precision_super recall_super NDCG mAP MRR LAUC HR \\\n", "0 0.333333 0.75 0.676907 0.574074 0.611111 0.638889 1.0 \n", "0 0.333333 0.75 0.720550 0.629630 0.666667 0.722222 1.0 \n", "\n", " F_2 Whole_average Reco in test Test coverage Shannon Gini \n", "0 0.698413 0.637521 0.888889 0.8 1.386294 0.250000 \n", "0 0.698413 0.657361 0.777778 0.8 1.351784 0.357143 " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Training data:\n" ] }, { "data": { "text/plain": [ "matrix([[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]], dtype=int64)" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Test data:\n" ] }, { "data": { "text/plain": [ "matrix([[0, 0, 0, 0, 0, 0, 3, 0],\n", " [0, 0, 0, 0, 5, 0, 0, 0],\n", " [5, 0, 4, 0, 0, 0, 0, 2]], dtype=int64)" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Recommendations:\n" ] }, { "data": { "text/html": [ "
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
0123456
00305.0204.0604.0
110403.0602.0702.0
220405.0204.0704.0
\n", "
" ], "text/plain": [ " 0 1 2 3 4 5 6\n", "0 0 30 5.0 20 4.0 60 4.0\n", "1 10 40 3.0 60 2.0 70 2.0\n", "2 20 40 5.0 20 4.0 70 4.0" ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Estimations:\n" ] }, { "data": { "text/html": [ "
\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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
useritemest_score
00604.0
110403.0
22003.0
320204.0
420704.0
\n", "
" ], "text/plain": [ " user item est_score\n", "0 0 60 4.0\n", "1 10 40 3.0\n", "2 20 0 3.0\n", "3 20 20 4.0\n", "4 20 70 4.0" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "import evaluation_measures as ev\n", "import imp\n", "import helpers\n", "imp.reload(ev)\n", "\n", "dir_path=\"Recommendations generated/toy-example/\"\n", "super_reactions=[4,5]\n", "test=pd.read_csv('./Datasets/toy-example/test.csv', sep='\\t', header=None)\n", "\n", "display(ev.evaluate_all(test, dir_path, super_reactions, topK=3))\n", "#also you can just type ev.evaluate_all() - I put above values as default\n", "\n", "toy_train_read=pd.read_csv('./Datasets/toy-example/train.csv', sep='\\t', header=None, names=['user', 'item', 'rating', 'timestamp'])\n", "toy_test_read=pd.read_csv('./Datasets/toy-example/test.csv', sep='\\t', header=None, names=['user', 'item', 'rating', 'timestamp'])\n", "reco=pd.read_csv('Recommendations generated/toy-example/Self_BaselineUI_reco.csv', header=None)\n", "estimations=pd.read_csv('Recommendations generated/toy-example/Self_BaselineUI_estimations.csv', names=['user', 'item', 'est_score'])\n", "toy_train_ui, toy_test_ui, toy_user_code_id, toy_user_id_code, \\\n", "toy_item_code_id, toy_item_id_code = helpers.data_to_csr(toy_train_read, toy_test_read)\n", "\n", "print('Training data:')\n", "display(toy_train_ui.todense())\n", "\n", "print('Test data:')\n", "display(toy_test_ui.todense())\n", "\n", "print('Recommendations:')\n", "display(reco)\n", "\n", "print('Estimations:')\n", "display(estimations)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Sample recommendations" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Here is what user rated high:\n" ] }, { "data": { "text/html": [ "
\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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
userratingtitlegenres
229697575Rob Roy (1995)Drama, Romance, War
35577575Aliens (1986)Action, Sci-Fi, Thriller, War
372947575Contact (1997)Drama, Sci-Fi
319327575Shawshank Redemption, The (1994)Drama
65457575Unforgiven (1992)Western
62617575Raiders of the Lost Ark (1981)Action, Adventure
537307575Get Shorty (1995)Action, Comedy, Drama
251357574Twelve Monkeys (1995)Drama, Sci-Fi
267417574Star Trek IV: The Voyage Home (1986)Action, Adventure, Sci-Fi
272697574Empire Strikes Back, The (1980)Action, Adventure, Drama, Romance, Sci-Fi, War
513657574Interview with the Vampire (1994)Drama, Horror
548007574Scream 2 (1997)Horror, Thriller
280157574Face/Off (1997)Action, Sci-Fi, Thriller
534487574Nightmare Before Christmas, The (1993)Children's, Comedy, Musical
285347574Right Stuff, The (1983)Drama
\n", "
" ], "text/plain": [ " user rating title \\\n", "22969 757 5 Rob Roy (1995) \n", "3557 757 5 Aliens (1986) \n", "37294 757 5 Contact (1997) \n", "31932 757 5 Shawshank Redemption, The (1994) \n", "6545 757 5 Unforgiven (1992) \n", "6261 757 5 Raiders of the Lost Ark (1981) \n", "53730 757 5 Get Shorty (1995) \n", "25135 757 4 Twelve Monkeys (1995) \n", "26741 757 4 Star Trek IV: The Voyage Home (1986) \n", "27269 757 4 Empire Strikes Back, The (1980) \n", "51365 757 4 Interview with the Vampire (1994) \n", "54800 757 4 Scream 2 (1997) \n", "28015 757 4 Face/Off (1997) \n", "53448 757 4 Nightmare Before Christmas, The (1993) \n", "28534 757 4 Right Stuff, The (1983) \n", "\n", " genres \n", "22969 Drama, Romance, War \n", "3557 Action, Sci-Fi, Thriller, War \n", "37294 Drama, Sci-Fi \n", "31932 Drama \n", "6545 Western \n", "6261 Action, Adventure \n", "53730 Action, Comedy, Drama \n", "25135 Drama, Sci-Fi \n", "26741 Action, Adventure, Sci-Fi \n", "27269 Action, Adventure, Drama, Romance, Sci-Fi, War \n", "51365 Drama, Horror \n", "54800 Horror, Thriller \n", "28015 Action, Sci-Fi, Thriller \n", "53448 Children's, Comedy, Musical \n", "28534 Drama " ] }, "metadata": {}, "output_type": "display_data" }, { "name": "stdout", "output_type": "stream", "text": [ "Here is what we recommend:\n" ] }, { "data": { "text/html": [ "
\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", " \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", " \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", " \n", " \n", " \n", "
userrec_nbtitlegenres
755757.01Great Day in Harlem, A (1994)Documentary
1697757.02Tough and Deadly (1995)Action, Drama, Thriller
2639757.03Aiqing wansui (1994)Drama
3581757.04Delta of Venus (1994)Drama
4523757.05Someone Else's America (1995)Drama
5465757.06Saint of Fort Washington, The (1993)Drama
6407757.07Celestial Clockwork (1994)Comedy
7348757.08Some Mother's Son (1996)Drama
9244757.09Maya Lin: A Strong Clear Vision (1994)Documentary
8290757.010Prefontaine (1997)Drama
\n", "
" ], "text/plain": [ " user rec_nb title \\\n", "755 757.0 1 Great Day in Harlem, A (1994) \n", "1697 757.0 2 Tough and Deadly (1995) \n", "2639 757.0 3 Aiqing wansui (1994) \n", "3581 757.0 4 Delta of Venus (1994) \n", "4523 757.0 5 Someone Else's America (1995) \n", "5465 757.0 6 Saint of Fort Washington, The (1993) \n", "6407 757.0 7 Celestial Clockwork (1994) \n", "7348 757.0 8 Some Mother's Son (1996) \n", "9244 757.0 9 Maya Lin: A Strong Clear Vision (1994) \n", "8290 757.0 10 Prefontaine (1997) \n", "\n", " genres \n", "755 Documentary \n", "1697 Action, Drama, Thriller \n", "2639 Drama \n", "3581 Drama \n", "4523 Drama \n", "5465 Drama \n", "6407 Comedy \n", "7348 Drama \n", "9244 Documentary \n", "8290 Drama " ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "train=pd.read_csv('./Datasets/ml-100k/train.csv', sep='\\t', header=None, names=['user', 'item', 'rating', 'timestamp'])\n", "items=pd.read_csv('./Datasets/ml-100k/movies.csv')\n", "\n", "user=random.choice(list(set(train['user'])))\n", "\n", "train_content=pd.merge(train, items, left_on='item', right_on='id')\n", "\n", "print('Here is what user rated high:')\n", "display(train_content[train_content['user']==user][['user', 'rating', 'title', 'genres']]\\\n", " .sort_values(by='rating', ascending=False)[:15])\n", "\n", "reco = np.loadtxt('Recommendations generated/ml-100k/Self_BaselineUI_reco.csv', delimiter=',')\n", "items=pd.read_csv('./Datasets/ml-100k/movies.csv')\n", "\n", "# Let's ignore scores - they are not used in evaluation: \n", "reco_users=reco[:,:1]\n", "reco_items=reco[:,1::2]\n", "# Let's put them into one array\n", "reco=np.concatenate((reco_users, reco_items), axis=1)\n", "\n", "# Let's rebuild it user-item dataframe\n", "recommended=[]\n", "for row in reco:\n", " for rec_nb, entry in enumerate(row[1:]):\n", " recommended.append((row[0], rec_nb+1, entry))\n", "recommended=pd.DataFrame(recommended, columns=['user','rec_nb', 'item'])\n", "\n", "recommended_content=pd.merge(recommended, items, left_on='item', right_on='id')\n", "\n", "print('Here is what we recommend:')\n", "recommended_content[recommended_content['user']==user][['user', 'rec_nb', 'title', 'genres']].sort_values(by='rec_nb')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# project task 3: implement some other evaluation measure" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [], "source": [ "# it may be your idea, modification of what we have already implemented \n", "# (for example Hit2 rate which would count as a success users whoreceived at least 2 relevant recommendations) \n", "# or something well-known\n", "# expected output: modification of evaluation_measures.py such that evaluate_all will also display your measure" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "943it [00:00, 11351.38it/s]\n", "943it [00:00, 11776.77it/s]\n", "943it [00:00, 11631.38it/s]\n", "943it [00:00, 11925.95it/s]\n", "943it [00:00, 11489.67it/s]\n", "943it [00:00, 12396.58it/s]\n", "943it [00:00, 12561.94it/s]\n", "943it [00:00, 12078.99it/s]\n", "943it [00:00, 10955.16it/s]\n", "943it [00:00, 10947.46it/s]\n", "943it [00:00, 11489.44it/s]\n", "943it [00:00, 11925.88it/s]\n", "943it [00:00, 10585.87it/s]\n", "943it [00:00, 11925.80it/s]\n", "943it [00:00, 11631.25it/s]\n", "943it [00:00, 11631.52it/s]\n", "943it [00:00, 12396.70it/s]\n", "943it [00:00, 10829.27it/s]\n" ] }, { "data": { "text/html": [ "
\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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \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", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
ModelRMSEMAEprecisionrecallF_1F_05precision_superrecall_superNDCGmAPMRRLAUCHRF_2Whole_averageReco in testTest coverageShannonGini
0Self_RP3Beta3.7029283.5277130.3226940.2160690.2121520.2475380.2452790.2849830.3882710.2482390.6363180.6056830.9109230.2054500.3769670.9997880.1789324.5496630.950182
0Self_P33.7024463.5272730.2821850.1920920.1867490.2169800.2041850.2400960.3391140.2049050.5721570.5935440.8759280.1817020.3408031.0000000.0772013.8758920.974947
0Self_TopPop2.5082582.2179090.1888650.1169190.1187320.1415840.1304720.1374730.2146510.1117070.4009390.5555460.7656420.1127500.2496071.0000000.0389613.1590790.987317
0Self_SVDBaseline3.6458713.4803080.1359490.0788680.0820110.0991880.1069740.1037670.1594860.0797830.3285760.5363110.6320250.0771450.2016740.9998940.2813855.1407210.909056
0Ready_SVD0.9508350.7486760.0978790.0483350.0537800.0684200.0861590.0802890.1135530.0540940.2490370.5208930.4984090.0484390.1599410.9979850.2049064.3957210.954872
0Self_SVD0.9139660.7178460.1055140.0445660.0541520.0715750.0953860.0757670.1088020.0517300.2009190.5190210.4825030.0467410.1547230.8616120.1421363.8454610.973440
0Ready_Baseline0.9494590.7524870.0914100.0376520.0460300.0612860.0796140.0564630.0959570.0431780.1981930.5155010.4379640.0395490.1419001.0000000.0339112.8365130.991139
0Ready_SVDBiased0.9432770.7436280.0809120.0330480.0404450.0538810.0708150.0496310.0904960.0419280.2001920.5131760.4114530.0347760.1350630.9987270.1681104.1656180.964211
0Self_KNNSurprisetask0.9462550.7452090.0834570.0328480.0412270.0554930.0747850.0488900.0895770.0409020.1890570.5130760.4178150.0349960.1351770.8885470.1305923.6118060.978659
0Self_TopRated2.5082582.2179090.0793210.0326670.0399830.0531700.0688840.0485820.0707660.0276020.1147900.5129430.4114530.0343850.1245461.0000000.0245312.7612380.991660
0Self_GlobalAvg1.1257600.9435340.0611880.0259680.0313830.0413430.0405580.0321070.0676950.0274700.1711870.5095460.3849420.0272130.1183831.0000000.0259742.7117720.992003
0Ready_Random1.5142651.2159560.0487800.0210070.0246670.0324950.0318670.0234140.0529040.0205110.1267900.5070240.3223750.0216350.1027890.9880170.1839835.1004430.906900
0Ready_I-KNN1.0303860.8130670.0260870.0069080.0105930.0160460.0211370.0095220.0242140.0089580.0480680.4998850.1548250.0080070.0695210.4023330.4343435.1336500.877999
0Ready_I-KNNBaseline0.9353270.7374240.0025450.0007550.0011050.0016020.0022530.0009300.0034440.0013620.0117600.4967240.0212090.0008620.0453790.4828210.0598852.2325780.994487
0Ready_U-KNN1.0234950.8079130.0007420.0002050.0003050.0004490.0005360.0001980.0008450.0002740.0027440.4964410.0074230.0002350.0425330.6021210.0108232.0891860.995706
0Self_BaselineIU0.9581360.7540510.0009540.0001880.0002980.0004810.0006440.0002230.0010430.0003350.0033480.4964330.0095440.0002200.0428090.6990460.0050511.9459100.995669
0Self_BaselineUI0.9675850.7627400.0009540.0001700.0002780.0004630.0006440.0001890.0007520.0001680.0016770.4964240.0095440.0002010.0426220.6005300.0050511.8031260.996380
0Self_IKNN1.0183630.8087930.0003180.0001080.0001400.0001890.0000000.0000000.0002140.0000370.0003680.4963910.0031810.0001180.0417550.3921530.1154404.1747410.965327
\n", "
" ], "text/plain": [ " Model RMSE MAE precision recall F_1 \\\n", "0 Self_RP3Beta 3.702928 3.527713 0.322694 0.216069 0.212152 \n", "0 Self_P3 3.702446 3.527273 0.282185 0.192092 0.186749 \n", "0 Self_TopPop 2.508258 2.217909 0.188865 0.116919 0.118732 \n", "0 Self_SVDBaseline 3.645871 3.480308 0.135949 0.078868 0.082011 \n", "0 Ready_SVD 0.950835 0.748676 0.097879 0.048335 0.053780 \n", "0 Self_SVD 0.913966 0.717846 0.105514 0.044566 0.054152 \n", "0 Ready_Baseline 0.949459 0.752487 0.091410 0.037652 0.046030 \n", "0 Ready_SVDBiased 0.943277 0.743628 0.080912 0.033048 0.040445 \n", "0 Self_KNNSurprisetask 0.946255 0.745209 0.083457 0.032848 0.041227 \n", "0 Self_TopRated 2.508258 2.217909 0.079321 0.032667 0.039983 \n", "0 Self_GlobalAvg 1.125760 0.943534 0.061188 0.025968 0.031383 \n", "0 Ready_Random 1.514265 1.215956 0.048780 0.021007 0.024667 \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_BaselineIU 0.958136 0.754051 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.247538 0.245279 0.284983 0.388271 0.248239 0.636318 \n", "0 0.216980 0.204185 0.240096 0.339114 0.204905 0.572157 \n", "0 0.141584 0.130472 0.137473 0.214651 0.111707 0.400939 \n", "0 0.099188 0.106974 0.103767 0.159486 0.079783 0.328576 \n", "0 0.068420 0.086159 0.080289 0.113553 0.054094 0.249037 \n", "0 0.071575 0.095386 0.075767 0.108802 0.051730 0.200919 \n", "0 0.061286 0.079614 0.056463 0.095957 0.043178 0.198193 \n", "0 0.053881 0.070815 0.049631 0.090496 0.041928 0.200192 \n", "0 0.055493 0.074785 0.048890 0.089577 0.040902 0.189057 \n", "0 0.053170 0.068884 0.048582 0.070766 0.027602 0.114790 \n", "0 0.041343 0.040558 0.032107 0.067695 0.027470 0.171187 \n", "0 0.032495 0.031867 0.023414 0.052904 0.020511 0.126790 \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 F_2 Whole_average Reco in test Test coverage \\\n", "0 0.605683 0.910923 0.205450 0.376967 0.999788 0.178932 \n", "0 0.593544 0.875928 0.181702 0.340803 1.000000 0.077201 \n", "0 0.555546 0.765642 0.112750 0.249607 1.000000 0.038961 \n", "0 0.536311 0.632025 0.077145 0.201674 0.999894 0.281385 \n", "0 0.520893 0.498409 0.048439 0.159941 0.997985 0.204906 \n", "0 0.519021 0.482503 0.046741 0.154723 0.861612 0.142136 \n", "0 0.515501 0.437964 0.039549 0.141900 1.000000 0.033911 \n", "0 0.513176 0.411453 0.034776 0.135063 0.998727 0.168110 \n", "0 0.513076 0.417815 0.034996 0.135177 0.888547 0.130592 \n", "0 0.512943 0.411453 0.034385 0.124546 1.000000 0.024531 \n", "0 0.509546 0.384942 0.027213 0.118383 1.000000 0.025974 \n", "0 0.507024 0.322375 0.021635 0.102789 0.988017 0.183983 \n", "0 0.499885 0.154825 0.008007 0.069521 0.402333 0.434343 \n", "0 0.496724 0.021209 0.000862 0.045379 0.482821 0.059885 \n", "0 0.496441 0.007423 0.000235 0.042533 0.602121 0.010823 \n", "0 0.496433 0.009544 0.000220 0.042809 0.699046 0.005051 \n", "0 0.496424 0.009544 0.000201 0.042622 0.600530 0.005051 \n", "0 0.496391 0.003181 0.000118 0.041755 0.392153 0.115440 \n", "\n", " Shannon Gini \n", "0 4.549663 0.950182 \n", "0 3.875892 0.974947 \n", "0 3.159079 0.987317 \n", "0 5.140721 0.909056 \n", "0 4.395721 0.954872 \n", "0 3.845461 0.973440 \n", "0 2.836513 0.991139 \n", "0 4.165618 0.964211 \n", "0 3.611806 0.978659 \n", "0 2.761238 0.991660 \n", "0 2.711772 0.992003 \n", "0 5.100443 0.906900 \n", "0 5.133650 0.877999 \n", "0 2.232578 0.994487 \n", "0 2.089186 0.995706 \n", "0 1.945910 0.995669 \n", "0 1.803126 0.996380 \n", "0 4.174741 0.965327 " ] }, "execution_count": 57, "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)" ] } ], "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.6" } }, "nbformat": 4, "nbformat_minor": 4 }