{"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":"Grabbing the last 20 aid for a session and using them as prediction.","metadata":{}},{"cell_type":"code","source":"import pandas as pd\n\ntrain = pd.read_parquet('../input/otto-full-optimized-memory-footprint/train.parquet')\ntest = pd.read_parquet('../input/otto-full-optimized-memory-footprint/test.parquet')\n\n!pip install pickle5\nimport pickle5 as pickle\n\nwith open('../input/otto-full-optimized-memory-footprint/id2type.pkl', \"rb\") as fh:\n    id2type = pickle.load(fh)\nwith open('../input/otto-full-optimized-memory-footprint/type2id.pkl', \"rb\") as fh:\n    type2id = pickle.load(fh)\n    \nsample_sub = pd.read_csv('../input/otto-recommender-system/sample_submission.csv')","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-11-03T12:41:45.632798Z","iopub.execute_input":"2022-11-03T12:41:45.633596Z","iopub.status.idle":"2022-11-03T12:42:21.935105Z","shell.execute_reply.started":"2022-11-03T12:41:45.633516Z","shell.execute_reply":"2022-11-03T12:42:21.933495Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_sub.head()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-03T12:42:26.691387Z","iopub.execute_input":"2022-11-03T12:42:26.691896Z","iopub.status.idle":"2022-11-03T12:42:26.706160Z","shell.execute_reply.started":"2022-11-03T12:42:26.691848Z","shell.execute_reply":"2022-11-03T12:42:26.704735Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\ntest = test.sort_values(['session', 'ts'])","metadata":{"execution":{"iopub.status.busy":"2022-11-03T12:42:26.708873Z","iopub.execute_input":"2022-11-03T12:42:26.709390Z","iopub.status.idle":"2022-11-03T12:42:35.991086Z","shell.execute_reply.started":"2022-11-03T12:42:26.709354Z","shell.execute_reply":"2022-11-03T12:42:35.989675Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"session_aids = test.groupby('session')['aid'].apply(lambda x: list(x)[-20:])","metadata":{"execution":{"iopub.status.busy":"2022-11-03T13:34:43.284724Z","iopub.execute_input":"2022-11-03T13:34:43.285282Z","iopub.status.idle":"2022-11-03T13:35:17.521635Z","shell.execute_reply.started":"2022-11-03T13:34:43.285236Z","shell.execute_reply":"2022-11-03T13:35:17.520106Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"session_types = ['clicks', 'carts', 'orders']","metadata":{"execution":{"iopub.status.busy":"2022-11-03T13:35:31.145956Z","iopub.execute_input":"2022-11-03T13:35:31.146446Z","iopub.status.idle":"2022-11-03T13:35:31.153223Z","shell.execute_reply.started":"2022-11-03T13:35:31.146406Z","shell.execute_reply":"2022-11-03T13:35:31.151592Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"session_aids.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-03T13:35:36.498757Z","iopub.execute_input":"2022-11-03T13:35:36.499317Z","iopub.status.idle":"2022-11-03T13:35:36.510648Z","shell.execute_reply.started":"2022-11-03T13:35:36.499274Z","shell.execute_reply":"2022-11-03T13:35:36.509519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"%%time\n\nsession_type = []\nlabels = []\n\nfor session, aids in session_aids.iteritems():\n    for st in session_types:\n        session_type.append(f'{session}_{st}')\n        labels.append(' '.join([str(a) for a in aids]))","metadata":{"execution":{"iopub.status.busy":"2022-11-03T13:39:34.734284Z","iopub.execute_input":"2022-11-03T13:39:34.734770Z","iopub.status.idle":"2022-11-03T13:39:45.815309Z","shell.execute_reply.started":"2022-11-03T13:39:34.734734Z","shell.execute_reply":"2022-11-03T13:39:45.813961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission = pd.DataFrame({'session_type': session_type, 'labels': labels})","metadata":{"execution":{"iopub.status.busy":"2022-11-03T13:40:19.575942Z","iopub.execute_input":"2022-11-03T13:40:19.576525Z","iopub.status.idle":"2022-11-03T13:40:20.787709Z","shell.execute_reply.started":"2022-11-03T13:40:19.576481Z","shell.execute_reply":"2022-11-03T13:40:20.786337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-03T13:41:12.866964Z","iopub.execute_input":"2022-11-03T13:41:12.868341Z","iopub.status.idle":"2022-11-03T13:41:12.884073Z","shell.execute_reply.started":"2022-11-03T13:41:12.868296Z","shell.execute_reply":"2022-11-03T13:41:12.882817Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv('submission.csv', index=False)","metadata":{},"execution_count":null,"outputs":[]}]}