{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.12","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":31254,"databundleVersionId":3103714,"sourceType":"competition"}],"dockerImageVersionId":30587,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport os\n# for dirname, _, filenames in os.walk('/kaggle/input'):\n#     for filename in filenames:\n#         print(os.path.join(dirname, filename))\n","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-11-16T15:42:27.349443Z","iopub.execute_input":"2023-11-16T15:42:27.349791Z","iopub.status.idle":"2023-11-16T15:42:27.361203Z","shell.execute_reply.started":"2023-11-16T15:42:27.349759Z","shell.execute_reply":"2023-11-16T15:42:27.359877Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Scenario 1: The relevant items are predicted at the top of the list.\n# Scenario 2: The relevant items are predicted towards the bottom of the list.\n# In Scenario 1, the precision at each cutoff k will be higher, resulting in a higher average precision, and thus a higher MAP@12 score, compared to Scenario 2.\n\ndef apk(actual, predicted, k=12):\n    \"\"\"\n    Computes the average precision at k between two lists of items.\n    \"\"\"\n    if len(predicted) > k:\n        predicted = predicted[:k]\n\n    score = 0.0\n    num_hits = 0.0\n\n    for i, p in enumerate(predicted):\n        if p in actual and p not in predicted[:i]:\n            num_hits += 1.0\n            score += num_hits / (i + 1.0)\n\n    if not actual:\n        return 0.0\n\n    return score / min(len(actual), k)\n\ndef mapk(actual, predicted, k=12):\n    \"\"\"\n    Computes the mean average precision at k.\n    \"\"\"\n    return sum(apk(a, p, k) for a, p in zip(actual, predicted)) / len(actual)\n\n# Example usage\nactual = [ [1, 2,3,4]] # Actual items purchased by 3 customers\npredicted = [ [1, 2,0,0]] # Top 12 predicted items for these 3 customers\n\nprint(\"MAP@12:\", mapk(actual, predicted))\npredicted = [ [0, 0,3,4]] # Top 12 predicted items for these 3 customers\n\nprint(\"MAP@12:\", mapk(actual, predicted))","metadata":{"execution":{"iopub.status.busy":"2023-11-16T15:55:57.083601Z","iopub.execute_input":"2023-11-16T15:55:57.083997Z","iopub.status.idle":"2023-11-16T15:55:57.096141Z","shell.execute_reply.started":"2023-11-16T15:55:57.083968Z","shell.execute_reply":"2023-11-16T15:55:57.095179Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## ","metadata":{}}]}