{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"collapsed":false},"outputs":[],"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"},{"cell_type":"markdown","metadata":{},"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"},{"cell_type":"code","execution_count":null,"metadata":{"collapsed":false},"outputs":[],"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) )"},{"cell_type":"markdown","metadata":{},"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"},{"cell_type":"code","execution_count":null,"metadata":{"collapsed":false},"outputs":[],"source":"metrics.mapk([[1],[1],[1],[1],[1]],[[1,2,3,4,9,7,5],[2,1,3,4,5],[3,2,1,4,5],[4,2,3,1,5],[4,2,3,5,1]], 5)"},{"cell_type":"code","execution_count":null,"metadata":{"collapsed":false},"outputs":[],"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)"}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"}},"nbformat":4,"nbformat_minor":0}