{"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_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Introduction:\n\n* Session = user, Aid = product\n\nThe competition's objective is to predict e-commerce clicks, cart additions, and orders using information from previous user session events. There are three types of interactions and 1671803 unique sessions overall (Clicks, Carts and Orders). Therefore, 5015409 predictions (1671803*3) would be made.\n\nDetails could be found in this page: https://www.kaggle.com/competitions/otto-recommender-system/overview/evaluation\n\n# In short: \n\nThe only product that needs to be predicted for the click prediction is the **next one** the user will click, for instances: aid1. However, we can submit a list of up to 20 products for prediction; **as long as aid1 is on the list, the score will be awarded.**\n\nWe could also submit a list of products (maximum 20) for prediction for cart and orders.  For example, the user adds **30** products into **cart**, and **order 3** products.\n\nThe maximum size of a prediction is 20, so **even though there are 30 products in the cart, the full score can still be obtained as long as every item on the prediction list appears in the cart**.\n\nSince 3 products were ordered, I can still **receive a full score as long as all three appear on the prediction list.**\n\n\n","metadata":{}},{"cell_type":"markdown","source":"# Method:\n\nI would predict the next aid(s) by only the **last 20 aids that the users have interacted with.**\n","metadata":{}},{"cell_type":"code","source":"# Import Module\nfrom collections import OrderedDict\nimport pandas as pd\nimport numpy as np\nimport warnings\n# ignore all warnings\nwarnings. filterwarnings(\"ignore\")\nprint(\"Imported\")\n\n## Load Data\ndf = pd.read_parquet('/kaggle/input/otto-full-optimized-memory-footprint/test.parquet')\nprint(df)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T11:09:23.442043Z","iopub.execute_input":"2023-02-01T11:09:23.442544Z","iopub.status.idle":"2023-02-01T11:09:23.664594Z","shell.execute_reply.started":"2023-02-01T11:09:23.442498Z","shell.execute_reply":"2023-02-01T11:09:23.662895Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"As there are cold users, users' interactions less than the max prediction 20, we would add the top 20 interacted items into the prediction list","metadata":{}},{"cell_type":"code","source":"productcount = pd.DataFrame()\nproductcount[\"count\"] = df['aid'].value_counts()\ntop20 = productcount[\"count\"].tolist()[:20]\nprint(top20)\nprint(productcount)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T11:09:23.666605Z","iopub.execute_input":"2023-02-01T11:09:23.667141Z","iopub.status.idle":"2023-02-01T11:09:24.107920Z","shell.execute_reply.started":"2023-02-01T11:09:23.667108Z","shell.execute_reply":"2023-02-01T11:09:24.106311Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# # Get last 20 unique aids for each session\n\ndf = df.sort_values([\"session\", \"type\", \"ts\"]).groupby([\"session\"]).apply(\n    lambda x: list(OrderedDict.fromkeys(x.tail(40).aid.tolist() + top20).keys())[:20])\nprint(df)\nprint(\"==\"*32)\n\nsubmission = pd.DataFrame()\nfor i in ['_clicks','_carts','_orders']:\n    new_df = pd.DataFrame(df.add_suffix(str(i)), columns=[\"labels\"]).reset_index()\n    print(new_df)\n    submission = pd.concat(\n        [new_df, submission]\n    )\n\nsubmission = submission.rename(columns={'session': 'session_type'})\nsubmission[\"labels\"] = submission['labels'].apply(lambda x: \" \".join(str(i) for i in x))\nprint(submission)\nsubmission.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2023-02-01T11:14:14.749866Z","iopub.execute_input":"2023-02-01T11:14:14.750334Z","iopub.status.idle":"2023-02-01T11:14:47.716364Z","shell.execute_reply.started":"2023-02-01T11:14:14.750300Z","shell.execute_reply":"2023-02-01T11:14:47.715099Z"},"trusted":true},"execution_count":null,"outputs":[]}]}