{"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":"code","source":"import os\nimport numpy as np\nimport pandas as pd\nimport gc","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-03-14T04:08:21.844797Z","iopub.execute_input":"2022-03-14T04:08:21.845101Z","iopub.status.idle":"2022-03-14T04:08:21.849459Z","shell.execute_reply.started":"2022-03-14T04:08:21.845052Z","shell.execute_reply":"2022-03-14T04:08:21.848609Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!ls ../input/hm-public-submissions","metadata":{"execution":{"iopub.status.busy":"2022-03-14T04:08:21.850774Z","iopub.execute_input":"2022-03-14T04:08:21.850936Z","iopub.status.idle":"2022-03-14T04:08:22.16865Z","shell.execute_reply.started":"2022-03-14T04:08:21.850917Z","shell.execute_reply":"2022-03-14T04:08:22.167777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# **Adding LSTM models to the ensemble**","metadata":{}},{"cell_type":"markdown","source":"# Note\n* I am not sure whether I should get any credit for this notebook as this is based(mostly) on the work of others.\n* I added two more submissions to the ensemble and tried a few options with them.","metadata":{}},{"cell_type":"markdown","source":"# New Notebooks\n* LB: 0.0210 - https://www.kaggle.com/astrung/recbole-lstm-sequential-for-recomendation-tutorial\n* LB: 0.0221 - https://www.kaggle.com/astrung/lstm-sequential-modelwith-item-features-tutorial\n* both notebooks by @astrung","metadata":{}},{"cell_type":"markdown","source":"# Predictions in this competition are a list of 12 itens ordered by most relevant first.\n# In this notebook I will show how to ensemble lists of different models\n# To ensemble I used submissions from 3 public notebooks:\n- LB: 0.0225 - https://www.kaggle.com/lichtlab/0-0226-byfone-chris-combination-approach/data?scriptVersionId=89289696\n- LB: 0.0225 - https://www.kaggle.com/lunapandachan/h-m-trending-products-weekly-add-test/notebook\n- LB: 0.0217 - https://www.kaggle.com/tarique7/hnm-exponential-decay-with-alternate-items/notebook","metadata":{}},{"cell_type":"code","source":"sub0 = pd.read_csv('../input/hm-public-submissions/0-0226-byfone-chris-combination-approach.csv').sort_values('customer_id').reset_index(drop=True)\nsub1 = pd.read_csv('../input/hm-public-submissions/h-m-trending-products-weekly-add-test.csv').sort_values('customer_id').reset_index(drop=True)\nsub2 = pd.read_csv('../input/hm-public-submissions/hnm-exponential-decay-with-alternate-items.csv').sort_values('customer_id').reset_index(drop=True)\n\nsub0.shape, sub1.shape, sub2.shape","metadata":{"execution":{"iopub.status.busy":"2022-03-14T04:08:22.170235Z","iopub.execute_input":"2022-03-14T04:08:22.171836Z","iopub.status.idle":"2022-03-14T04:08:44.791279Z","shell.execute_reply.started":"2022-03-14T04:08:22.171806Z","shell.execute_reply":"2022-03-14T04:08:44.79038Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub3 = pd.read_csv('../input/submission-recbole-lstm/submission.csv').sort_values('customer_id').reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T04:08:44.793483Z","iopub.execute_input":"2022-03-14T04:08:44.793803Z","iopub.status.idle":"2022-03-14T04:08:51.930568Z","shell.execute_reply.started":"2022-03-14T04:08:44.793768Z","shell.execute_reply":"2022-03-14T04:08:51.929633Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub4 = pd.read_csv('../input/submission-lstm-sequential/submission (1).csv').sort_values('customer_id').reset_index(drop=True)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T04:08:51.931773Z","iopub.execute_input":"2022-03-14T04:08:51.932039Z","iopub.status.idle":"2022-03-14T04:08:59.071104Z","shell.execute_reply.started":"2022-03-14T04:08:51.932003Z","shell.execute_reply":"2022-03-14T04:08:59.070233Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# How many predictions are in common between models\n\nprint((sub0['prediction']==sub1['prediction']).mean())\nprint((sub0['prediction']==sub2['prediction']).mean())\nprint((sub1['prediction']==sub2['prediction']).mean())","metadata":{"execution":{"iopub.status.busy":"2022-03-14T04:08:59.073267Z","iopub.execute_input":"2022-03-14T04:08:59.073591Z","iopub.status.idle":"2022-03-14T04:08:59.96959Z","shell.execute_reply.started":"2022-03-14T04:08:59.073553Z","shell.execute_reply":"2022-03-14T04:08:59.96843Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# How many predictions are in common between old models and new \n\nprint((sub3['prediction']==sub0['prediction']).mean())\nprint((sub3['prediction']==sub1['prediction']).mean())\nprint((sub3['prediction']==sub2['prediction']).mean())","metadata":{"execution":{"iopub.status.busy":"2022-03-14T04:08:59.972173Z","iopub.execute_input":"2022-03-14T04:08:59.97271Z","iopub.status.idle":"2022-03-14T04:09:00.851728Z","shell.execute_reply.started":"2022-03-14T04:08:59.972672Z","shell.execute_reply":"2022-03-14T04:09:00.850678Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# How many predictions are in common between old models and new\n\nprint((sub4['prediction']==sub0['prediction']).mean())\nprint((sub4['prediction']==sub1['prediction']).mean())\nprint((sub4['prediction']==sub2['prediction']).mean())\nprint((sub4['prediction']==sub3['prediction']).mean())","metadata":{"execution":{"iopub.status.busy":"2022-03-14T04:09:00.852999Z","iopub.execute_input":"2022-03-14T04:09:00.853308Z","iopub.status.idle":"2022-03-14T04:09:02.042739Z","shell.execute_reply.started":"2022-03-14T04:09:00.853269Z","shell.execute_reply":"2022-03-14T04:09:02.041654Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub0.columns = ['customer_id', 'prediction0']\nsub0['prediction1'] = sub1['prediction']\nsub0['prediction2'] = sub2['prediction']\nsub0['prediction3'] = sub3['prediction']\nsub0['prediction4'] = sub4['prediction']\ndel sub1, sub2, sub3, sub4\ngc.collect()\nsub0.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-14T04:09:02.044746Z","iopub.execute_input":"2022-03-14T04:09:02.044985Z","iopub.status.idle":"2022-03-14T04:09:02.380301Z","shell.execute_reply.started":"2022-03-14T04:09:02.044956Z","shell.execute_reply":"2022-03-14T04:09:02.379549Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def cust_blend(dt, W = [1,1,1,1]):\n    #Global ensemble weights\n    #W = [1.15,0.95,0.85]\n    \n    #Create a list of all model predictions\n    REC = []\n    \n    # Second Try\n    REC.append(dt['prediction0'].split())\n    REC.append(dt['prediction1'].split())\n    REC.append(dt['prediction2'].split())\n    REC.append(dt['prediction3'].split())\n    \n    #Create a dictionary of items recommended. \n    #Assign a weight according the order of appearance and multiply by global weights\n    res = {}\n    for M in range(len(REC)):\n        for n, v in enumerate(REC[M]):\n            if v in res:\n                res[v] += (W[M]/(n+1))\n            else:\n                res[v] = (W[M]/(n+1))\n    \n    # Sort dictionary by item weights\n    res = list(dict(sorted(res.items(), key=lambda item: -item[1])).keys())\n    \n    # Return the top 12 itens only\n    return ' '.join(res[:12])","metadata":{"execution":{"iopub.status.busy":"2022-03-14T04:09:02.381376Z","iopub.execute_input":"2022-03-14T04:09:02.382248Z","iopub.status.idle":"2022-03-14T04:09:02.390449Z","shell.execute_reply.started":"2022-03-14T04:09:02.382214Z","shell.execute_reply":"2022-03-14T04:09:02.389454Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub0['prediction'] = sub0.apply(cust_blend, W = [1.05,1.00,0.95,0.85], axis=1)\nsub0.head()","metadata":{"execution":{"iopub.status.busy":"2022-03-14T04:09:02.394194Z","iopub.execute_input":"2022-03-14T04:09:02.394484Z","iopub.status.idle":"2022-03-14T04:10:08.624623Z","shell.execute_reply.started":"2022-03-14T04:09:02.394455Z","shell.execute_reply":"2022-03-14T04:10:08.623104Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# How many predictions are in common with ensemble\n\nprint((sub0['prediction']==sub0['prediction0']).mean())\nprint((sub0['prediction']==sub0['prediction1']).mean())\nprint((sub0['prediction']==sub0['prediction2']).mean())\nprint((sub0['prediction']==sub0['prediction3']).mean())\nprint((sub0['prediction']==sub0['prediction4']).mean())","metadata":{"execution":{"iopub.status.busy":"2022-03-14T04:10:08.626978Z","iopub.execute_input":"2022-03-14T04:10:08.627244Z","iopub.status.idle":"2022-03-14T04:10:09.261993Z","shell.execute_reply.started":"2022-03-14T04:10:08.62722Z","shell.execute_reply":"2022-03-14T04:10:09.260884Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**The results of comparison seem interesting**","metadata":{}},{"cell_type":"markdown","source":"# Make a submission","metadata":{}},{"cell_type":"code","source":"del sub0['prediction0']\ndel sub0['prediction1']\ndel sub0['prediction2']\ndel sub0['prediction3']\ndel sub0['prediction4']\ngc.collect()\nsub0.to_csv('submission.csv', index=False)","metadata":{"execution":{"iopub.status.busy":"2022-03-14T04:10:09.263459Z","iopub.execute_input":"2022-03-14T04:10:09.263636Z","iopub.status.idle":"2022-03-14T04:10:18.634758Z","shell.execute_reply.started":"2022-03-14T04:10:09.263615Z","shell.execute_reply":"2022-03-14T04:10:18.633876Z"},"trusted":true},"execution_count":null,"outputs":[]}]}