{"cells":[{"metadata":{"trusted":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nfrom subprocess import check_output\n\nimport ml_metrics as metrics\n\n","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"##I am going to show how order matters in MAP@K when there is only 1 answer. \n\n##This experiment is done by calculating AP@K, which gives 1 value. MAP@K is the average of AP@K. \n"},{"metadata":{"trusted":true},"cell_type":"code","source":"actual = [1]\n\npredicted = [1,2,3,4,5]\n\nprint('Answer=',actual,'predicted=',predicted)\nprint('AP@5 =',metrics.apk(actual,predicted,5) )\n\npredicted = [2,1,3,4,5]\nprint('Answer=',actual,'predicted=',predicted)\nprint('AP@5 =',metrics.apk(actual,predicted,5) )\n\npredicted = [3,2,1,4,5]\nprint('Answer=',actual,'predicted=',predicted)\nprint('AP@5 =',metrics.apk(actual,predicted,5) )\n\npredicted = [4,2,3,1,5]\nprint('Answer=',actual,'predicted=',predicted)\nprint('AP@5 =',metrics.apk(actual,predicted,5) )\n\npredicted = [4,2,3,5,1]\nprint('Answer=',actual,'predicted=',predicted)\nprint('AP@5 =',metrics.apk(actual,predicted,5) )","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"actual = [1,2]\n\npredicted = [1,2,3,4,5]\n\nprint('Answer=',actual,'predicted=',predicted)\nprint('AP@5 =',metrics.apk(actual,predicted,5) )\n\npredicted = [2,1,3,4,5]\nprint('Answer=',actual,'predicted=',predicted)\nprint('AP@5 =',metrics.apk(actual,predicted,5) )\n\npredicted = [3,2,1,4,5]\nprint('Answer=',actual,'predicted=',predicted)\nprint('AP@5 =',metrics.apk(actual,predicted,5) )\n\npredicted = [4,2,3,1,5]\nprint('Answer=',actual,'predicted=',predicted)\nprint('AP@5 =',metrics.apk(actual,predicted,5) )\n\npredicted = [4,2,3,5,1]\nprint('Answer=',actual,'predicted=',predicted)\nprint('AP@5 =',metrics.apk(actual,predicted,5) )","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"As you see from the above example, the \"earlier\" you predict the correct answer 1, the higher your score. \n\n### Next is an example of how MAP@K is calculated with a list of hotels\nI took the list of predictions from the previous AP@K example, and calculated the MAP@K. You can see the MAP@K is the average of AP@K\n"},{"metadata":{"trusted":true},"cell_type":"code","source":"metrics.mapk([[1],[1],[1],[1],[1]],[[1,2,3,4,5],[2,1,3,4,5],[3,2,1,4,5],[4,2,3,1,5],[4,2,3,5,1]], 5)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":4}